Modularização de esquemas com extensões de tipos
Mantenha grandes esquemas GraphQL fáceis de manter dividindo-os em vários arquivos SDL e estendendo tipos raiz compartilhados com a palavra-chave extend no Spring Boot.
Modularização de esquemas com extensões de tipos é uma aula grátis de GraphQL APIs with Spring Boot no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de GraphQL APIs with Spring Boot, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de GraphQL APIs with Spring Boot inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
The Monolithic Schema Problem
As an API grows, a single schema.graphqls file becomes thousands of lines, hard to navigate and prone to merge conflicts.
Modularization splits the schema by feature so each team owns a focused slice.
Modularization vs Stitching
This is different from schema stitching. Stitching merges schemas from separate services. Modularization splits one service's schema into many files that load into the same runtime.
Multiple SDL Files in Spring
Spring for GraphQL automatically loads every .graphqls file under src/main/resources/graphql/ and merges them into one schema.
graphql/
book.graphqls
author.graphqls
review.graphqlsThe Single Root Problem
GraphQL allows only one Query type. If two files both declare type Query, the schema fails to build with a duplicate-type error.
Extending the Root Type
Declare type Query once, then use extend type Query in other files to add fields. The pieces merge into one root.
# book.graphqls
type Query { books: [Book!]! }
# author.graphqls
extend type Query { authors: [Author!]! }Extending Object Types
extend works on any object type, not just roots. A reviews module can add a field to Book without editing the book file.
# review.graphqls
extend type Book {
reviews: [Review!]!
}Resolvers Stay Modular Too
Each module gets its own controller. The extended field on Book is resolved by a @SchemaMapping in the review module's controller.
@Controller
public class ReviewController {
@SchemaMapping(typeName = "Book")
public List<Review> reviews(Book book) {
return reviewService.forBook(book.getId());
}
}Sharing Common Types
Cross-cutting types like PageInfo or shared enums live in a common.graphqls file. Every module references them without redefining.
# common.graphqls
type PageInfo {
hasNextPage: Boolean!
endCursor: String
}Organizing by Feature
Group SDL files and controllers by feature, not by GraphQL kind. Keep book.graphqls next to BookController mentally, so a feature change touches one cohesive area.
Validation at Startup
Spring assembles and validates the merged schema at startup. If an extend references a type that does not exist, the application fails fast with a clear error, catching mistakes early.
Best Practices
Keep modular schemas healthy:
- Declare each root type once, extend elsewhere
- One SDL file and controller per feature
- Centralize shared types in a common file
- Let startup validation guard your merges
Quick Check
Test your schema modularization knowledge.
Recap
You modularized a large schema:
- Split SDL into multiple feature files Spring auto-merges
- Declare root types once, add fields with
extend - Extend any object type from another module
- Centralize shared types and keep resolvers modular
Modular schemas scale cleanly across teams and features.
Perguntas Frequentes
A aula “Modularização de esquemas com extensões de tipos” é grátis?
Sim — o texto completo de “Modularização de esquemas com extensões de tipos” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de GraphQL APIs with Spring Boot, atualize para CoddyKit PRO. O curso de GraphQL APIs with Spring Boot inclui 4 aulas no total.
O que vou aprender em “Modularização de esquemas com extensões de tipos”?
Mantenha grandes esquemas GraphQL fáceis de manter dividindo-os em vários arquivos SDL e estendendo tipos raiz compartilhados com a palavra-chave extend no Spring Boot. Você pratica GraphQL APIs with Spring Boot com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar GraphQL APIs with Spring Boot?
Nenhuma experiência prévia é necessária. GraphQL APIs with Spring Boot no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Modularização de esquemas com extensões de tipos”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de GraphQL APIs with Spring Boot?
Sim. Cada aula de GraphQL APIs with Spring Boot inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Construindo Diretivas Personalizadas
- Fundamentos da Composição de Esquemas
- Mesclando Vários Esquemas GraphQL
- Modularização de esquemas com extensões de tipos