Skip to content

Test: Missing subscription field should not be an "internal error" #1106

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 1 commit into from
Dec 6, 2017
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
18 changes: 12 additions & 6 deletions src/subscription/__tests__/subscribe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ function emailSchemaWithResolvers(subscribeFn, resolveFn) {
type: EmailEventType,
resolve: resolveFn,
subscribe: subscribeFn,
// TODO: remove
args: {
priority: { type: GraphQLInt },
},
Expand Down Expand Up @@ -332,7 +331,7 @@ describe('Subscription Initialization Phase', () => {
);
});

it('throws an error for unknown root field', async () => {
it('resolves to an error for unknown subscription field', async () => {
const ast = parse(`
subscription {
unknownField
Expand All @@ -341,10 +340,17 @@ describe('Subscription Initialization Phase', () => {

const pubsub = new EventEmitter();

expectPromiseToThrow(
() => createSubscription(pubsub, emailSchema, ast),
'This subscription is not defined by the schema.',
);
const { subscription } = await createSubscription(pubsub, emailSchema, ast);

expect(subscription).to.deep.equal({
errors: [
{
message: 'The subscription field "unknownField" is not defined.',
locations: [{ line: 3, column: 9 }],
path: undefined,
},
],
});
});

it('throws an error if subscribe does not return an iterator', async () => {
Expand Down
12 changes: 9 additions & 3 deletions src/subscription/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
responsePathAsArray,
} from '../execution/execute';
import { GraphQLSchema } from '../type/schema';
import invariant from '../jsutils/invariant';
import mapAsyncIterator from './mapAsyncIterator';

import type { ObjMap } from '../jsutils/ObjMap';
Expand Down Expand Up @@ -223,8 +222,15 @@ export function createSourceEventStream(
const responseName = responseNames[0];
const fieldNodes = fields[responseName];
const fieldNode = fieldNodes[0];
const fieldDef = getFieldDef(schema, type, fieldNode.name.value);
invariant(fieldDef, 'This subscription is not defined by the schema.');
const fieldName = fieldNode.name.value;
const fieldDef = getFieldDef(schema, type, fieldName);

if (!fieldDef) {
throw new GraphQLError(
`The subscription field "${fieldName}" is not defined.`,
fieldNodes,
);
}

// Call the `subscribe()` resolver or the default resolver to produce an
// AsyncIterable yielding raw payloads.
Expand Down