0Pricing
GraphQL APIs with Spring Boot · Lezione

Enum e tipi scalari personalizzati

Estenda lo schema GraphQL oltre i tipi integrati definendo enum per insiemi di valori fissi e scalari personalizzati per tipi di dominio come date e URL in Spring Boot.

Enum e tipi scalari personalizzati è una lezione GraphQL APIs with Spring Boot gratuita su CoddyKit. Questa è la lezione 4 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.

Beyond Built-in Types

GraphQL ships with scalars like Int, String, and Boolean. But real domains need more: a fixed set of statuses, a proper date type, or a validated email.

Enums and custom scalars let your schema express these precisely.

What Is a GraphQL Enum?

An enum restricts a field to a fixed list of named values. Clients can only send or receive one of those values, and tooling can offer autocompletion.

enum OrderStatus {
  PENDING
  SHIPPED
  DELIVERED
  CANCELLED
}

Using an Enum in a Type

Reference the enum like any other type. The field is now guaranteed to hold a valid status.

type Order {
  id: ID!
  status: OrderStatus!
}

Mapping Enums to Java

Spring for GraphQL maps a schema enum to a Java enum automatically when the names match exactly.

public enum OrderStatus {
    PENDING, SHIPPED, DELIVERED, CANCELLED
}

Why Custom Scalars?

The built-in scalars are limited. Representing a date as a String loses meaning and validation. A custom scalar defines how a domain value is serialized, deserialized, and validated.

Declaring a Scalar in the Schema

First declare the scalar name in your SDL, then use it on fields.

scalar DateTime

type Event {
  id: ID!
  startsAt: DateTime!
}

Using a Pre-built Scalar Library

The graphql-java-extended-scalars library provides ready-made scalars for DateTime, Date, URL, and more, so you do not write them by hand.

// build.gradle
implementation 'com.graphql-java:graphql-java-extended-scalars:21.0'

Registering a Scalar in Spring

Wire the scalar into the runtime with a RuntimeWiringConfigurer bean so the schema knows how to handle it.

@Bean
public RuntimeWiringConfigurer scalars() {
    return wiring -> wiring.scalar(ExtendedScalars.DateTime);
}

Writing Your Own Scalar

For a domain type you can define a GraphQLScalarType with a custom Coercing implementation controlling parse and serialize logic.

GraphQLScalarType.newScalar()
    .name("Email")
    .coercing(new EmailCoercing())
    .build();

Validation Inside Coercing

A scalar's Coercing can reject invalid input by throwing a CoercingParseValueException, giving you type-level validation for free.

if (!value.toString().contains("@")) {
    throw new CoercingParseValueException("Invalid email");
}

Best Practices

Use these types wisely:

  • Prefer enums over free-form strings for fixed sets
  • Reuse library scalars before writing your own
  • Keep Coercing logic pure and predictable
  • Document each custom scalar's expected format

Quick Check

Test your knowledge of enums and scalars.

Recap

You extended your schema's type system:

  • Enums restrict fields to a fixed value set
  • They map automatically to Java enums by name
  • Custom scalars model domain types like dates and emails
  • Use library scalars or write a Coercing for your own
  • Register scalars via RuntimeWiringConfigurer

Richer types make your API safer and more expressive.

Domande Frequenti

La lezione «Enum e tipi scalari personalizzati» è gratuita?

Sì — il testo completo di «Enum e tipi scalari 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 «Enum e tipi scalari personalizzati»?

Estenda lo schema GraphQL oltre i tipi integrati definendo enum per insiemi di valori fissi e scalari personalizzati per tipi di dominio come date e URL in Spring Boot. 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 4 di 4.

Quanto tempo richiede la lezione «Enum e tipi scalari 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

  1. Modellare oggetti annidati e relazioni
  2. Implementare interfacce e tipi union
  3. Sfruttare i tipi input per le mutation
  4. Enum e tipi scalari personalizzati
← Torna a GraphQL APIs with Spring Boot