@@ -18,6 +18,11 @@ use crate::util::intern::AtomPool;
18
18
use super :: fulltext:: FulltextDefinition ;
19
19
use super :: { ApiSchema , Schema , SchemaValidationError } ;
20
20
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`
21
26
#[ derive( Clone , Debug , PartialEq ) ]
22
27
pub struct InputSchema {
23
28
inner : Arc < Inner > ,
@@ -30,14 +35,6 @@ pub struct Inner {
30
35
pool : Arc < AtomPool > ,
31
36
}
32
37
33
- impl std:: ops:: Deref for InputSchema {
34
- type Target = Inner ;
35
-
36
- fn deref ( & self ) -> & Self :: Target {
37
- & self . inner
38
- }
39
- }
40
-
41
38
impl CheapClone for InputSchema {
42
39
fn cheap_clone ( & self ) -> Self {
43
40
InputSchema {
@@ -68,22 +65,29 @@ impl InputSchema {
68
65
}
69
66
}
70
67
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.
71
71
pub fn new ( id : DeploymentHash , document : s:: Document ) -> Result < Self , SchemaValidationError > {
72
72
let schema = Schema :: new ( id, document) ?;
73
73
Ok ( Self :: create ( schema) )
74
74
}
75
75
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.
76
80
pub fn parse ( raw : & str , id : DeploymentHash ) -> Result < Self , Error > {
77
81
let schema = Schema :: parse ( raw, id) ?;
78
82
79
83
Ok ( Self :: create ( schema) )
80
84
}
81
- }
82
85
83
- impl Inner {
86
+ /// Generate the `ApiSchema` for use with GraphQL queries for this
87
+ /// `InputSchema`
84
88
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 ) ?;
87
91
schema. add_subgraph_id_directives ( schema. id . clone ( ) ) ;
88
92
ApiSchema :: from_api_schema ( schema)
89
93
}
@@ -105,6 +109,7 @@ impl Inner {
105
109
/// This function will return the type "Wallet" with the field "account"
106
110
pub fn get_field_related ( & self , key : & LoadRelatedRequest ) -> Result < ( & str , & s:: Field ) , Error > {
107
111
let field = self
112
+ . inner
108
113
. schema
109
114
. document
110
115
. get_object_type_definition ( key. entity_type . as_str ( ) )
@@ -131,6 +136,7 @@ impl Inner {
131
136
let field_name = derived_from. argument ( "field" ) . unwrap ( ) ;
132
137
133
138
let field = self
139
+ . inner
134
140
. schema
135
141
. document
136
142
. get_object_type_definition ( base_type)
@@ -166,6 +172,7 @@ impl Inner {
166
172
/// Construct a value for the entity type's id attribute
167
173
pub fn id_value ( & self , key : & EntityKey ) -> Result < store:: Value , Error > {
168
174
let base_type = self
175
+ . inner
169
176
. schema
170
177
. document
171
178
. get_object_type_definition ( key. entity_type . as_str ( ) )
@@ -198,21 +205,23 @@ impl Inner {
198
205
}
199
206
200
207
pub fn is_immutable ( & self , entity_type : & EntityType ) -> bool {
201
- self . immutable_types . contains ( entity_type)
208
+ self . inner . immutable_types . contains ( entity_type)
202
209
}
203
210
204
211
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)
206
213
}
207
214
208
215
pub fn types_for_interface ( & self , intf : & s:: InterfaceType ) -> Option < & Vec < s:: ObjectType > > {
209
- self . schema
216
+ self . inner
217
+ . schema
210
218
. types_for_interface
211
219
. get ( & EntityType :: new ( intf. name . clone ( ) ) )
212
220
}
213
221
214
222
pub fn find_object_type ( & self , entity_type : & EntityType ) -> Option < & s:: ObjectType > {
215
- self . schema
223
+ self . inner
224
+ . schema
216
225
. document
217
226
. definitions
218
227
. iter ( )
@@ -224,22 +233,22 @@ impl Inner {
224
233
}
225
234
226
235
pub fn get_enum_definitions ( & self ) -> Vec < & s:: EnumType > {
227
- self . schema . document . get_enum_definitions ( )
236
+ self . inner . schema . document . get_enum_definitions ( )
228
237
}
229
238
230
239
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 ( )
232
241
}
233
242
234
243
pub fn interface_types ( & self ) -> & BTreeMap < EntityType , Vec < s:: ObjectType > > {
235
- & self . schema . types_for_interface
244
+ & self . inner . schema . types_for_interface
236
245
}
237
246
238
247
pub fn entity_fulltext_definitions (
239
248
& self ,
240
249
entity : & str ,
241
250
) -> Result < Vec < FulltextDefinition > , anyhow:: Error > {
242
- Self :: fulltext_definitions ( & self . schema . document , entity)
251
+ Self :: fulltext_definitions ( & self . inner . schema . document , entity)
243
252
}
244
253
245
254
fn fulltext_definitions (
@@ -268,23 +277,23 @@ impl Inner {
268
277
}
269
278
270
279
pub fn id ( & self ) -> & DeploymentHash {
271
- & self . schema . id
280
+ & self . inner . schema . id
272
281
}
273
282
274
283
pub fn document_string ( & self ) -> String {
275
- self . schema . document . to_string ( )
284
+ self . inner . schema . document . to_string ( )
276
285
}
277
286
278
287
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 ( )
280
289
}
281
290
282
291
pub ( crate ) fn validate ( & self ) -> Result < ( ) , Vec < SchemaValidationError > > {
283
- self . schema . validate ( )
292
+ self . inner . schema . validate ( )
284
293
}
285
294
286
295
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)
288
297
}
289
298
290
299
pub fn try_make_entity <
@@ -294,7 +303,7 @@ impl Inner {
294
303
& self ,
295
304
iter : I ,
296
305
) -> Result < Entity , Error > {
297
- Entity :: try_make ( self . pool . clone ( ) , iter)
306
+ Entity :: try_make ( self . inner . pool . clone ( ) , iter)
298
307
}
299
308
}
300
309
@@ -348,7 +357,7 @@ fn atom_pool(document: &s::Document) -> AtomPool {
348
357
}
349
358
350
359
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 ( ) {
352
361
pool. intern ( defn. name . as_str ( ) ) ;
353
362
}
354
363
}
0 commit comments