Definire tipi di dati personalizzati
Crei tipi di oggetti, tipi scalari ed enumerazioni personalizzati nello schema GraphQL per rappresentare i dati della Sua applicazione.
Definire tipi di dati personalizzati è una lezione GraphQL APIs with Spring Boot gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento GraphQL APIs with Spring Boot, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso GraphQL APIs with Spring Boot include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Definire tipi di dati personalizzati» è gratuita?
Sì — il testo completo di «Definire tipi di dati personalizzati» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso GraphQL APIs with Spring Boot, passa a CoddyKit PRO. Il corso GraphQL APIs with Spring Boot include 4 lezioni in totale.
Cosa imparerò in «Definire tipi di dati personalizzati»?
Crei tipi di oggetti, tipi scalari ed enumerazioni personalizzati nello schema GraphQL per rappresentare i dati della Sua applicazione. Eserciti GraphQL APIs with Spring Boot con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare GraphQL APIs with Spring Boot?
Non è richiesta alcuna esperienza precedente. GraphQL APIs with Spring Boot su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «Definire tipi di dati personalizzati»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione GraphQL APIs with Spring Boot?
Sì. Ogni lezione GraphQL APIs with Spring Boot include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Definire tipi di dati personalizzati
- Implementare i resolver dei dati
- Eseguire query GraphQL semplici
- Mutation GraphQL: modificare i dati