Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo client: Can @defer be used with client side resolvers?

For some reason, I had to build a client-side only GraphQL server, my schema is built as follow:

    private buildSchema(): GraphQLSchema {
        const allTypes: string = ...// my types
        const allResolvers: IResolvers[] = ...// my resolvers

        return makeExecutableSchema({
            typeDefs: allTypes,
            resolvers: allResolvers
        });
    }

The client is as follow:

        this.client = new ApolloClient({
            link: new SchemaLink({schema: this.buildSchema()}),
            cache: new InMemoryCache({
                addTypename: false
            })
        });

And everything works fine except that my queries are not defered. For instance if I run:

const gqlQuery: string = `
{
user {
   name
   slowResolver @defer {
      text
      }
   }
}
`

const $result = this.apollo.getClient().watchQuery({
query: gql(gqlQuery)
});

The $result will be emited only when the whole query will be resolved (instead of user and then slowResolver as expected).

Any idea of what I missed in the workflow?

like image 754
Flavien Volken Avatar asked Dec 01 '25 02:12

Flavien Volken


1 Answers

The @defer directive was actually removed from Apollo, although there's been some work done to reimplement it. Even if it's implemented, though, deferred queries would have to be handled outside of the execution context. In other words, executing the schema can return a deferred execution result, but something else (like Apollo server itself) has to handle how that response (both the initial payload, and the subsequent patches) are actually sent to the server over whatever transport.

If you're defining a schema client-side, unfortunately, it's not going to be possible to use the @defer directive.

like image 68
Daniel Rearden Avatar answered Dec 05 '25 11:12

Daniel Rearden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!