diff --git a/src/subscription/__tests__/subscribe-test.js b/src/subscription/__tests__/subscribe-test.js index 180c69f401..3bad969f43 100644 --- a/src/subscription/__tests__/subscribe-test.js +++ b/src/subscription/__tests__/subscribe-test.js @@ -72,7 +72,6 @@ function emailSchemaWithResolvers(subscribeFn, resolveFn) { type: EmailEventType, resolve: resolveFn, subscribe: subscribeFn, - // TODO: remove args: { priority: { type: GraphQLInt }, }, @@ -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 @@ -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 () => { diff --git a/src/subscription/subscribe.js b/src/subscription/subscribe.js index 49a8228c66..eac9ebe5e5 100644 --- a/src/subscription/subscribe.js +++ b/src/subscription/subscribe.js @@ -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'; @@ -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.