Data Fetchers and Argument Binding
Implement @QueryMapping and @MutationMapping handlers with argument and input binding.
What a Data Fetcher Is
In Spring for GraphQL, every field in your schema is resolved by a data fetcher (the GraphQL-Java term for a resolver). When a client asks for a field, the engine invokes the fetcher bound to it.
Instead of registering raw DataFetcher beans, Spring lets you write annotated controller methods. The framework maps each method to a schema field and handles argument binding, return values, and async wrapping for you.
@QueryMappingresolves a field under the rootQuerytype.@MutationMappingresolves a field under the rootMutationtype.@SchemaMappingresolves any field (including nested object fields).
The Schema Drives Everything
Spring for GraphQL is schema-first. You declare your types and operations in an SDL file under src/main/resources/graphql/ (for example schema.graphqls), and your controller methods bind to those field names.
Consider this schema. The book and books fields live under Query, so each needs a @QueryMapping handler.
type Query {
books: [Book!]!
book(id: ID!): Book
}
type Mutation {
addBook(input: AddBookInput!): Book!
}
type Book {
id: ID!
title: String!
pages: Int!
}
input AddBookInput {
title: String!
pages: Int!
}All lessons in this course
- Schema-First Design and Type Mapping
- Data Fetchers and Argument Binding
- Solving N+1 with Batch Loaders
- Subscriptions, Errors, and Schema Security