10

I am planning to implement Graphql in my spring boot application. I Googled many sites for Graphql server setup in Java and came across two ways of doing it .

One is implementing GraphQlResolver like below

 public class MyResolver implements GraphQLResolver<ModelX>

and another one is by Implementing Datafetcher Reference: https://www.graphql-java.com

@Component
public class MyDataFetcher implements DataFetcher<ModelX> {


@Override
public ModelX get(DataFetchingEnvironment environment) {
    // TODO Auto-generated method stub

}

}

Please provide some information on differences in both the approaches and best among them

Sam
  • 163
  • 1
  • 11

2 Answers2

15

DataFetcher is from graphql-java library , the only GraphQL Java implementation that I known in Java world so far.

GraphQLResolver is from another library called graphql-java-tools which is built on top of graphql-java . You can think that it provides a way which allow you to build a GraphQL server in a more high level way or a way that you may find more convenient. At the end , GraphQLResolver will somehow invoke DataFetcher#get() for resolving the value for a field.

An similar analogy in Spring is that graphql-java like Servlet while graphql-java-tools like SpringMVC.

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • I prefer using GraphQLResolver over DataFetcher because there is less boilerplate code involved while using resolvers instead of DataFetchers. – Rajshree Rai Feb 09 '23 at 07:24
0

The term "resolver" is a general GraphQL term and is agnostic of any specific GraphQL implementation/framework/language. Each field in GraphQL is backed by a function called the resolver which is provided by the GraphQL server developer. In short, the resolver is the first logic hit to map any specific field to any specific response.

The Netflix DGS library is now open source (as of late 2020) and it introduced "DataFetchers". DataFetchers, in the DGS world, are simply a DGS-specific way of implementing resolvers.

Reading:

Matt C.
  • 2,330
  • 3
  • 22
  • 26