At a glance
- Project
- GraphQL Spec
- Identifier
- #776
- Stage
- RFC 3 / Accepted
- Champion
- @benjie
- Updated
- 2025-06-14
- Latest activity
- Added to WG agenda on
- Source
- Pull request
Pull request 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
- Discussed in GraphQL WG
April 2021
- Pull request merged on by leebyron
- @leebyron committed "Update Section 5 -- Validation.md" on
- Advanced to RFC 3 on by leebyron
January 2021
- Advanced to RFC 2 on by leebyron
- Discussed in GraphQL WG
December 2020
- Advanced to RFC 1 on by leebyron
- Discussed in GraphQL WG
November 2020
- Top comment edited on by benjie
- 4 commits pushed on
- @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 by benjie
- Discussed in GraphQL WG
<code>__typename and subscriptions</code> (5m, Benjie)
Meeting notes · GraphQL WG · 2020-10-01
September 2020
- Top comment edited on by benjie
- Pull request created on by benjie
- @benjie committed "__typename is not valid at subscription root" on