1- import _ from "lodash" ;
2- import * as R from "ramda" ;
1+ import _ , { map , shuffle } from "lodash" ;
32
43import {
54 GraphQLNamedType ,
@@ -16,6 +15,16 @@ import {
1615 isInterfaceType ,
1716 isScalarType ,
1817 SingleFieldSubscriptionsRule ,
18+ GraphQLEnumType ,
19+ GraphQLInputObjectType ,
20+ GraphQLInputType ,
21+ GraphQLInterfaceType ,
22+ GraphQLList ,
23+ GraphQLNonNull ,
24+ GraphQLObjectType ,
25+ GraphQLOutputType ,
26+ GraphQLScalarType ,
27+ GraphQLUnionType ,
1928} from "graphql" ;
2029import {
2130 SimplifiedIntrospection ,
@@ -26,7 +35,31 @@ import {
2635} from "./types.js" ;
2736import { typeNameToId } from "./utils.js" ;
2837
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+ ) {
3063 let unwrappedType = type ;
3164 const typeWrappers = [ ] ;
3265
@@ -214,46 +247,52 @@ function addParent(schema: SimplifiedIntrospection) {
214247 if ( type . type === "[Circular]" ) return [ type ] ;
215248
216249 // 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 [ ] ;
218251 if (
219252 type . type &&
220253 type . type . kind &&
221- R . includes ( type . type . kind , [ "SCALAR" , "ENUM" ] )
254+ _ . includes ( [ "SCALAR" , "ENUM" ] , type . type . kind )
222255 )
223256 return [ ] ;
224257
225- return R . chain ( ( currentType ) => {
258+ return _ . flatMap ( _ . values ( type . fields ) , ( currentType ) => {
226259 // if it's a composite object, use recursion to introspect the inner object
227260 const innerTypes = currentType . type . fields
228- ? R . chain ( ( subType ) => extractFields ( subType ) , currentType . type . fields )
261+ ? _ . flatMap ( currentType . type . fields , ( subType ) =>
262+ extractFields ( subType )
263+ )
229264 : [ currentType ] ;
230265 // 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+ } ) ;
233268 }
234269
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 )
238272 ) ;
239273
240274 /*
241275 * Group fields by their corresponding type id
242276 */
277+ const groupedFieldsByType = _ . groupBy ( allFields , function ( field ) {
278+ return field . type . id ;
279+ } ) ;
243280
281+ /*
282+ * Extract id and prepare the mapping
283+ */
284+ const mappings = _ . mapValues ( groupedFieldsByType , function ( t ) {
285+ return _ . map ( t , ( field ) => field . id ) ;
286+ } ) ;
244287
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 ) => {
253292 if ( mappings [ type ] ) {
254293 ( schema . types [ type ] as any ) . parents = mappings [ type ] ;
255294 }
256- } , R . keys ( schema . types ) ) ;
295+ } ) ;
257296}
258297
259298export function getSchema ( schema : GraphQLSchema ) {
0 commit comments