apollo-feature-requests
apollo-feature-requests copied to clipboard
Support `@defer` directive for local resolvers
Description
I have a local resolver for a client-side field which fetches some data from the server. The fetch that happens in that resolver can take up to 3 seconds to complete. This is a problem because when that field is part of another query, the entire query will have to wait for the resolver code to finish before the query's loading state changes to false.
Proposed solution
As the title states, I'd like to be able to use the @defer directive alongside the @client directive to instruct Apollo Client that this field can be deferred. For example, take the following query:
query PersonQuery($personId: ID!) {
person(id: $personId) {
id
friends @client @defer
}
}
The friends field is a reference to a field that is being resolved locally using resolvers passed to Apollo Client. The friends resolver returns a Promise, which resolves itself after an asynchronous request is made to an external API.
Since Apollo Client knows about the friends resolver function and knows that it is a Promise, when it encounters the above query, it could initially return { id: "id", friends: null }, and when the Promise resolves, return the full set of data, e.g., { id: "id", friends: [...] }.
Alternative approaches
What I'm really looking for is a way to tell Apollo Client that it can resolve the initial query without waiting for the local resolver to finish. This doesn't need to be done using @defer.