@@ -137,8 +137,8 @@ function simplifySchema(schema: GraphQLSchema): SimplifiedIntrospection {
137137 return {
138138 types : mapValues ( schema . getTypeMap ( ) , ( type ) => convertType ( schema , type ) ) ,
139139 queryType : schema . getQueryType ( ) . name ,
140- mutationType : schema . getMutationType ( ) . name ?? null ,
141- subscriptionType : schema . getSubscriptionType ( ) . name ?? null ,
140+ mutationType : schema . getMutationType ( ) ? .name ?? null ,
141+ subscriptionType : schema . getSubscriptionType ( ) ? .name ?? null ,
142142 //FIXME:
143143 //directives:
144144 } ;
@@ -209,13 +209,63 @@ function assignTypesAndIDs(schema: SimplifiedIntrospection) {
209209}
210210
211211function addParent ( schema : SimplifiedIntrospection ) {
212- return schema ;
212+ function extractFields ( type ) {
213+ // no need to inspect circular types any further
214+ if ( type . type === "[Circular]" ) return [ type ] ;
215+
216+ // same with scalars and enums
217+ if ( _ . includes ( [ "SCALAR" , "ENUM" ] , type . kind && type . kind ) ) return [ ] ;
218+ if (
219+ type . type &&
220+ type . type . kind &&
221+ _ . includes ( [ "SCALAR" , "ENUM" ] , type . type . kind )
222+ )
223+ return [ ] ;
224+
225+ return _ . flatMap ( _ . values ( type . fields ) , ( currentType ) => {
226+ // if it's a composite object, use recursion to introspect the inner object
227+ const innerTypes = currentType . type . fields
228+ ? _ . flatMap ( currentType . type . fields , ( subType ) =>
229+ extractFields ( subType )
230+ )
231+ : [ currentType ] ;
232+ // dedupe types by their id
233+ return _ . uniqBy ( _ . concat ( innerTypes , currentType ) , ( x ) => x . id ) ;
234+ } ) ;
235+ }
236+
237+ const allFields = _ . flatMap ( _ . values ( schema . types ) , ( type ) =>
238+ extractFields ( type )
239+ ) ;
240+
241+ /*
242+ * Group fields by their corresponding type id
243+ */
244+ const groupedFieldsByType = _ . groupBy ( allFields , function ( field ) {
245+ return field . type . id ;
246+ } ) ;
247+
248+ /*
249+ * Extract id and prepare the mapping
250+ */
251+ const mappings = _ . mapValues ( groupedFieldsByType , function ( t ) {
252+ return _ . map ( t , ( field ) => field . id ) ;
253+ } ) ;
254+
255+ /*
256+ * Assign the mapping
257+ */
258+ _ . each ( Object . keys ( schema . types ) , ( type ) => {
259+ if ( mappings [ type ] ) {
260+ ( schema . types [ type ] as any ) . parents = mappings [ type ] ;
261+ }
262+ } ) ;
213263}
214264
215265export function getSchema ( schema : GraphQLSchema ) {
216266 const simpleSchema = simplifySchema ( schema ) ;
217267 assignTypesAndIDs ( simpleSchema ) ;
218- addParent ( simpleSchema ) ;
268+ // addParent(simpleSchema);
219269
220270 // Force cast to the correct type
221271 // FIXME: This is not the right way of doing it
0 commit comments