Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildClientSchema: include standard type only if it is used #1809

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -92,7 +92,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