1
- import _ from "lodash" ;
1
+ import _ , { map , shuffle } from "lodash" ;
2
2
3
3
import {
4
4
GraphQLNamedType ,
@@ -15,6 +15,16 @@ import {
15
15
isInterfaceType ,
16
16
isScalarType ,
17
17
SingleFieldSubscriptionsRule ,
18
+ GraphQLEnumType ,
19
+ GraphQLInputObjectType ,
20
+ GraphQLInputType ,
21
+ GraphQLInterfaceType ,
22
+ GraphQLList ,
23
+ GraphQLNonNull ,
24
+ GraphQLObjectType ,
25
+ GraphQLOutputType ,
26
+ GraphQLScalarType ,
27
+ GraphQLUnionType ,
18
28
} from "graphql" ;
19
29
import {
20
30
SimplifiedIntrospection ,
@@ -25,7 +35,31 @@ import {
25
35
} from "./types.js" ;
26
36
import { typeNameToId } from "./utils.js" ;
27
37
28
- function unwrapType ( type ) {
38
+ function unwrapType (
39
+ type :
40
+ | GraphQLScalarType < unknown , unknown >
41
+ | GraphQLEnumType
42
+ | GraphQLInputObjectType
43
+ | GraphQLList < GraphQLInputType >
44
+ | GraphQLNonNull <
45
+ | GraphQLEnumType
46
+ | GraphQLInputObjectType
47
+ | GraphQLScalarType < unknown , unknown >
48
+ | GraphQLList < GraphQLInputType >
49
+ >
50
+ | GraphQLInterfaceType
51
+ | GraphQLUnionType
52
+ | GraphQLObjectType < any , any >
53
+ | GraphQLList < GraphQLOutputType >
54
+ | GraphQLNonNull <
55
+ | GraphQLEnumType
56
+ | GraphQLInterfaceType
57
+ | GraphQLUnionType
58
+ | GraphQLScalarType < unknown , unknown >
59
+ | GraphQLObjectType < any , any >
60
+ | GraphQLList < GraphQLOutputType >
61
+ >
62
+ ) {
29
63
let unwrappedType = type ;
30
64
const typeWrappers = [ ] ;
31
65
@@ -208,7 +242,57 @@ function assignTypesAndIDs(schema: SimplifiedIntrospection) {
208
242
}
209
243
210
244
function addParent ( schema : SimplifiedIntrospection ) {
211
- return schema ;
245
+ function extractFields ( type ) {
246
+ // no need to inspect circular types any further
247
+ if ( type . type === "[Circular]" ) return [ type ] ;
248
+
249
+ // same with scalars and enums
250
+ if ( _ . includes ( [ "SCALAR" , "ENUM" ] , type . kind && type . kind ) ) return [ ] ;
251
+ if (
252
+ type . type &&
253
+ type . type . kind &&
254
+ _ . includes ( [ "SCALAR" , "ENUM" ] , type . type . kind )
255
+ )
256
+ return [ ] ;
257
+
258
+ return _ . flatMap ( _ . values ( type . fields ) , ( currentType ) => {
259
+ // if it's a composite object, use recursion to introspect the inner object
260
+ const innerTypes = currentType . type . fields
261
+ ? _ . flatMap ( currentType . type . fields , ( subType ) =>
262
+ extractFields ( subType )
263
+ )
264
+ : [ currentType ] ;
265
+ // dedupe types by their id
266
+ return _ . uniqBy ( _ . concat ( innerTypes , currentType ) , ( x ) => x . id ) ;
267
+ } ) ;
268
+ }
269
+
270
+ const allFields = _ . flatMap ( _ . values ( schema . types ) , ( type ) =>
271
+ extractFields ( type )
272
+ ) ;
273
+
274
+ /*
275
+ * Group fields by their corresponding type id
276
+ */
277
+ const groupedFieldsByType = _ . groupBy ( allFields , function ( field ) {
278
+ return field . type . id ;
279
+ } ) ;
280
+
281
+ /*
282
+ * Extract id and prepare the mapping
283
+ */
284
+ const mappings = _ . mapValues ( groupedFieldsByType , function ( t ) {
285
+ return _ . map ( t , ( field ) => field . id ) ;
286
+ } ) ;
287
+
288
+ /*
289
+ * Assign the mapping
290
+ */
291
+ _ . each ( Object . keys ( schema . types ) , ( type ) => {
292
+ if ( mappings [ type ] ) {
293
+ ( schema . types [ type ] as any ) . parents = mappings [ type ] ;
294
+ }
295
+ } ) ;
212
296
}
213
297
214
298
export function getSchema ( schema : GraphQLSchema ) {
0 commit comments