From c793927e68b86504a6a7128ee8152fd31c7e68ee Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Fri, 12 Oct 2018 10:31:13 +0200 Subject: [PATCH 01/15] Simplify union type field name Field names are not necessary, since createTypeName is used --- packages/gatsby/src/schema/infer-graphql-type.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/gatsby/src/schema/infer-graphql-type.js b/packages/gatsby/src/schema/infer-graphql-type.js index fc159bb73b922..650b501f1e3e1 100644 --- a/packages/gatsby/src/schema/infer-graphql-type.js +++ b/packages/gatsby/src/schema/infer-graphql-type.js @@ -257,12 +257,7 @@ function inferFromFieldName(value, selector, types): GraphQLFieldConfig<*, *> { // If there's more than one type, we'll create a union type. if (fields.length > 1) { type = new GraphQLUnionType({ - name: createTypeName( - `Union_${key}_${fields - .map(f => f.name) - .sort() - .join(`__`)}` - ), + name: createTypeName(`Union_${key}`), description: `Union interface for the field "${key}" for types [${fields .map(f => f.name) .sort() From 0fede76e808d3df83f915a2dd976f637d899f014 Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Fri, 12 Oct 2018 10:38:53 +0200 Subject: [PATCH 02/15] Share union type instances when subfield types equal --- .../gatsby/src/schema/infer-graphql-type.js | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/gatsby/src/schema/infer-graphql-type.js b/packages/gatsby/src/schema/infer-graphql-type.js index 650b501f1e3e1..7ffd9ee8b55c1 100644 --- a/packages/gatsby/src/schema/infer-graphql-type.js +++ b/packages/gatsby/src/schema/infer-graphql-type.js @@ -24,6 +24,7 @@ const { const DateType = require(`./types/type-date`) const FileType = require(`./types/type-file`) const is32BitInteger = require(`../utils/is-32-bit-integer`) +const unionTypes = [] import type { GraphQLOutputType } from "graphql" import type { @@ -256,16 +257,23 @@ function inferFromFieldName(value, selector, types): GraphQLFieldConfig<*, *> { let type // If there's more than one type, we'll create a union type. if (fields.length > 1) { - type = new GraphQLUnionType({ - name: createTypeName(`Union_${key}`), - description: `Union interface for the field "${key}" for types [${fields - .map(f => f.name) - .sort() - .join(`, `)}]`, - types: fields.map(f => f.nodeObjectType), - resolveType: data => - fields.find(f => f.name == data.internal.type).nodeObjectType, - }) + type = unionTypes.find(type => + _.isEqual(type.getTypes(), fields.map(f => f.nodeObjectType)) + ) + + if (!type) { + type = new GraphQLUnionType({ + name: createTypeName(`Union_${key}`), + description: `Union interface for the field "${key}" for types [${fields + .map(f => f.name) + .sort() + .join(`, `)}]`, + types: fields.map(f => f.nodeObjectType), + resolveType: data => + fields.find(f => f.name == data.internal.type).nodeObjectType, + }) + unionTypes.push(type) + } } else { type = fields[0].nodeObjectType } From f54e0562e56ed2684fae93a6ab6ac901a0298e9d Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Fri, 12 Oct 2018 13:37:49 +0200 Subject: [PATCH 03/15] Share union type instance on a per-key basis --- packages/gatsby/src/schema/infer-graphql-type.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/gatsby/src/schema/infer-graphql-type.js b/packages/gatsby/src/schema/infer-graphql-type.js index 7ffd9ee8b55c1..af878cfa6fe6d 100644 --- a/packages/gatsby/src/schema/infer-graphql-type.js +++ b/packages/gatsby/src/schema/infer-graphql-type.js @@ -24,7 +24,7 @@ const { const DateType = require(`./types/type-date`) const FileType = require(`./types/type-file`) const is32BitInteger = require(`../utils/is-32-bit-integer`) -const unionTypes = [] +const unionTypes = {} import type { GraphQLOutputType } from "graphql" import type { @@ -257,13 +257,17 @@ function inferFromFieldName(value, selector, types): GraphQLFieldConfig<*, *> { let type // If there's more than one type, we'll create a union type. if (fields.length > 1) { - type = unionTypes.find(type => - _.isEqual(type.getTypes(), fields.map(f => f.nodeObjectType)) - ) + const typeName = `Union_${key}` + + if (unionTypes[typeName]) { + type = unionTypes[typeName].find(type => + _.isEqual(type.getTypes(), fields.map(f => f.nodeObjectType)) + ) + } if (!type) { type = new GraphQLUnionType({ - name: createTypeName(`Union_${key}`), + name: createTypeName(typeName), description: `Union interface for the field "${key}" for types [${fields .map(f => f.name) .sort() @@ -272,7 +276,7 @@ function inferFromFieldName(value, selector, types): GraphQLFieldConfig<*, *> { resolveType: data => fields.find(f => f.name == data.internal.type).nodeObjectType, }) - unionTypes.push(type) + ;(unionTypes[typeName] = unionTypes[typeName] || []).push(type) } } else { type = fields[0].nodeObjectType From 5fae19820385444dfc35cb1bf03fdbed98b53ced Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Tue, 16 Oct 2018 10:05:38 +0200 Subject: [PATCH 04/15] Clear union types when updating schema --- packages/gatsby/src/schema/index.js | 2 ++ .../gatsby/src/schema/infer-graphql-type.js | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/gatsby/src/schema/index.js b/packages/gatsby/src/schema/index.js index 9911434a50b97..0e74d0454de69 100644 --- a/packages/gatsby/src/schema/index.js +++ b/packages/gatsby/src/schema/index.js @@ -7,8 +7,10 @@ const buildNodeTypes = require(`./build-node-types`) const buildNodeConnections = require(`./build-node-connections`) const { store } = require(`../redux`) const invariant = require(`invariant`) +const { clearUnionTypes } = require('./infer-graphql-type') module.exports = async ({ parentSpan }) => { + clearUnionTypes() const typesGQL = await buildNodeTypes({ parentSpan }) const connections = buildNodeConnections(_.values(typesGQL)) diff --git a/packages/gatsby/src/schema/infer-graphql-type.js b/packages/gatsby/src/schema/infer-graphql-type.js index af878cfa6fe6d..87e30304c66c9 100644 --- a/packages/gatsby/src/schema/infer-graphql-type.js +++ b/packages/gatsby/src/schema/infer-graphql-type.js @@ -24,7 +24,7 @@ const { const DateType = require(`./types/type-date`) const FileType = require(`./types/type-file`) const is32BitInteger = require(`../utils/is-32-bit-integer`) -const unionTypes = {} +const unionTypes = new Map() import type { GraphQLOutputType } from "graphql" import type { @@ -257,17 +257,15 @@ function inferFromFieldName(value, selector, types): GraphQLFieldConfig<*, *> { let type // If there's more than one type, we'll create a union type. if (fields.length > 1) { - const typeName = `Union_${key}` + const typeName = `Union_${key}_${fields.map(f => f.name).sort().join(`__`)}` - if (unionTypes[typeName]) { - type = unionTypes[typeName].find(type => - _.isEqual(type.getTypes(), fields.map(f => f.nodeObjectType)) - ) + if (unionTypes.has(typeName)) { + type = unionTypes.get(typeName) } if (!type) { type = new GraphQLUnionType({ - name: createTypeName(typeName), + name: createTypeName(`Union_${key}`), description: `Union interface for the field "${key}" for types [${fields .map(f => f.name) .sort() @@ -276,7 +274,7 @@ function inferFromFieldName(value, selector, types): GraphQLFieldConfig<*, *> { resolveType: data => fields.find(f => f.name == data.internal.type).nodeObjectType, }) - ;(unionTypes[typeName] = unionTypes[typeName] || []).push(type) + unionTypes.set(typeName, type) } } else { type = fields[0].nodeObjectType @@ -428,3 +426,7 @@ function _inferObjectStructureFromNodes( export function inferObjectStructureFromNodes(options: inferTypeOptions) { return _inferObjectStructureFromNodes(options, null) } + +export function clearUnionTypes() { + unionTypes.clear() +} From f597b38b395b201afeab64b3951982652462c8b0 Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Tue, 16 Oct 2018 10:53:07 +0200 Subject: [PATCH 05/15] Use backticks for strings --- packages/gatsby/src/schema/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/schema/index.js b/packages/gatsby/src/schema/index.js index 0e74d0454de69..c2dff9b04f086 100644 --- a/packages/gatsby/src/schema/index.js +++ b/packages/gatsby/src/schema/index.js @@ -7,7 +7,7 @@ const buildNodeTypes = require(`./build-node-types`) const buildNodeConnections = require(`./build-node-connections`) const { store } = require(`../redux`) const invariant = require(`invariant`) -const { clearUnionTypes } = require('./infer-graphql-type') +const { clearUnionTypes } = require(`./infer-graphql-type`) module.exports = async ({ parentSpan }) => { clearUnionTypes() From 62d3926846b85de639987dbac583a964c25ee71b Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Thu, 18 Oct 2018 14:27:46 +0200 Subject: [PATCH 06/15] add basic test for reusing union type --- .../__tests__/infer-graphql-type-test.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index 07f354c74b97e..d52c4ba9059c8 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -724,6 +724,27 @@ describe(`GraphQL type inferance`, () => { payload: { id: `baz`, internal: { type: `Bar` } }, }) }) + + it(`Uses same union type for same key`, () => { + const fields = inferObjectStructureFromNodes({ + nodes: [ + { + test___NODE: [`pet_1`, `child_1`], + }, + ], + types, + }) + + const fields2 = inferObjectStructureFromNodes({ + nodes: [ + { + test___NODE: [`pet_1`, `child_1`], + }, + ], + types, + }) + expect(fields.test.type).toEqual(fields2.test.type) + }) }) it(`Infers graphql type from array of nodes`, () => From d7f4a6767165cb74ce12a6f169ebb28a7ea3dddb Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Thu, 18 Oct 2018 16:58:53 +0200 Subject: [PATCH 07/15] Update test case description and use consistent node notation throughout file --- .../schema/__tests__/infer-graphql-type-test.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index d52c4ba9059c8..2622a07469cfe 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -725,22 +725,13 @@ describe(`GraphQL type inferance`, () => { }) }) - it(`Uses same union type for same key`, () => { + it(`Uses same union type for same child node types and key`, () => { const fields = inferObjectStructureFromNodes({ - nodes: [ - { - test___NODE: [`pet_1`, `child_1`], - }, - ], + nodes: [{ test___NODE: [`pet_1`, `child_1`] } ], types, }) - const fields2 = inferObjectStructureFromNodes({ - nodes: [ - { - test___NODE: [`pet_1`, `child_1`], - }, - ], + nodes: [{ test___NODE: [`pet_1`, `child_2`] }], types, }) expect(fields.test.type).toEqual(fields2.test.type) From 0cd1716fec01c92b5d94addc61703dba95c9e933 Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Thu, 18 Oct 2018 17:01:27 +0200 Subject: [PATCH 08/15] Add test case for different keys (same child node types) --- .../src/schema/__tests__/infer-graphql-type-test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index 2622a07469cfe..b4e79b74e58d8 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -736,6 +736,19 @@ describe(`GraphQL type inferance`, () => { }) expect(fields.test.type).toEqual(fields2.test.type) }) + + + it(`Uses a different type for the same child node types but a different key`, () => { + const fields = inferObjectStructureFromNodes({ + nodes: [{ test___NODE: [`pet_1`, `child_1`] }], + types, + }) + const fields2 = inferObjectStructureFromNodes({ + nodes: [{ differentKey___NODE: [`pet_1`, `child_2`] }], + types, + }) + expect(fields.test.type).not.toEqual(fields2.differentKey.type) + }) }) it(`Infers graphql type from array of nodes`, () => From 135a789eea115aacefe49db73335e4ec6b206fdf Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Thu, 18 Oct 2018 17:04:15 +0200 Subject: [PATCH 09/15] Add test case for different child node types (but the same keys) --- .../schema/__tests__/infer-graphql-type-test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index b4e79b74e58d8..309e167372362 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -749,6 +749,22 @@ describe(`GraphQL type inferance`, () => { }) expect(fields.test.type).not.toEqual(fields2.differentKey.type) }) + + it(`Uses a different type for the different child node types but the same key`, () => { + store.dispatch({ + type: `CREATE_NODE`, + payload: { id: `toy_1`, internal: { type: `Toy` } }, + }) + const fields = inferObjectStructureFromNodes({ + nodes: [{ test___NODE: [`pet_1`, `child_1`] } ], + types, + }) + const fields2 = inferObjectStructureFromNodes({ + nodes: [{ test___NODE: [`pet_1`, `child_1`, `toy_1`] }], + types: types.concat([{ name: `Toy` }]), + }) + expect(fields.test.type).not.toEqual(fields2.test.type) + }) }) it(`Infers graphql type from array of nodes`, () => From e2f6e6b033acf1be72d8890f108bcc3bb3d6a118 Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Thu, 18 Oct 2018 19:19:49 +0200 Subject: [PATCH 10/15] Add test case for clearing union types --- .../src/schema/__tests__/infer-graphql-type-test.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index 309e167372362..c23cb8a4ad8a3 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -8,7 +8,7 @@ const path = require(`path`) const normalizePath = require(`normalize-path`) const { clearTypeExampleValues } = require(`../data-tree-utils`) const { typeConflictReporter } = require(`../type-conflict-reporter`) -const { inferObjectStructureFromNodes } = require(`../infer-graphql-type`) +const { inferObjectStructureFromNodes, clearUnionTypes } = require(`../infer-graphql-type`) function queryResult(nodes, fragment, { types = [], ignoreFields } = {}) { const schema = new GraphQLSchema({ @@ -765,6 +765,14 @@ describe(`GraphQL type inferance`, () => { }) expect(fields.test.type).not.toEqual(fields2.test.type) }) + + it(`Creates a new type after schema updates clear union types`, () => { + const nodes = [{ test___NODE: [`pet_1`, `child_1`] } ] + const fields = inferObjectStructureFromNodes({ nodes, types }) + clearUnionTypes() + const updatedFields = inferObjectStructureFromNodes({ nodes, types }) + expect(fields.test.type).not.toEqual(updatedFields.test.type) + }) }) it(`Infers graphql type from array of nodes`, () => From 8955e56bef1ad3dee964b6af5992b04fa6f7eb3c Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Thu, 18 Oct 2018 19:48:19 +0200 Subject: [PATCH 11/15] Move union types to separate describe block and clear union types before each test --- .../__tests__/infer-graphql-type-test.js | 134 +++++++++--------- 1 file changed, 70 insertions(+), 64 deletions(-) diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index c23cb8a4ad8a3..2ff98e6d280d7 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -698,80 +698,86 @@ describe(`GraphQL type inferance`, () => { ) }) - it(`Creates union types when an array field is linking to multiple node types`, async () => { - let result = await queryResult( - [{ linked___NODE: [`child_1`, `pet_1`] }], - ` - linked { - __typename - ... on Child { - hair - } - ... on Pet { - species - } - } - `, - { types } - ) - expect(result.errors).not.toBeDefined() - expect(result.data.listNode[0].linked[0].hair).toEqual(`brown`) - expect(result.data.listNode[0].linked[0].__typename).toEqual(`Child`) - expect(result.data.listNode[0].linked[1].species).toEqual(`dog`) - expect(result.data.listNode[0].linked[1].__typename).toEqual(`Pet`) - store.dispatch({ - type: `CREATE_NODE`, - payload: { id: `baz`, internal: { type: `Bar` } }, + describe(`Creation of union types when array field is linking to multiple types`, () => { + beforeEach(() => { + clearUnionTypes() }) - }) - it(`Uses same union type for same child node types and key`, () => { - const fields = inferObjectStructureFromNodes({ - nodes: [{ test___NODE: [`pet_1`, `child_1`] } ], - types, + it(`Creates union types`, async () => { + let result = await queryResult( + [{ linked___NODE: [`child_1`, `pet_1`] }], + ` + linked { + __typename + ... on Child { + hair + } + ... on Pet { + species + } + } + `, + { types } + ) + expect(result.errors).not.toBeDefined() + expect(result.data.listNode[0].linked[0].hair).toEqual(`brown`) + expect(result.data.listNode[0].linked[0].__typename).toEqual(`Child`) + expect(result.data.listNode[0].linked[1].species).toEqual(`dog`) + expect(result.data.listNode[0].linked[1].__typename).toEqual(`Pet`) + store.dispatch({ + type: `CREATE_NODE`, + payload: { id: `baz`, internal: { type: `Bar` } }, + }) }) - const fields2 = inferObjectStructureFromNodes({ - nodes: [{ test___NODE: [`pet_1`, `child_2`] }], - types, + + it(`Uses same union type for same child node types and key`, () => { + const fields = inferObjectStructureFromNodes({ + nodes: [{ test___NODE: [`pet_1`, `child_1`] } ], + types, + }) + const fields2 = inferObjectStructureFromNodes({ + nodes: [{ test___NODE: [`pet_1`, `child_2`] }], + types, + }) + expect(fields.test.type).toEqual(fields2.test.type) }) - expect(fields.test.type).toEqual(fields2.test.type) - }) - it(`Uses a different type for the same child node types but a different key`, () => { - const fields = inferObjectStructureFromNodes({ - nodes: [{ test___NODE: [`pet_1`, `child_1`] }], - types, - }) - const fields2 = inferObjectStructureFromNodes({ - nodes: [{ differentKey___NODE: [`pet_1`, `child_2`] }], - types, + it(`Uses a different type for the same child node types but a different key`, () => { + const fields = inferObjectStructureFromNodes({ + nodes: [{ test___NODE: [`pet_1`, `child_1`] }], + types, + }) + const fields2 = inferObjectStructureFromNodes({ + nodes: [{ differentKey___NODE: [`pet_1`, `child_2`] }], + types, + }) + expect(fields.test.type).not.toEqual(fields2.differentKey.type) }) - expect(fields.test.type).not.toEqual(fields2.differentKey.type) - }) - it(`Uses a different type for the different child node types but the same key`, () => { - store.dispatch({ - type: `CREATE_NODE`, - payload: { id: `toy_1`, internal: { type: `Toy` } }, - }) - const fields = inferObjectStructureFromNodes({ - nodes: [{ test___NODE: [`pet_1`, `child_1`] } ], - types, - }) - const fields2 = inferObjectStructureFromNodes({ - nodes: [{ test___NODE: [`pet_1`, `child_1`, `toy_1`] }], - types: types.concat([{ name: `Toy` }]), + it(`Uses a different type for the different child node types but the same key`, () => { + store.dispatch({ + type: `CREATE_NODE`, + payload: { id: `toy_1`, internal: { type: `Toy` } }, + }) + const fields = inferObjectStructureFromNodes({ + nodes: [{ test___NODE: [`pet_1`, `child_1`] } ], + types, + }) + const fields2 = inferObjectStructureFromNodes({ + nodes: [{ test___NODE: [`pet_1`, `child_1`, `toy_1`] }], + types: types.concat([{ name: `Toy` }]), + }) + expect(fields.test.type).not.toEqual(fields2.test.type) }) - expect(fields.test.type).not.toEqual(fields2.test.type) - }) - it(`Creates a new type after schema updates clear union types`, () => { - const nodes = [{ test___NODE: [`pet_1`, `child_1`] } ] - const fields = inferObjectStructureFromNodes({ nodes, types }) - clearUnionTypes() - const updatedFields = inferObjectStructureFromNodes({ nodes, types }) - expect(fields.test.type).not.toEqual(updatedFields.test.type) + it(`Creates a new type after schema updates clear union types`, () => { + const nodes = [{ test___NODE: [`pet_1`, `child_1`] } ] + const fields = inferObjectStructureFromNodes({ nodes, types }) + clearUnionTypes() + const updatedFields = inferObjectStructureFromNodes({ nodes, types }) + expect(fields.test.type).not.toEqual(updatedFields.test.type) + }) }) }) From 0abce60821770da3526a0051e2a734093ba9b5cb Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Thu, 18 Oct 2018 20:09:07 +0200 Subject: [PATCH 12/15] Convert seenNames to Map and add clear method --- packages/gatsby/src/schema/create-type-name.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/gatsby/src/schema/create-type-name.js b/packages/gatsby/src/schema/create-type-name.js index 1a28ea6ca90ee..90a2dfbf75773 100644 --- a/packages/gatsby/src/schema/create-type-name.js +++ b/packages/gatsby/src/schema/create-type-name.js @@ -1,14 +1,18 @@ const _ = require(`lodash`) -const seenNames = {} +const seenNames = new Map() module.exports = function createTypeName(name) { const cameledName = _.camelCase(name) - if (seenNames[cameledName]) { - seenNames[cameledName] += 1 - return `${cameledName}_${seenNames[cameledName]}` + if (seenNames.has(cameledName)) { + seenNames.set(cameledName, seenNames.get(cameledName) + 1) + return `${cameledName}_${seenNames.get(cameledName)}` } else { - seenNames[cameledName] = 1 + seenNames.set(cameledName, 1) return cameledName } } + +module.exports.clearTypeNames = () => { + seenNames.clear() +} From a74e5b6567f3c23d4a5b44112d6d8eb4f8e57e8e Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Thu, 18 Oct 2018 20:11:06 +0200 Subject: [PATCH 13/15] Add test case for reliably naming union types --- .../src/schema/__tests__/infer-graphql-type-test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index 2ff98e6d280d7..4cab37d4b339b 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -9,6 +9,7 @@ const normalizePath = require(`normalize-path`) const { clearTypeExampleValues } = require(`../data-tree-utils`) const { typeConflictReporter } = require(`../type-conflict-reporter`) const { inferObjectStructureFromNodes, clearUnionTypes } = require(`../infer-graphql-type`) +const { clearTypeNames } = require(`../create-type-name`) function queryResult(nodes, fragment, { types = [], ignoreFields } = {}) { const schema = new GraphQLSchema({ @@ -700,6 +701,7 @@ describe(`GraphQL type inferance`, () => { describe(`Creation of union types when array field is linking to multiple types`, () => { beforeEach(() => { + clearTypeNames() clearUnionTypes() }) @@ -778,6 +780,14 @@ describe(`GraphQL type inferance`, () => { const updatedFields = inferObjectStructureFromNodes({ nodes, types }) expect(fields.test.type).not.toEqual(updatedFields.test.type) }) + + it(`Uses a reliable naming convention for union types`, () => { + const nodes = [{ test___NODE: [`pet_1`, `child_1`] } ] + const fields = inferObjectStructureFromNodes({ nodes, types }) + clearUnionTypes() + const updatedFields = inferObjectStructureFromNodes({ nodes, types }) + expect(updatedFields.test.type.ofType.name).toEqual('unionTestNode_2') + }) }) }) From d03819f0765ff2d149a95d6e1f763a713ce16fbe Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Thu, 18 Oct 2018 20:13:56 +0200 Subject: [PATCH 14/15] Remove unused fields as a variable, use backticks for all strings --- .../gatsby/src/schema/__tests__/infer-graphql-type-test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index 4cab37d4b339b..f8d88b0bfe0da 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -783,10 +783,10 @@ describe(`GraphQL type inferance`, () => { it(`Uses a reliable naming convention for union types`, () => { const nodes = [{ test___NODE: [`pet_1`, `child_1`] } ] - const fields = inferObjectStructureFromNodes({ nodes, types }) + inferObjectStructureFromNodes({ nodes, types }) clearUnionTypes() const updatedFields = inferObjectStructureFromNodes({ nodes, types }) - expect(updatedFields.test.type.ofType.name).toEqual('unionTestNode_2') + expect(updatedFields.test.type.ofType.name).toEqual(`unionTestNode_2`) }) }) }) From 746ccc3c1008fa8f1eb1ebb8e9e6c441206ae251 Mon Sep 17 00:00:00 2001 From: Harold Angenent Date: Thu, 18 Oct 2018 20:23:40 +0200 Subject: [PATCH 15/15] Improve language for test descriptions --- .../gatsby/src/schema/__tests__/infer-graphql-type-test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index f8d88b0bfe0da..82bdff5d8820d 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -745,7 +745,7 @@ describe(`GraphQL type inferance`, () => { }) - it(`Uses a different type for the same child node types but a different key`, () => { + it(`Uses a different type for the same child node types with a different key`, () => { const fields = inferObjectStructureFromNodes({ nodes: [{ test___NODE: [`pet_1`, `child_1`] }], types, @@ -757,7 +757,7 @@ describe(`GraphQL type inferance`, () => { expect(fields.test.type).not.toEqual(fields2.differentKey.type) }) - it(`Uses a different type for the different child node types but the same key`, () => { + it(`Uses a different type for different child node types with the same key`, () => { store.dispatch({ type: `CREATE_NODE`, payload: { id: `toy_1`, internal: { type: `Toy` } }, @@ -781,7 +781,7 @@ describe(`GraphQL type inferance`, () => { expect(fields.test.type).not.toEqual(updatedFields.test.type) }) - it(`Uses a reliable naming convention for union types`, () => { + it(`Uses a reliable naming convention`, () => { const nodes = [{ test___NODE: [`pet_1`, `child_1`] } ] inferObjectStructureFromNodes({ nodes, types }) clearUnionTypes()