diff --git a/src/GraphQL/ParseGraphQLSchema.js b/src/GraphQL/ParseGraphQLSchema.js index ce97ae6c66..03212098b6 100644 --- a/src/GraphQL/ParseGraphQLSchema.js +++ b/src/GraphQL/ParseGraphQLSchema.js @@ -200,6 +200,41 @@ class ParseGraphQLSchema { if (typeof this.graphQLCustomTypeDefs.getTypeMap === 'function') { const customGraphQLSchemaTypeMap = this.graphQLCustomTypeDefs.getTypeMap(); + const findAndReplaceLastType = (parent, key) => { + if (parent[key].name) { + if ( + this.graphQLAutoSchema.getType(parent[key].name) && + this.graphQLAutoSchema.getType(parent[key].name) !== parent[key] + ) { + // To avoid unresolved field on overloaded schema + // replace the final type with the auto schema one + parent[key] = this.graphQLAutoSchema.getType(parent[key].name); + } + } else { + if (parent[key].ofType) { + findAndReplaceLastType(parent[key], 'ofType'); + } + } + }; + Object.values(customGraphQLSchemaTypeMap).forEach( + (customGraphQLSchemaType) => { + if ( + !customGraphQLSchemaType || + !customGraphQLSchemaType.name || + customGraphQLSchemaType.name.startsWith('__') + ) { + return; + } + const autoGraphQLSchemaType = this.graphQLAutoSchema.getType( + customGraphQLSchemaType.name + ); + if (!autoGraphQLSchemaType) { + this.graphQLAutoSchema._typeMap[ + customGraphQLSchemaType.name + ] = customGraphQLSchemaType; + } + } + ); Object.values(customGraphQLSchemaTypeMap).forEach( (customGraphQLSchemaType) => { if ( @@ -212,30 +247,11 @@ class ParseGraphQLSchema { const autoGraphQLSchemaType = this.graphQLAutoSchema.getType( customGraphQLSchemaType.name ); + if ( autoGraphQLSchemaType && typeof customGraphQLSchemaType.getFields === 'function' ) { - const findAndReplaceLastType = (parent, key) => { - if (parent[key].name) { - if ( - this.graphQLAutoSchema.getType(parent[key].name) && - this.graphQLAutoSchema.getType(parent[key].name) !== - parent[key] - ) { - // To avoid unresolved field on overloaded schema - // replace the final type with the auto schema one - parent[key] = this.graphQLAutoSchema.getType( - parent[key].name - ); - } - } else { - if (parent[key].ofType) { - findAndReplaceLastType(parent[key], 'ofType'); - } - } - }; - Object.values(customGraphQLSchemaType.getFields()).forEach( (field) => { findAndReplaceLastType(field, 'type'); @@ -245,10 +261,6 @@ class ParseGraphQLSchema { ...autoGraphQLSchemaType.getFields(), ...customGraphQLSchemaType.getFields(), }; - } else { - this.graphQLAutoSchema._typeMap[ - customGraphQLSchemaType.name - ] = customGraphQLSchemaType; } } );