0Pricing
GraphQL APIs with Spring Boot · Aula

Definindo Tipos de Dados Personalizados

Crie tipos de objeto personalizados, tipos escalares e enumerações em seu esquema GraphQL para representar os dados da sua aplicação.

Definindo Tipos de Dados Personalizados é uma aula grátis de GraphQL APIs with Spring Boot no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de GraphQL APIs with Spring Boot, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de GraphQL APIs with Spring Boot inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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.

Perguntas Frequentes

A aula “Definindo Tipos de Dados Personalizados” é grátis?

Sim — o texto completo de “Definindo Tipos de Dados Personalizados” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de GraphQL APIs with Spring Boot, atualize para CoddyKit PRO. O curso de GraphQL APIs with Spring Boot inclui 4 aulas no total.

O que vou aprender em “Definindo Tipos de Dados Personalizados”?

Crie tipos de objeto personalizados, tipos escalares e enumerações em seu esquema GraphQL para representar os dados da sua aplicação. Você pratica GraphQL APIs with Spring Boot com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar GraphQL APIs with Spring Boot?

Nenhuma experiência prévia é necessária. GraphQL APIs with Spring Boot no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.

Quanto tempo leva a aula “Definindo Tipos de Dados Personalizados”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de GraphQL APIs with Spring Boot?

Sim. Cada aula de GraphQL APIs with Spring Boot inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Definindo Tipos de Dados Personalizados
  2. Implementando Resolvedores de Dados
  3. Executando Consultas GraphQL Simples
  4. Mutações GraphQL: alterando dados
← Voltar para GraphQL APIs with Spring Boot