DataFetcher is the core interface of GraphQL Java that Spring for GraphQL builds on. It is a function that receives a DataFetchingEnvironment and returns the value of one field in your schema. The GraphQL engine calls a data fetcher for every field it needs to resolve, so your controllers and registrations ultimately become data fetchers.
In Spring for GraphQL you rarely implement the interface by hand. Annotated controllers such as @QueryMapping and @SchemaMapping are turned into data fetchers for you, while RuntimeWiringConfigurer lets you register them explicitly.
The interface comes from graphql.schema.DataFetcher<T> and has one method: T get(DataFetchingEnvironment environment) throws Exception. The result can be a plain value, a Mono, a Flux, a CompletableFuture or an object that the engine then resolves further, field by field.
The environment gives access to:
getArgument("name")getSource()getSelectionSet()DataLoader registry for batchingIf you register no fetcher for a field, GraphQL Java uses PropertyDataFetcher, which reads a property or a record component by name. That is why simple fields on your Java classes need no code.
Since Spring for GraphQL 1.0 the recommended style is annotated controllers. Given this schema:
you write:
Spring wraps each annotated method in a DataFetcher. The method name matches the field name, and author(Book book) receives the parent Book as its source argument. The title and id fields use PropertyDataFetcher automatically.
You can also implement the interface and register it with RuntimeWiringConfigurer, which is useful for dynamic wiring or for migrating existing GraphQL Java code.
Spring Data repositories can be exposed the same way. Annotating a repository with @GraphQlRepository and extending QuerydslPredicateExecutor or QueryByExampleExecutor lets Spring for GraphQL register data fetchers for queries over your entities.
A naive author fetcher runs once per book, so a list of 100 books triggers 100 author lookups. The fix is to batch with @BatchMapping, which uses a DataLoader internally.
The framework collects all books requested in one query, calls the method once, and distributes the results. Use this whenever a field loads related data.
| Concept | Responsibility |
|---|---|
DataFetcher | Returns the value of a single field |
DataLoader | Batches and caches many fetches into one call |
TypeResolver | Chooses the concrete GraphQL type for an interface or union |
Instrumentation | Observes or wraps execution for tracing and metrics |
Keep data fetchers thin: delegate to services, never block in a reactive application, and handle errors by throwing exceptions that a DataFetcherExceptionResolver converts into GraphQL errors.
Usually not. Annotated methods are adapted to data fetchers automatically. Implement it directly only for custom wiring.
Yes. GraphQL Java resolves a CompletableFuture, and Spring for GraphQL adapts Mono and Flux results for you.
GraphQL Java falls back to PropertyDataFetcher, which reads the property from the parent object, and returns null if it is missing.
Throw an exception and register a DataFetcherExceptionResolver (or use @GraphQlExceptionHandler in a controller) to map it to a GraphQL error.
A DataFetcher resolves one field of a GraphQL schema. In Spring for GraphQL you mostly get them through @QueryMapping, @SchemaMapping and @BatchMapping, and register them manually with RuntimeWiringConfigurer when needed. Use batching to avoid N+1 queries, and keep fetchers thin.