From e8816bfa67b79d30bf9dc0cfcec07aa70ca25df3 Mon Sep 17 00:00:00 2001 From: stefanprobst Date: Mon, 25 Mar 2019 10:02:57 +0100 Subject: [PATCH] fix(schema): Preserve resolvers on ___NODE fields (#12806) --- .../src/schema/infer/__tests__/merge-types.js | 42 +++++++++++++++++++ .../src/schema/infer/add-inferred-fields.js | 18 ++++---- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/packages/gatsby/src/schema/infer/__tests__/merge-types.js b/packages/gatsby/src/schema/infer/__tests__/merge-types.js index f6a9e6ec5695b..bef4a151e3c7c 100644 --- a/packages/gatsby/src/schema/infer/__tests__/merge-types.js +++ b/packages/gatsby/src/schema/infer/__tests__/merge-types.js @@ -35,6 +35,12 @@ const nodes = [ }, ], }, + { + id: `id2`, + internal: { type: `LinkTest` }, + link___NODE: `id1`, + links___NODE: [`id1`], + }, ] describe(`merges explicit and inferred type definitions`, () => { @@ -276,4 +282,40 @@ describe(`merges explicit and inferred type definitions`, () => { // Date resolvers expect(fields.explicitDate.resolve).toBeUndefined() }) + + it(`preserves foreign-key resolvers on ___NODE fields when noDefaultResolvers: false`, async () => { + const typeDefs = ` + type LinkTest implements Node { + link: Test! + links: [Test!]! + } + ` + store.dispatch({ type: `CREATE_TYPES`, payload: typeDefs }) + + await build({}) + const { schema } = store.getState() + const { link, links } = schema.getType(`LinkTest`).getFields() + expect(link.type.toString()).toBe(`Test!`) + expect(links.type.toString()).toBe(`[Test!]!`) + expect(link.resolve).toBeDefined() + expect(links.resolve).toBeDefined() + }) + + it(`ignores foreign-key resolvers on ___NODE fields when noDefaultResolvers: true`, async () => { + const typeDefs = ` + type LinkTest implements Node @infer(noDefaultResolvers: true) { + link: Test! + links: [Test!]! + } + ` + store.dispatch({ type: `CREATE_TYPES`, payload: typeDefs }) + + await build({}) + const { schema } = store.getState() + const { link, links } = schema.getType(`LinkTest`).getFields() + expect(link.type.toString()).toBe(`Test!`) + expect(links.type.toString()).toBe(`[Test!]!`) + expect(link.resolve).toBeUndefined() + expect(links.resolve).toBeUndefined() + }) }) diff --git a/packages/gatsby/src/schema/infer/add-inferred-fields.js b/packages/gatsby/src/schema/infer/add-inferred-fields.js index 1beb8153050d2..35079a5c17a2c 100644 --- a/packages/gatsby/src/schema/infer/add-inferred-fields.js +++ b/packages/gatsby/src/schema/infer/add-inferred-fields.js @@ -102,25 +102,27 @@ const addInferredFieldsImpl = ({ let lists = 0 let namedFieldType = fieldType while (namedFieldType.ofType) { - namedFieldType = namedFieldType.ofType if (namedFieldType instanceof GraphQLList) { lists++ } + namedFieldType = namedFieldType.ofType } - if (arrays === lists) { + const namedInferredTypeName = + typeof namedInferredType === `string` + ? namedInferredType + : namedInferredType.getTypeName() + + if (arrays === lists && namedFieldType.name === namedInferredTypeName) { if ( namedFieldType instanceof GraphQLObjectType && - typeof namedInferredType !== `string` && - namedFieldType.name === namedInferredType.getTypeName() + namedInferredType instanceof ObjectTypeComposer ) { const fieldTypeComposer = typeComposer.getFieldTC(key) const inferredFields = namedInferredType.getFields() fieldTypeComposer.addFields(inferredFields) - } else if ( - addDefaultResolvers && - namedFieldType.name === namedInferredType - ) { + } + if (addDefaultResolvers) { let field = typeComposer.getField(key) if (!field.type) { field = {