0Pricing
GraphQL APIs with Spring Boot · Leçon

Modulariser un schéma avec des extensions de types

Gardez les grands schémas GraphQL faciles à maintenir en les répartissant dans plusieurs fichiers SDL et en étendant les types racine partagés avec le mot-clé extend dans Spring Boot.

Modulariser un schéma avec des extensions de types est une leçon GraphQL APIs with Spring Boot gratuite sur CoddyKit. Ceci est la leçon 4 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage GraphQL APIs with Spring Boot, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours GraphQL APIs with Spring Boot comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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.graphqls

The 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.

Questions Fréquemment Posées

La leçon « Modulariser un schéma avec des extensions de types » est-elle gratuite ?

Oui — le texte complet de « Modulariser un schéma avec des extensions de types » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours GraphQL APIs with Spring Boot, passe à CoddyKit PRO. Le cours GraphQL APIs with Spring Boot comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Modulariser un schéma avec des extensions de types » ?

Gardez les grands schémas GraphQL faciles à maintenir en les répartissant dans plusieurs fichiers SDL et en étendant les types racine partagés avec le mot-clé extend dans Spring Boot. Tu pratiques GraphQL APIs with Spring Boot avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer GraphQL APIs with Spring Boot ?

Aucune expérience préalable n'est requise. GraphQL APIs with Spring Boot sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 4 sur 4.

Combien de temps prend la leçon « Modulariser un schéma avec des extensions de types » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon GraphQL APIs with Spring Boot ?

Oui. Chaque leçon GraphQL APIs with Spring Boot inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Créer des directives personnalisées
  2. Principes fondamentaux de la composition de schémas
  3. Fusionner plusieurs schémas GraphQL
  4. Modulariser un schéma avec des extensions de types
← Retour à GraphQL APIs with Spring Boot