0Pricing
GraphQL APIs with Spring Boot · Lektion

Benutzerdefinierte Datentypen definieren

Erstellen Sie benutzerdefinierte Objekttypen, Skalartypen und Enums in Ihrem GraphQL-Schema, um die Daten Ihrer Anwendung abzubilden.

Benutzerdefinierte Datentypen definieren ist eine kostenlose GraphQL APIs with Spring Boot-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des GraphQL APIs with Spring Boot-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der GraphQL APIs with Spring Boot-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Benutzerdefinierte Datentypen definieren“ kostenlos?

Ja — der vollständige Text von „Benutzerdefinierte Datentypen definieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des GraphQL APIs with Spring Boot-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der GraphQL APIs with Spring Boot-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Benutzerdefinierte Datentypen definieren“?

Erstellen Sie benutzerdefinierte Objekttypen, Skalartypen und Enums in Ihrem GraphQL-Schema, um die Daten Ihrer Anwendung abzubilden. Du übst GraphQL APIs with Spring Boot mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um GraphQL APIs with Spring Boot zu starten?

Keine Vorkenntnisse erforderlich. GraphQL APIs with Spring Boot auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Benutzerdefinierte Datentypen definieren“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser GraphQL APIs with Spring Boot-Lektion Code schreiben und ausführen?

Ja. Jede GraphQL APIs with Spring Boot-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Benutzerdefinierte Datentypen definieren
  2. Daten-Resolver implementieren
  3. Einfache GraphQL-Abfragen ausführen
  4. GraphQL-Mutationen: Daten ändern
← Zurück zu GraphQL APIs with Spring Boot