Skip to content

Commit d14c155

Browse files
committed
graph: Address review comments
1 parent 8a620be commit d14c155

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

graph/src/schema/api.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ impl TryFrom<&r::Value> for ErrorPolicy {
7373
}
7474
}
7575

76+
/// The schema used for responding to queries. It is generated from an
77+
/// `InputSchema` by calling `api_schema` on it. Code that handles GraphQL
78+
/// queries against a subgraph should use an `ApiSchema` to access the
79+
/// underlying GraphQL schema
7680
#[derive(Debug)]
7781
pub struct ApiSchema {
7882
schema: Schema,

graph/src/schema/input_schema.rs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ use crate::util::intern::AtomPool;
1818
use super::fulltext::FulltextDefinition;
1919
use super::{ApiSchema, Schema, SchemaValidationError};
2020

21+
/// The internal representation of a subgraph schema, i.e., the
22+
/// `schema.graphql` file that is part of a subgraph. Any code that deals
23+
/// with writing a subgraph should use this struct. Code that deals with
24+
/// querying subgraphs will instead want to use an `ApiSchema` which can be
25+
/// generated with the `api_schema` method on `InputSchema`
2126
#[derive(Clone, Debug, PartialEq)]
2227
pub struct InputSchema {
2328
inner: Arc<Inner>,
@@ -30,14 +35,6 @@ pub struct Inner {
3035
pool: Arc<AtomPool>,
3136
}
3237

33-
impl std::ops::Deref for InputSchema {
34-
type Target = Inner;
35-
36-
fn deref(&self) -> &Self::Target {
37-
&self.inner
38-
}
39-
}
40-
4138
impl CheapClone for InputSchema {
4239
fn cheap_clone(&self) -> Self {
4340
InputSchema {
@@ -68,22 +65,29 @@ impl InputSchema {
6865
}
6966
}
7067

68+
/// Create a new `InputSchema` from the GraphQL document that resulted
69+
/// from parsing a subgraph's `schema.graphql`. The document must have
70+
/// already been validated.
7171
pub fn new(id: DeploymentHash, document: s::Document) -> Result<Self, SchemaValidationError> {
7272
let schema = Schema::new(id, document)?;
7373
Ok(Self::create(schema))
7474
}
7575

76+
/// A convenience function for creating an `InputSchema` from the string
77+
/// representation of the subgraph's GraphQL schema `raw` and its
78+
/// deployment hash `id`. An `InputSchema` that is constructed with this
79+
/// function still has to be validated after construction.
7680
pub fn parse(raw: &str, id: DeploymentHash) -> Result<Self, Error> {
7781
let schema = Schema::parse(raw, id)?;
7882

7983
Ok(Self::create(schema))
8084
}
81-
}
8285

83-
impl Inner {
86+
/// Generate the `ApiSchema` for use with GraphQL queries for this
87+
/// `InputSchema`
8488
pub fn api_schema(&self) -> Result<ApiSchema, anyhow::Error> {
85-
let mut schema = self.schema.clone();
86-
schema.document = api_schema(&self.schema.document)?;
89+
let mut schema = self.inner.schema.clone();
90+
schema.document = api_schema(&self.inner.schema.document)?;
8791
schema.add_subgraph_id_directives(schema.id.clone());
8892
ApiSchema::from_api_schema(schema)
8993
}
@@ -105,6 +109,7 @@ impl Inner {
105109
/// This function will return the type "Wallet" with the field "account"
106110
pub fn get_field_related(&self, key: &LoadRelatedRequest) -> Result<(&str, &s::Field), Error> {
107111
let field = self
112+
.inner
108113
.schema
109114
.document
110115
.get_object_type_definition(key.entity_type.as_str())
@@ -131,6 +136,7 @@ impl Inner {
131136
let field_name = derived_from.argument("field").unwrap();
132137

133138
let field = self
139+
.inner
134140
.schema
135141
.document
136142
.get_object_type_definition(base_type)
@@ -166,6 +172,7 @@ impl Inner {
166172
/// Construct a value for the entity type's id attribute
167173
pub fn id_value(&self, key: &EntityKey) -> Result<store::Value, Error> {
168174
let base_type = self
175+
.inner
169176
.schema
170177
.document
171178
.get_object_type_definition(key.entity_type.as_str())
@@ -198,21 +205,23 @@ impl Inner {
198205
}
199206

200207
pub fn is_immutable(&self, entity_type: &EntityType) -> bool {
201-
self.immutable_types.contains(entity_type)
208+
self.inner.immutable_types.contains(entity_type)
202209
}
203210

204211
pub fn get_named_type(&self, name: &str) -> Option<&s::TypeDefinition> {
205-
self.schema.document.get_named_type(name)
212+
self.inner.schema.document.get_named_type(name)
206213
}
207214

208215
pub fn types_for_interface(&self, intf: &s::InterfaceType) -> Option<&Vec<s::ObjectType>> {
209-
self.schema
216+
self.inner
217+
.schema
210218
.types_for_interface
211219
.get(&EntityType::new(intf.name.clone()))
212220
}
213221

214222
pub fn find_object_type(&self, entity_type: &EntityType) -> Option<&s::ObjectType> {
215-
self.schema
223+
self.inner
224+
.schema
216225
.document
217226
.definitions
218227
.iter()
@@ -224,22 +233,22 @@ impl Inner {
224233
}
225234

226235
pub fn get_enum_definitions(&self) -> Vec<&s::EnumType> {
227-
self.schema.document.get_enum_definitions()
236+
self.inner.schema.document.get_enum_definitions()
228237
}
229238

230239
pub fn get_object_type_definitions(&self) -> Vec<&s::ObjectType> {
231-
self.schema.document.get_object_type_definitions()
240+
self.inner.schema.document.get_object_type_definitions()
232241
}
233242

234243
pub fn interface_types(&self) -> &BTreeMap<EntityType, Vec<s::ObjectType>> {
235-
&self.schema.types_for_interface
244+
&self.inner.schema.types_for_interface
236245
}
237246

238247
pub fn entity_fulltext_definitions(
239248
&self,
240249
entity: &str,
241250
) -> Result<Vec<FulltextDefinition>, anyhow::Error> {
242-
Self::fulltext_definitions(&self.schema.document, entity)
251+
Self::fulltext_definitions(&self.inner.schema.document, entity)
243252
}
244253

245254
fn fulltext_definitions(
@@ -268,23 +277,23 @@ impl Inner {
268277
}
269278

270279
pub fn id(&self) -> &DeploymentHash {
271-
&self.schema.id
280+
&self.inner.schema.id
272281
}
273282

274283
pub fn document_string(&self) -> String {
275-
self.schema.document.to_string()
284+
self.inner.schema.document.to_string()
276285
}
277286

278287
pub fn get_fulltext_directives(&self) -> Result<Vec<&s::Directive>, Error> {
279-
self.schema.document.get_fulltext_directives()
288+
self.inner.schema.document.get_fulltext_directives()
280289
}
281290

282291
pub(crate) fn validate(&self) -> Result<(), Vec<SchemaValidationError>> {
283-
self.schema.validate()
292+
self.inner.schema.validate()
284293
}
285294

286295
pub fn make_entity<I: IntoEntityIterator>(&self, iter: I) -> Result<Entity, Error> {
287-
Entity::make(self.pool.clone(), iter)
296+
Entity::make(self.inner.pool.clone(), iter)
288297
}
289298

290299
pub fn try_make_entity<
@@ -294,7 +303,7 @@ impl Inner {
294303
&self,
295304
iter: I,
296305
) -> Result<Entity, Error> {
297-
Entity::try_make(self.pool.clone(), iter)
306+
Entity::try_make(self.inner.pool.clone(), iter)
298307
}
299308
}
300309

@@ -348,7 +357,7 @@ fn atom_pool(document: &s::Document) -> AtomPool {
348357
}
349358

350359
for object_type in document.get_object_type_definitions() {
351-
for defn in Inner::fulltext_definitions(&document, &object_type.name).unwrap() {
360+
for defn in InputSchema::fulltext_definitions(&document, &object_type.name).unwrap() {
352361
pool.intern(defn.name.as_str());
353362
}
354363
}

0 commit comments

Comments
 (0)