Skip to content

Consistent error messages in buildClientSchema #2186

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

Merged
merged 10 commits into from
Nov 11, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/utilities/__tests__/buildClientSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: {}.',
);
});

Expand Down Expand Up @@ -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: {}.',
);
});

Expand All @@ -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", .* }\./,
);
});

Expand All @@ -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", .* }\./,
);
});

Expand Down Expand Up @@ -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", .* }\./,
);
});

Expand All @@ -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", .* }\./,
);
});

Expand Down Expand Up @@ -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",.* }\./,
);
});

Expand All @@ -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", .* }\./,
);
});

Expand All @@ -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", .* }\./,
);
});

Expand All @@ -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", .* }\./,
);
});

Expand All @@ -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", .* }\./,
);
});
});
Expand Down
52 changes: 26 additions & 26 deletions src/utilities/buildClientSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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}.`,
);
}

Expand All @@ -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}.`,
);
}

Expand Down Expand Up @@ -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}.`,
);
}

Expand Down Expand Up @@ -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}.`,
);
}

Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -336,17 +336,17 @@ 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(
typeIntrospection.fields,
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 {
Expand Down Expand Up @@ -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({
Expand Down