Schema-First Design and Type Mapping
Define a GraphQL schema and map types, queries, and mutations to Java controller methods.
Schema-First in Spring for GraphQL
Spring for GraphQL is schema-first: you describe your API in a .graphql Schema Definition Language (SDL) file, and your Java code maps onto it.
- The schema is the single source of truth for the API contract.
- Clients ask for exactly the fields they need.
- Your controllers provide the data behind each field.
By convention, Spring Boot auto-discovers .graphqls / .graphql files under src/main/resources/graphql/.
Defining Object Types in SDL
A GraphQL object type describes the shape of an entity. Each field has a name and a type.
ID,String,Int,Float,Booleanare the built-in scalars.- A trailing
!marks a field as non-null. [Type]denotes a list.
Place this in src/main/resources/graphql/schema.graphqls.
type Book {
id: ID!
title: String!
pageCount: Int
author: Author!
}
type Author {
id: ID!
name: String!
books: [Book!]!
}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