From ff0afa2e369e079663f9099dd9dc3b0c5f459d0f Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Wed, 22 May 2019 21:07:54 +0300 Subject: [PATCH] buildClientSchema: include standard type only if it is used (#1809) Details: https://github.com/graphql/graphql-js/commit/183ff32bee4bc23eb23657f79f414c2400ecb06a#r32971387 --- .../__tests__/buildClientSchema-test.js | 29 ++++++++++--------- src/utilities/buildClientSchema.js | 4 ++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/utilities/__tests__/buildClientSchema-test.js b/src/utilities/__tests__/buildClientSchema-test.js index 0ec0f1d994..a1e77c19f7 100644 --- a/src/utilities/__tests__/buildClientSchema-test.js +++ b/src/utilities/__tests__/buildClientSchema-test.js @@ -40,9 +40,6 @@ function cycleIntrospection(sdlString) { const clientSchema = buildClientSchema(initialIntrospection); const secondIntrospection = introspectionFromSchema(clientSchema); - hackToRemoveStandardTypes(secondIntrospection); - hackToRemoveStandardTypes(initialIntrospection); - /** * If the client then runs the introspection query against the client-side * schema, it should get a result identical to what was returned by the server @@ -51,14 +48,6 @@ function cycleIntrospection(sdlString) { return printSchema(clientSchema); } -// Temporary hack to remove always presented standard types should be removed in 15.0 -function hackToRemoveStandardTypes(introspection) { - (introspection.__schema: any).types = introspection.__schema.types.filter( - ({ name }) => - ['ID', 'Float', 'Int', 'Boolean', 'String'].indexOf(name) === -1, - ); -} - describe('Type System: build schema from introspection', () => { it('builds a simple schema', () => { const sdl = dedent` @@ -138,6 +127,20 @@ describe('Type System: build schema from introspection', () => { expect(clientSchema.getType('CustomScalar')).not.to.equal(customScalar); }); + it('include standard type only if it is used', () => { + const schema = buildSchema(` + type Query { + foo: String + } + `); + const introspection = introspectionFromSchema(schema); + const clientSchema = buildClientSchema(introspection); + + expect(clientSchema.getType('Int')).to.equal(undefined); + expect(clientSchema.getType('Float')).to.equal(undefined); + expect(clientSchema.getType('ID')).to.equal(undefined); + }); + it('builds a schema with a recursive type reference', () => { const sdl = dedent` schema { @@ -329,10 +332,8 @@ describe('Type System: build schema from introspection', () => { const introspection = introspectionFromSchema(schema); const clientSchema = buildClientSchema(introspection); - const secondIntrospection = introspectionFromSchema(clientSchema); - hackToRemoveStandardTypes(secondIntrospection); - hackToRemoveStandardTypes(introspection); + const secondIntrospection = introspectionFromSchema(clientSchema); expect(secondIntrospection).to.deep.equal(introspection); const clientFoodEnum = clientSchema.getType('Food'); diff --git a/src/utilities/buildClientSchema.js b/src/utilities/buildClientSchema.js index 26c8ad6084..acad5033d3 100644 --- a/src/utilities/buildClientSchema.js +++ b/src/utilities/buildClientSchema.js @@ -90,7 +90,9 @@ export function buildClientSchema( ); for (const stdType of [...specifiedScalarTypes, ...introspectionTypes]) { - typeMap[stdType.name] = stdType; + if (typeMap[stdType.name]) { + typeMap[stdType.name] = stdType; + } } // Get the root Query, Mutation, and Subscription types.