Modularyzacja schematu za pomocą rozszerzeń typów
Utrzymają Państwo duże schematy GraphQL w dobrym stanie, dzieląc je na wiele plików SDL i rozszerzając współdzielone typy główne za pomocą słowa kluczowego extend w Spring Boot.
Modularyzacja schematu za pomocą rozszerzeń typów to bezpłatna lekcja GraphQL APIs with Spring Boot na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej GraphQL APIs with Spring Boot, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs GraphQL APIs with Spring Boot zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Modularyzacja schematu za pomocą rozszerzeń typów” jest bezpłatna?
Tak — pełny tekst „Modularyzacja schematu za pomocą rozszerzeń typów” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu GraphQL APIs with Spring Boot, przejdź na CoddyKit PRO. Kurs GraphQL APIs with Spring Boot zawiera 4 lekcji w sumie.
Co nauczysz się w „Modularyzacja schematu za pomocą rozszerzeń typów”?
Utrzymają Państwo duże schematy GraphQL w dobrym stanie, dzieląc je na wiele plików SDL i rozszerzając współdzielone typy główne za pomocą słowa kluczowego extend w Spring Boot. Ćwiczysz GraphQL APIs with Spring Boot z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć GraphQL APIs with Spring Boot?
Nie wymagamy żadnego doświadczenia. GraphQL APIs with Spring Boot w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Modularyzacja schematu za pomocą rozszerzeń typów”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji GraphQL APIs with Spring Boot?
Tak. Każda lekcja GraphQL APIs with Spring Boot zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Tworzenie niestandardowych dyrektyw
- Podstawy Schema Stitching
- Scalanie wielu schematów GraphQL
- Modularyzacja schematu za pomocą rozszerzeń typów