diff --git a/src/utilities/__tests__/buildClientSchema-test.js b/src/utilities/__tests__/buildClientSchema-test.js index b474fc423a..6ad7618fa7 100644 --- a/src/utilities/__tests__/buildClientSchema-test.js +++ b/src/utilities/__tests__/buildClientSchema-test.js @@ -29,10 +29,10 @@ import { introspectionFromSchema } from '../introspectionFromSchema'; /** * This function does a full cycle of going from a string with the contents of * the SDL, build in-memory GraphQLSchema from it, produce a client-side - * representation of the schema by using "buildClientSchema"and then finally - * printing that that schema into the SDL + * representation of the schema by using "buildClientSchema" and then + * returns that schema printed as SDL. */ -function cycleIntrospection(sdlString) { +function cycleIntrospection(sdlString: string): string { const serverSchema = buildSchema(sdlString); const initialIntrospection = introspectionFromSchema(serverSchema); const clientSchema = buildClientSchema(initialIntrospection); @@ -53,7 +53,7 @@ describe('Type System: build schema from introspection', () => { query: Simple } - """This is simple type""" + """This is a simple type""" type Simple { """This is a string field""" string: String @@ -143,7 +143,7 @@ describe('Type System: build schema from introspection', () => { expect(clientSchema.getType('CustomScalar')).not.to.equal(customScalar); }); - it('include standard type only if it is used', () => { + it('includes standard types only if they are used', () => { const schema = buildSchema(` type Query { foo: String @@ -604,12 +604,12 @@ describe('Type System: build schema from introspection', () => { it('throws when introspection is missing __schema property', () => { // $DisableFlowOnNegativeTest expect(() => buildClientSchema(null)).to.throw( - 'Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: null', + 'Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: null.', ); // $DisableFlowOnNegativeTest expect(() => buildClientSchema({})).to.throw( - 'Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: {}', + 'Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: {}.', ); }); @@ -653,7 +653,7 @@ describe('Type System: build schema from introspection', () => { delete introspection.__schema.queryType.name; expect(() => buildClientSchema(introspection)).to.throw( - 'Unknown type reference: {}', + 'Unknown type reference: {}.', ); }); @@ -669,7 +669,7 @@ describe('Type System: build schema from introspection', () => { delete queryTypeIntrospection.kind; expect(() => buildClientSchema(introspection)).to.throw( - 'Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema', + /Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: { name: "Query", .* }\./, ); }); @@ -685,7 +685,7 @@ describe('Type System: build schema from introspection', () => { delete queryTypeIntrospection.interfaces; expect(() => buildClientSchema(introspection)).to.throw( - 'Introspection result missing interfaces: { kind: "OBJECT", name: "Query",', + /Introspection result missing interfaces: { kind: "OBJECT", name: "Query", .* }\./, ); }); @@ -715,7 +715,7 @@ describe('Type System: build schema from introspection', () => { delete queryTypeIntrospection.fields; expect(() => buildClientSchema(introspection)).to.throw( - 'Introspection result missing fields: { kind: "OBJECT", name: "Query",', + /Introspection result missing fields: { kind: "OBJECT", name: "Query", .* }\./, ); }); @@ -731,7 +731,7 @@ describe('Type System: build schema from introspection', () => { delete queryTypeIntrospection.fields[0].args; expect(() => buildClientSchema(introspection)).to.throw( - 'Introspection result missing field args: { name: "foo",', + /Introspection result missing field args: { name: "foo", .* }\./, ); }); @@ -783,7 +783,7 @@ describe('Type System: build schema from introspection', () => { delete someUnionIntrospection.possibleTypes; expect(() => buildClientSchema(introspection)).to.throw( - 'Introspection result missing possibleTypes: { kind: "UNION", name: "SomeUnion",', + /Introspection result missing possibleTypes: { kind: "UNION", name: "SomeUnion",.* }\./, ); }); @@ -799,7 +799,7 @@ describe('Type System: build schema from introspection', () => { delete someEnumIntrospection.enumValues; expect(() => buildClientSchema(introspection)).to.throw( - 'Introspection result missing enumValues: { kind: "ENUM", name: "SomeEnum",', + /Introspection result missing enumValues: { kind: "ENUM", name: "SomeEnum", .* }\./, ); }); @@ -815,7 +815,7 @@ describe('Type System: build schema from introspection', () => { delete someInputObjectIntrospection.inputFields; expect(() => buildClientSchema(introspection)).to.throw( - 'Introspection result missing inputFields: { kind: "INPUT_OBJECT", name: "SomeInputObject",', + /Introspection result missing inputFields: { kind: "INPUT_OBJECT", name: "SomeInputObject", .* }\./, ); }); @@ -832,7 +832,7 @@ describe('Type System: build schema from introspection', () => { delete someDirectiveIntrospection.locations; expect(() => buildClientSchema(introspection)).to.throw( - 'Introspection result missing directive locations: { name: "SomeDirective",', + /Introspection result missing directive locations: { name: "SomeDirective", .* }\./, ); }); @@ -849,7 +849,7 @@ describe('Type System: build schema from introspection', () => { delete someDirectiveIntrospection.args; expect(() => buildClientSchema(introspection)).to.throw( - 'Introspection result missing directive args: { name: "SomeDirective",', + /Introspection result missing directive args: { name: "SomeDirective", .* }\./, ); }); }); diff --git a/src/utilities/buildClientSchema.js b/src/utilities/buildClientSchema.js index bedb9118d0..45c96b2bc1 100644 --- a/src/utilities/buildClientSchema.js +++ b/src/utilities/buildClientSchema.js @@ -74,8 +74,9 @@ export function buildClientSchema( ): GraphQLSchema { devAssert( isObjectLike(introspection) && isObjectLike(introspection.__schema), - 'Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: ' + - inspect(introspection), + `Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: ${inspect( + introspection, + )}.`, ); // Get the schema from the introspection result. @@ -88,6 +89,7 @@ export function buildClientSchema( typeIntrospection => buildType(typeIntrospection), ); + // Include standard types only if they are used. for (const stdType of [...specifiedScalarTypes, ...introspectionTypes]) { if (typeMap[stdType.name]) { typeMap[stdType.name] = stdType; @@ -142,7 +144,7 @@ export function buildClientSchema( return GraphQLNonNull(assertNullableType(nullableType)); } if (!typeRef.name) { - throw new Error('Unknown type reference: ' + inspect(typeRef)); + throw new Error(`Unknown type reference: ${inspect(typeRef)}.`); } return getNamedType(typeRef.name); } @@ -163,10 +165,9 @@ export function buildClientSchema( if (isInputType(type)) { return type; } + const typeStr = inspect(type); throw new Error( - 'Introspection must provide input type for arguments, but received: ' + - inspect(type) + - '.', + `Introspection must provide input type for arguments, but received: ${typeStr}.`, ); } @@ -177,10 +178,9 @@ export function buildClientSchema( if (isOutputType(type)) { return type; } + const typeStr = inspect(type); throw new Error( - 'Introspection must provide output type for fields, but received: ' + - inspect(type) + - '.', + `Introspection must provide output type for fields, but received: ${typeStr}.`, ); } @@ -217,9 +217,9 @@ export function buildClientSchema( return buildInputObjectDef(type); } } + const typeStr = inspect(type); throw new Error( - 'Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema:' + - inspect(type), + `Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ${typeStr}.`, ); } @@ -247,9 +247,9 @@ export function buildClientSchema( } if (!implementingIntrospection.interfaces) { + const implementingIntrospectionStr = inspect(implementingIntrospection); throw new Error( - 'Introspection result missing interfaces: ' + - inspect(implementingIntrospection), + `Introspection result missing interfaces: ${implementingIntrospectionStr}.`, ); } @@ -282,9 +282,9 @@ export function buildClientSchema( unionIntrospection: IntrospectionUnionType, ): GraphQLUnionType { if (!unionIntrospection.possibleTypes) { + const unionIntrospectionStr = inspect(unionIntrospection); throw new Error( - 'Introspection result missing possibleTypes: ' + - inspect(unionIntrospection), + `Introspection result missing possibleTypes: ${unionIntrospectionStr}.`, ); } return new GraphQLUnionType({ @@ -298,9 +298,9 @@ export function buildClientSchema( enumIntrospection: IntrospectionEnumType, ): GraphQLEnumType { if (!enumIntrospection.enumValues) { + const enumIntrospectionStr = inspect(enumIntrospection); throw new Error( - 'Introspection result missing enumValues: ' + - inspect(enumIntrospection), + `Introspection result missing enumValues: ${enumIntrospectionStr}.`, ); } return new GraphQLEnumType({ @@ -321,9 +321,9 @@ export function buildClientSchema( inputObjectIntrospection: IntrospectionInputObjectType, ): GraphQLInputObjectType { if (!inputObjectIntrospection.inputFields) { + const inputObjectIntrospectionStr = inspect(inputObjectIntrospection); throw new Error( - 'Introspection result missing inputFields: ' + - inspect(inputObjectIntrospection), + `Introspection result missing inputFields: ${inputObjectIntrospectionStr}.`, ); } return new GraphQLInputObjectType({ @@ -336,7 +336,7 @@ export function buildClientSchema( function buildFieldDefMap(typeIntrospection) { if (!typeIntrospection.fields) { throw new Error( - 'Introspection result missing fields: ' + inspect(typeIntrospection), + `Introspection result missing fields: ${inspect(typeIntrospection)}.`, ); } return keyValMap( @@ -344,9 +344,9 @@ export function buildClientSchema( fieldIntrospection => fieldIntrospection.name, fieldIntrospection => { if (!fieldIntrospection.args) { + const fieldIntrospectionStr = inspect(fieldIntrospection); throw new Error( - 'Introspection result missing field args: ' + - inspect(fieldIntrospection), + `Introspection result missing field args: ${fieldIntrospectionStr}.`, ); } return { @@ -382,15 +382,15 @@ export function buildClientSchema( function buildDirective(directiveIntrospection) { if (!directiveIntrospection.args) { + const directiveIntrospectionStr = inspect(directiveIntrospection); throw new Error( - 'Introspection result missing directive args: ' + - inspect(directiveIntrospection), + `Introspection result missing directive args: ${directiveIntrospectionStr}.`, ); } if (!directiveIntrospection.locations) { + const directiveIntrospectionStr = inspect(directiveIntrospection); throw new Error( - 'Introspection result missing directive locations: ' + - inspect(directiveIntrospection), + `Introspection result missing directive locations: ${directiveIntrospectionStr}.`, ); } return new GraphQLDirective({