At a glance
- Identifier: #776
- Stage: RFC 3 / Accepted
- Champion: @benjie
- Latest activity: Added to WG agenda on 2021-05-13
- Spec PR: https://github.com/graphql/graphql-spec/pull/776
Spec PR description
I'm writing up some of the GraphQL "query ambiguity" work currently and noticed this issue in the spec. In the case of a subscription it seems that
subscription {
__typename
}
does not correctly get evaluated by the reference implementation during subscriptions (though you can query it with graphql(...) you can't access it via subscribe(...)). It doesn't really make sense to request __typename here since it's not ever going to change during the life of the schema, and subscriptions only support one root-level field, however I thought it was worth highlighting.
Reproduction with GraphQL-js (toggle which doc is commented to see a functioning subscription):
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLInt,
GraphQLFloat,
parse,
validate,
subscribe,
} = require("graphql");
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function* everySecond() {
for (let i = 0; i < 1000; i++) {
yield i;
await sleep(1000);
}
}
const Query = new GraphQLObjectType({
name: "Query",
fields: {
a: {
type: GraphQLInt,
},
},
});
const Subscription = new GraphQLObjectType({
name: "Subscription",
fields: {
ts: {
type: GraphQLFloat,
subscribe() {
return everySecond();
},
resolve() {
return Date.now();
},
},
},
});
const schema = new GraphQLSchema({
query: Query,
subscription: Subscription,
});
async function main() {
const doc = parse("subscription { __typename }");
// const doc = parse("subscription { ts }");
const errors = validate(schema, doc);
if (errors && errors.length) {
console.dir(errors);
throw new Error("Errors occurred");
}
const result = await subscribe(schema, doc);
console.dir(result);
for await (const r of result) {
console.dir(r);
}
}
main().catch(e => {
console.dir(e);
process.exit(1);
});
Produces this error:
Error: Subscription field must return Async Iterable. Received: undefined
at [...]/node_modules/graphql/subscription/subscribe.js:169:13
at async main ([...]/test.js:57:18)
Timeline
May 2021
April 2021
- Spec PR merged on 2021-04-15 by leebyron
- @leebyron committed "Update Section 5 -- Validation.md" on 2021-04-15
- Advanced to RFC 3 on 2021-04-15 by leebyron
January 2021
- Advanced to RFC 2 on 2021-01-07 by leebyron
- Added to WG agenda on 2021-01-07
- Mentioned in WG notes on 2021-01-07
December 2020
- Advanced to RFC 1 on 2020-12-03 by leebyron
- Added to WG agenda on 2020-12-03
- Mentioned in WG notes on 2020-12-03
November 2020
- Top comment edited on 2020-11-30 by benjie
- 4 commits pushed on 2020-11-25
- @benjie committed "Update spec edits based on Lee's comments"
- @benjie committed "Simplify"
- @benjie committed "Clarify"
- @benjie committed "Typo"
October 2020
- Top comment edited on 2020-10-01 by benjie
- Added to WG agenda on 2020-10-01
- Mentioned in WG notes on 2020-10-01
September 2020
- Top comment edited on 2020-09-14 by benjie
- Spec PR created on 2020-09-14 by benjie
- @benjie committed "__typename is not valid at subscription root" on 2020-09-14