Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f2c96b0

Browse files
authored
Test: Missing subscription field should not be an "internal error" (graphql#1106)
A remaining issue with subscriptions is attempting to subscribe to a missing field. The existing implementation creates an internal error which is an improper blame since there is no possibility to fix it internally - it is an issue with the query. Since there is no clear consistent behavior relative to query/mutation operations (which simply skip over unknown fields), and the corner-case where this occurs should deal with the error - this changes the internal error to an external yielded GraphQL error. Note: we still require a spec change since there is an ambiguity of this exact scenario.
1 parent c1eca36 commit f2c96b0

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/subscription/__tests__/subscribe-test.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ function emailSchemaWithResolvers(subscribeFn, resolveFn) {
7272
type: EmailEventType,
7373
resolve: resolveFn,
7474
subscribe: subscribeFn,
75-
// TODO: remove
7675
args: {
7776
priority: { type: GraphQLInt },
7877
},
@@ -332,7 +331,7 @@ describe('Subscription Initialization Phase', () => {
332331
);
333332
});
334333

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

342341
const pubsub = new EventEmitter();
343342

344-
expectPromiseToThrow(
345-
() => createSubscription(pubsub, emailSchema, ast),
346-
'This subscription is not defined by the schema.',
347-
);
343+
const { subscription } = await createSubscription(pubsub, emailSchema, ast);
344+
345+
expect(subscription).to.deep.equal({
346+
errors: [
347+
{
348+
message: 'The subscription field "unknownField" is not defined.',
349+
locations: [{ line: 3, column: 9 }],
350+
path: undefined,
351+
},
352+
],
353+
});
348354
});
349355

350356
it('throws an error if subscribe does not return an iterator', async () => {

src/subscription/subscribe.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
responsePathAsArray,
2424
} from '../execution/execute';
2525
import { GraphQLSchema } from '../type/schema';
26-
import invariant from '../jsutils/invariant';
2726
import mapAsyncIterator from './mapAsyncIterator';
2827

2928
import type { ObjMap } from '../jsutils/ObjMap';
@@ -223,8 +222,15 @@ export function createSourceEventStream(
223222
const responseName = responseNames[0];
224223
const fieldNodes = fields[responseName];
225224
const fieldNode = fieldNodes[0];
226-
const fieldDef = getFieldDef(schema, type, fieldNode.name.value);
227-
invariant(fieldDef, 'This subscription is not defined by the schema.');
225+
const fieldName = fieldNode.name.value;
226+
const fieldDef = getFieldDef(schema, type, fieldName);
227+
228+
if (!fieldDef) {
229+
throw new GraphQLError(
230+
`The subscription field "${fieldName}" is not defined.`,
231+
fieldNodes,
232+
);
233+
}
228234

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

0 commit comments

Comments
 (0)