Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Datafetchers and Resolvers

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

like image 966
Sam Avatar asked Jun 12 '19 05:06

Sam


People also ask

What are field resolvers?

A resolver is a function that's responsible for populating the data for a single field in your schema. Whenever a client queries for a particular field, the resolver for that field fetches the requested data from the appropriate data source.

How do resolvers work in GraphQL?

Each field on each type is backed by a function called the resolver which is provided by the GraphQL server developer. When a field is executed, the corresponding resolver is called to produce the next value. If a field produces a scalar value like a string or number, then the execution completes.

What is DataFetchingEnvironment?

A DataFetchingEnvironment instance of passed to a DataFetcher as a execution context and its the place where you can find out information to help you resolve a data value given a graphql field input.

What is GraphQL DataFetcher?

GraphQL Java uses DataFetcher s to fetch data to include in the result of a query. More specifically, a DataFetcher retrieves the data for a single field when a query is executed. Every field has an assigned DataFetcher .


Video Answer


1 Answers

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.

like image 200
Ken Chan Avatar answered Nov 22 '22 22:11

Ken Chan