-
Couldn't load subscription status.
- Fork 2.1k
Description
Currently, query resolvers for Array types can return not only JavaScript arrays, but any iterable. However, async iterables are not (yet) supported. Since GraphQL.js supports async resolvers and iterables, it would be only consequential to support async iterables here as well.
This question has been raised in #1537 and #2262 already. In the first issue, it was assumed that this would help to create a streaming interface, and I therefore dismissed and closed. But we may want to support async generators for other reasons, like those discussed in the second issue. Also, some backend services may already provide async iterable that could then be directly used in resolvers. In the first issue, I also argued that async generators are not guaranteed to finish, but the same is true for sync generators, so this is not really a reason to not support them. I thought it may be good to open a new separate issue to discuss this question.
Here is an example that shows how I expect this to work:
import { graphql, GraphQLSchema,
GraphQLObjectType, GraphQLList, GraphQLString } from 'graphql';
// currently works only if you remove the "async" here:
async function* resolve() {
for (const s of ["my", "values"]) yield s;
}
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
list: {
type: new GraphQLList(GraphQLString),
resolve
},
},
}),
});
const source = '{ list }';
const result = await graphql({ schema, source })
console.log(result)As it is, the code throws an GraphQLError: "Expected Iterable, but did not find one for field Query.list."
To change this, the completeListValue function would need to support the case that result is an async iterable. If that was supported already, we could probably just replace the Array.from by Array.fromAsync.