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

Error logging for new interface type semantics #350

Merged
merged 1 commit into from
May 5, 2016
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
61 changes: 61 additions & 0 deletions src/type/__tests__/schema-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

import {
GraphQLSchema,
GraphQLInterfaceType,
GraphQLObjectType,
GraphQLString
} from '../';

import { describe, it } from 'mocha';
import { expect } from 'chai';

const InterfaceType = new GraphQLInterfaceType({
name: 'Interface',
fields: { fieldName: { type: GraphQLString } },
resolveType() {
return ImplementingType;
}
});

const ImplementingType = new GraphQLObjectType({
name: 'Object',
interfaces: [ InterfaceType ],
fields: { fieldName: { type: GraphQLString, resolve: () => '' }}
});

const Schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
getObject: {
type: InterfaceType,
resolve() {
return {};
}
}
}
})
});

describe('Type System: Schema', () => {
describe('Getting possible types', () => {
it('throws human-reable error if schema.types is not defined', () => {
const checkPossible = () => {
return Schema.isPossibleType(InterfaceType, ImplementingType);
};
expect(checkPossible).to.throw(
'Could not find possible implementing types for Interface in schema. ' +
'Check that schema.types is defined and is an array ofall possible ' +
'types in the schema.'
);
});
});
});
9 changes: 8 additions & 1 deletion src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,15 @@ export class GraphQLSchema {
}

if (!possibleTypeMap[abstractType.name]) {
const possibleTypes = this.getPossibleTypes(abstractType);
invariant(
Array.isArray(possibleTypes),
`Could not find possible implementing types for ${abstractType} in ` +
'schema. Check that schema.types is defined and is an array of' +
'all possible types in the schema.'
);
possibleTypeMap[abstractType.name] =
this.getPossibleTypes(abstractType).reduce(
possibleTypes.reduce(
(map, type) => ((map[type.name] = true), map),
Object.create(null)
);
Expand Down