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
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.
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.
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.
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 .
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With