Définir des types de données personnalisés
Créez des types d'objets et des types scalaires personnalisés ainsi que des énumérations dans votre schéma GraphQL pour représenter les données de votre application.
Définir des types de données personnalisés est une leçon GraphQL APIs with Spring Boot gratuite sur CoddyKit. Ceci est la leçon 1 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.
Custom Types: Your Data's Blueprint
In GraphQL, data types are like blueprints for your application's information. They define the shape and kind of data that can be queried or modified.
While GraphQL provides some basic types, you'll often need to define your own to perfectly match your application's unique data structure.
GraphQL's Building Blocks
GraphQL has built-in scalar types like String, Int, Boolean, Float, and ID. These are fundamental, single-value types.
But real-world data is complex! This is where Object Types come in. They let you define objects with multiple fields, each having its own type.
Crafting Object Types
You define an object type using the type keyword in GraphQL's Schema Definition Language (SDL). Think of it as creating a custom class or struct.
Each field within an object type has a name and a type. Fields can be other object types, scalar types, or even lists of types.
Example: A 'Book' Object
Let's imagine we're building an API for a library. We'd need a way to represent a book. Here's how you might define a Book object type:
type Book {
id: ID!
title: String!
author: String
publicationYear: Int
}The ! after a type means the field is non-nullable – it must always have a value.
Beyond Basic Scalars
Sometimes, built-in scalars aren't specific enough. For example, a Date field could be a String, but what if you need to ensure a specific format?
Custom Scalar Types allow you to define your own scalar types with specific serialization, parsing, and validation rules. This ensures data consistency.
Declaring Custom Scalars
Defining a custom scalar in your schema is straightforward. You use the scalar keyword, followed by the name of your new scalar type.
Later, your Spring Boot application will need to provide the actual logic for handling this custom scalar's data transformations.
scalar Date
type Event {
id: ID!
name: String!
eventDate: Date!
}Enums for Fixed Values
Enum Types (short for 'enumeration') are special scalar types that represent a finite set of possible values. They're great for fields where you want to restrict choices to a predefined list.
Using enums makes your schema more self-documenting and helps prevent invalid data from being sent to your API.
Defining an Enum Type
For our library API, a book might have a specific status. We can define an enum for this:
enum BookStatus {
AVAILABLE
CHECKED_OUT
LOST
REPAIR
}Now, any field using BookStatus can only have one of these four values.
Combining Your Custom Types
Let's update our Book type to use our new BookStatus enum and a custom Date scalar.
scalar Date
enum BookStatus {
AVAILABLE
CHECKED_OUT
LOST
}
type Book {
id: ID!
title: String!
author: String
publicationYear: Int
status: BookStatus!
lastCheckedOut: Date
}This shows how custom types build a richer, more precise schema.
Check Your Understanding
Which of the following statements about GraphQL custom data types are TRUE?
Recap: Your Data's Foundation
You've learned how to define custom data types in GraphQL using SDL!
- Object Types (
type) structure complex data. - Custom Scalar Types (
scalar) handle specific data formats. - Enum Types (
enum) restrict field values to a finite set.
These tools are crucial for building a precise and robust GraphQL schema that accurately represents your application's domain.
Questions Fréquemment Posées
La leçon « Définir des types de données personnalisés » est-elle gratuite ?
Oui — le texte complet de « Définir des types de données personnalisés » 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 « Définir des types de données personnalisés » ?
Créez des types d'objets et des types scalaires personnalisés ainsi que des énumérations dans votre schéma GraphQL pour représenter les données de votre application. 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 1 sur 4.
Combien de temps prend la leçon « Définir des types de données personnalisés » ?
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
- Définir des types de données personnalisés
- Implémenter les résolveurs de données
- Exécuter des requêtes GraphQL simples
- Mutations GraphQL : modifier les données