GraphQL APIs with Spring Boot · 강의

열거형과 사용자 지정 스칼라 타입

고정 값 집합을 위한 열거형과 Spring Boot에서 날짜 및 URL 같은 도메인 타입을 위한 사용자 지정 스칼라를 정의하여 기본 제공 타입을 넘어 GraphQL 스키마를 확장하세요.

레슨 4/413개 단계

열거형과 사용자 지정 스칼라 타입은(는) CoddyKit의 무료 GraphQL APIs with Spring Boot 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 GraphQL APIs with Spring Boot 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. GraphQL APIs with Spring Boot 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

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.

무료로 시작

AI 튜터와 함께 GraphQL APIs with Spring Boot을(를) 배우세요 — 무료

브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.

코스
12
레슨
48

자주 묻는 질문

“열거형과 사용자 지정 스칼라 타입” 강의는 무료인가요?

네 — “열거형과 사용자 지정 스칼라 타입” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 GraphQL APIs with Spring Boot 강의 전체를 잠금 해제할 수 있습니다. GraphQL APIs with Spring Boot 강의에는 총 4개의 강의가 포함되어 있습니다.

“열거형과 사용자 지정 스칼라 타입”에서 뭘 배우나요?

고정 값 집합을 위한 열거형과 Spring Boot에서 날짜 및 URL 같은 도메인 타입을 위한 사용자 지정 스칼라를 정의하여 기본 제공 타입을 넘어 GraphQL 스키마를 확장하세요. 브라우저에서 직접 실행하는 실습 코드로 GraphQL APIs with Spring Boot을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

GraphQL APIs with Spring Boot을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 GraphQL APIs with Spring Boot은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.

“열거형과 사용자 지정 스칼라 타입” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 GraphQL APIs with Spring Boot 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 GraphQL APIs with Spring Boot 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 중첩 객체 및 관계 모델링
  2. 인터페이스와 유니온 타입 구현
  3. 변이에 입력 타입 활용하기
  4. 열거형과 사용자 지정 스칼라 타입
← GraphQL APIs with Spring Boot(으)로 돌아가기