@@ -10,7 +10,6 @@ use proc_macro2::TokenStream;
10
10
use query:: QueryContext ;
11
11
use std:: collections:: { BTreeMap , BTreeSet } ;
12
12
use unions:: GqlUnion ;
13
- use introspection_response;
14
13
15
14
pub const DEFAULT_SCALARS : & [ & ' static str ] = & [ "ID" , "String" , "Int" , "Float" , "Boolean" ] ;
16
15
@@ -221,7 +220,8 @@ impl ::std::convert::From<graphql_parser::schema::Document> for Schema {
221
220
schema. scalars . insert ( scalar. name ) ;
222
221
}
223
222
schema:: TypeDefinition :: Union ( union) => {
224
- schema. unions . insert ( union. name , GqlUnion ( union. types ) ) ;
223
+ let tys: BTreeSet < String > = union. types . into_iter ( ) . collect ( ) ;
224
+ schema. unions . insert ( union. name , GqlUnion ( tys) ) ;
225
225
}
226
226
schema:: TypeDefinition :: Interface ( interface) => {
227
227
schema. interfaces . insert (
@@ -263,7 +263,11 @@ impl ::std::convert::From<::introspection_response::IntrospectionResponse> for S
263
263
use introspection_response:: __TypeKind;
264
264
265
265
let mut schema = Schema :: new ( ) ;
266
- let root = src. data . schema . expect ( "__Schema is not null" ) ;
266
+ let root = src. schema . expect ( "__Schema is not null" ) ;
267
+
268
+ // Holds which objects implement which interfaces so we can populate GqlInterface#implemented_by later.
269
+ // It maps interface names to a vec of implementation names.
270
+ let mut interface_implementations: BTreeMap < String , Vec < String > > = BTreeMap :: new ( ) ;
267
271
268
272
for ty in root
269
273
. types
@@ -288,10 +292,12 @@ impl ::std::convert::From<::introspection_response::IntrospectionResponse> for S
288
292
. insert ( name. clone ( ) , GqlEnum { name, variants } ) ;
289
293
}
290
294
Some ( __TypeKind:: SCALAR ) => {
291
- schema. scalars . insert ( name) ;
295
+ if DEFAULT_SCALARS . iter ( ) . find ( |s| s == & & name. as_str ( ) ) . is_none ( ) {
296
+ schema. scalars . insert ( name) ;
297
+ }
292
298
}
293
299
Some ( __TypeKind:: UNION ) => {
294
- let variants: Vec < String > = ty
300
+ let variants: BTreeSet < String > = ty
295
301
. possible_types
296
302
. clone ( )
297
303
. unwrap ( )
@@ -301,25 +307,57 @@ impl ::std::convert::From<::introspection_response::IntrospectionResponse> for S
301
307
schema. unions . insert ( name. clone ( ) , GqlUnion ( variants) ) ;
302
308
}
303
309
Some ( __TypeKind:: OBJECT ) => {
310
+ for implementing in ty
311
+ . interfaces
312
+ . clone ( )
313
+ . unwrap_or_else ( || Vec :: new ( ) )
314
+ . into_iter ( )
315
+ . filter_map ( |t| t)
316
+ . map ( |t| t. type_ref . name )
317
+ {
318
+ interface_implementations
319
+ . entry ( implementing. expect ( "interface name" ) )
320
+ . and_modify ( |objects| objects. push ( name. clone ( ) ) )
321
+ . or_insert_with ( || vec ! [ name. clone( ) ] ) ;
322
+ }
323
+
304
324
let fields: Vec < GqlObjectField > = ty
305
325
. fields
306
326
. clone ( )
307
327
. unwrap ( )
308
328
. into_iter ( )
309
- . filter_map ( |t| t. map ( |t| GqlObjectField {
310
- name : t. name . expect ( "field name" ) ,
311
- type_ : FieldType :: from ( t. type_ . expect ( "field type" ) )
312
- } ) )
329
+ . filter_map ( |t| {
330
+ t. map ( |t| GqlObjectField {
331
+ name : t. name . expect ( "field name" ) ,
332
+ type_ : FieldType :: from ( t. type_ . expect ( "field type" ) ) ,
333
+ } )
334
+ } )
313
335
. collect ( ) ;
314
- schema. objects . insert ( name. clone ( ) , GqlObject {
315
- name,
316
- fields,
317
- } ) ;
336
+ schema
337
+ . objects
338
+ . insert ( name. clone ( ) , GqlObject { name, fields } ) ;
318
339
}
319
340
Some ( __TypeKind:: INTERFACE ) => {
320
- } ,
341
+ let iface = GqlInterface {
342
+ name : name. clone ( ) ,
343
+ implemented_by : Vec :: new ( ) ,
344
+ fields : ty
345
+ . fields
346
+ . clone ( )
347
+ . expect ( "interface fields" )
348
+ . into_iter ( )
349
+ . filter_map ( |f| f)
350
+ . map ( |f| GqlObjectField {
351
+ name : f. name . expect ( "field name" ) ,
352
+ type_ : FieldType :: from ( f. type_ . expect ( "field type" ) ) ,
353
+ } )
354
+ . collect ( ) ,
355
+ } ;
356
+ schema. interfaces . insert ( name, iface) ;
357
+ }
321
358
Some ( __TypeKind:: INPUT_OBJECT ) => {
322
- } ,
359
+ schema. inputs . insert ( name, GqlInput ) ;
360
+ }
323
361
_ => unimplemented ! ( "unimplemented definition" ) ,
324
362
}
325
363
}
0 commit comments