Skip to content

Commit

Permalink
buildClientSchema: add dev check for invalid introspection argument (#…
Browse files Browse the repository at this point in the history
…1974)

Prevents situations like #1970
  • Loading branch information
IvanGoncharov authored Jun 12, 2019
1 parent 42ef363 commit 91d673f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/utilities/__tests__/buildClientSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,18 @@ describe('Type System: build schema from introspection', () => {
directive @SomeDirective on QUERY
`);

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',
);

// $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: {}',
);
});

it('throws when referenced unknown type', () => {
const introspection = introspectionFromSchema(dummySchema);

Expand Down
7 changes: 7 additions & 0 deletions src/utilities/buildClientSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import objectValues from '../polyfills/objectValues';
import inspect from '../jsutils/inspect';
import invariant from '../jsutils/invariant';
import keyValMap from '../jsutils/keyValMap';
import isObjectLike from '../jsutils/isObjectLike';
import { valueFromAST } from './valueFromAST';
import { parseValue } from '../language/parser';
import {
Expand Down Expand Up @@ -72,6 +73,12 @@ export function buildClientSchema(
introspection: IntrospectionQuery,
options?: Options,
): GraphQLSchema {
invariant(
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),
);

// Get the schema from the introspection result.
const schemaIntrospection = introspection.__schema;

Expand Down

0 comments on commit 91d673f

Please sign in to comment.