Skip to content

Commit

Permalink
buildClientSchema: include standard type only if it is used (#1809)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed May 22, 2019
1 parent 4bff6d8 commit ff0afa2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
29 changes: 15 additions & 14 deletions src/utilities/__tests__/buildClientSchema-test.js
Expand Up @@ -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
Expand All @@ -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`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 3 additions & 1 deletion src/utilities/buildClientSchema.js
Expand Up @@ -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.
Expand Down

0 comments on commit ff0afa2

Please sign in to comment.