Is there any built in @defer support? #324
-
I've been racking my brain trying to figure out how to use graphql-java-tools for the resolvers and autogenerated GraphQL endpoint together with graphql-java's support for the defer directive. I already have a BatchLoader returning the values via CompletableFuture.supplyAsync so it feels like it should be an easy wire-up. Am I missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I have the same question (#433 ). Wish one of the authors provided some example, code snippet or documentation. |
Beta Was this translation helpful? Give feedback.
-
Okay I figured out a small clean hack to get it to work with minimal effort graphql-java project has inbuilt In this graphql-java-tools project, you can technically create a GraphQLSchema object and expose it as a bean and the kickstart autoconfigure will pick it up. Problem is, when you create a GraphQLSchema object you need to define all the resolvers and types etc, which is all boilerplate code. This project automatically autowires all of it using spring. So doing it in the graphql-java way would defeat the purpose of using this project. To circumvent that, for now, unless the authors of this project find a more elegant way to support this, you can get Add this bean in any @configuration class @Bean
public GraphQLDirective deferDirective(GraphQLSchemaServletProvider graphQLSchemaServletProvider) throws IllegalAccessException {
GraphQLSchema graphQLSchema = graphQLSchemaServletProvider.getSchema();
//getting existing directives
Set<GraphQLDirective> graphQLDirectives = new HashSet<>(graphQLSchema.getDirectives());
//adding Defer directive to the existing set
graphQLDirectives.add(Directives.DeferDirective);
//setting private field using reflection since there is no public way to modify this variable
FieldUtils.writeField(graphQLSchema, "directives", graphQLDirectives, true);
return Directives.DeferDirective;
} And that's all, you now have enabled defer support. You don't need to modify any existing code, or return CompletableFuture, Publisher etc. Its all just done by the engine. When you send a query to the server, you can just add @defer to any field in a query and that field's result will be deferred. Ex:- In this query, postText resolves quickly whereas comments and reviews would show up later when they are computed resolved using service side events. query {
post {
postText
comments @defer {
commentText
}
reviews @defer {
reviewText {
}
} Note:- the type of the deferred field has to be nullable (So you cannot use !) |
Beta Was this translation helpful? Give feedback.
@Roundaround
Okay I figured out a small clean hack to get it to work with minimal effort
graphql-java project has inbuilt
@defer
support. The directives can be defined while creating a GraphQLSchema Object. Previously@defer
directive was enabled by default. But now it is disabled by default. So, you need to enable it by addinggraphql.Directives.DeferDirective
to the set of derectives.In this graphql-java-tools project, you can technically create a GraphQLSchema object and expose it as a bean and the kickstart autoconfigure will pick it up. Problem is, when you create a GraphQLSchema object you need to define all the resolvers and types etc, which is all boilerplate code. This project a…