1
- import _ from "lodash" ;
2
- import * as R from "ramda" ;
1
+ import _ , { map , shuffle } from "lodash" ;
3
2
4
3
import {
5
4
GraphQLNamedType ,
@@ -16,6 +15,16 @@ import {
16
15
isInterfaceType ,
17
16
isScalarType ,
18
17
SingleFieldSubscriptionsRule ,
18
+ GraphQLEnumType ,
19
+ GraphQLInputObjectType ,
20
+ GraphQLInputType ,
21
+ GraphQLInterfaceType ,
22
+ GraphQLList ,
23
+ GraphQLNonNull ,
24
+ GraphQLObjectType ,
25
+ GraphQLOutputType ,
26
+ GraphQLScalarType ,
27
+ GraphQLUnionType ,
19
28
} from "graphql" ;
20
29
import {
21
30
SimplifiedIntrospection ,
@@ -26,7 +35,31 @@ import {
26
35
} from "./types.js" ;
27
36
import { typeNameToId } from "./utils.js" ;
28
37
29
- 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
+ ) {
30
63
let unwrappedType = type ;
31
64
const typeWrappers = [ ] ;
32
65
@@ -214,46 +247,52 @@ function addParent(schema: SimplifiedIntrospection) {
214
247
if ( type . type === "[Circular]" ) return [ type ] ;
215
248
216
249
// same with scalars and enums
217
- if ( R . includes ( type . kind && type . kind , [ "SCALAR" , "ENUM" ] ) ) return [ ] ;
250
+ if ( _ . includes ( [ "SCALAR" , "ENUM" ] , type . kind && type . kind ) ) return [ ] ;
218
251
if (
219
252
type . type &&
220
253
type . type . kind &&
221
- R . includes ( type . type . kind , [ "SCALAR" , "ENUM" ] )
254
+ _ . includes ( [ "SCALAR" , "ENUM" ] , type . type . kind )
222
255
)
223
256
return [ ] ;
224
257
225
- return R . chain ( ( currentType ) => {
258
+ return _ . flatMap ( _ . values ( type . fields ) , ( currentType ) => {
226
259
// if it's a composite object, use recursion to introspect the inner object
227
260
const innerTypes = currentType . type . fields
228
- ? R . chain ( ( subType ) => extractFields ( subType ) , currentType . type . fields )
261
+ ? _ . flatMap ( currentType . type . fields , ( subType ) =>
262
+ extractFields ( subType )
263
+ )
229
264
: [ currentType ] ;
230
265
// dedupe types by their id
231
- return R . uniqBy ( ( x ) => x . id , R . append ( currentType , innerTypes ) ) ;
232
- } , R . values ( type . fields ) ) ;
266
+ return _ . uniqBy ( _ . concat ( innerTypes , currentType ) , ( x ) => x . id ) ;
267
+ } ) ;
233
268
}
234
269
235
- const allFields = R . chain (
236
- ( type ) => extractFields ( type ) ,
237
- R . values ( schema . types )
270
+ const allFields = _ . flatMap ( _ . values ( schema . types ) , ( type ) =>
271
+ extractFields ( type )
238
272
) ;
239
273
240
274
/*
241
275
* Group fields by their corresponding type id
242
276
*/
277
+ const groupedFieldsByType = _ . groupBy ( allFields , function ( field ) {
278
+ return field . type . id ;
279
+ } ) ;
243
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
+ } ) ;
244
287
245
- const mappings = R . map (
246
- x => R . map ( y => y . id , x ) ,
247
- R . groupBy ( function ( field ) {
248
- return field . type . id ;
249
- } , allFields )
250
- ) ;
251
-
252
- R . forEach ( type => {
288
+ /*
289
+ * Assign the mapping
290
+ */
291
+ _ . each ( Object . keys ( schema . types ) , ( type ) => {
253
292
if ( mappings [ type ] ) {
254
293
( schema . types [ type ] as any ) . parents = mappings [ type ] ;
255
294
}
256
- } , R . keys ( schema . types ) ) ;
295
+ } ) ;
257
296
}
258
297
259
298
export function getSchema ( schema : GraphQLSchema ) {
0 commit comments