# Scalars

## Scalars

GraphQL에는 `Int`, `Float`, `String`, `Boolean` 및 `ID`와 같은 기본 유형이 포함됩니다. 그러나 때때로 사용자 정의 원자 데이터 유형 (예: `날짜`)을 지원해야 할 수도 있습니다.

## Schema first

사용자 지정 스칼라를 정의하려면 (스칼라에 대한 자세한 내용은 [여기](https://www.apollographql.com/docs/graphql-tools/scalars.html)) 다음과 같이 형식 정의와 전용 리졸버를 만들어야합니다. 여기서는 공식 문서에서와 같이 데모 목적으로 `graphql-type-json` 패키지를 가져옵니다. 이 npm 패키지는 `JSON` GraphQL 스칼라 유형을 정의합니다. 먼저 패키지를 설치합니다:

```bash
$ npm i --save graphql-type-json
```

패키지가 설치되면 커스텀 리졸버를 `forRoot()`메소드에 전달해야 합니다 :

```typescript
import * as GraphQLJSON from 'graphql-type-json';

@Module({
  imports: [
    GraphQLModule.forRoot({
      typePaths: ['./**/*.graphql'],
      resolvers: { JSON: GraphQLJSON },
    }),
  ],
})
export class ApplicationModule {}
```

이제 타입 정의에 `JSON` 스칼라를 사용할 수 있습니다 :

```java
scalar JSON

type Foo {
  field: JSON
}
```

스칼라 형식을 정의하는 또 다른 형태는 간단한 클래스를 만드는 것입니다. `Date` 유형으로 스키마를 향상시키고 싶다고 가정 해 봅시다.

```typescript
import { Scalar, CustomScalar } from '@nestjs/graphql';
import { Kind, ValueNode } from 'graphql';

@Scalar('Date')
export class DateScalar implements CustomScalar<number, Date> {
  description = 'Date custom scalar type';

  parseValue(value: number): Date {
    return new Date(value); // value from the client
  }

  serialize(value: Date): number {
    return value.getTime(); // value sent to the client
  }

  parseLiteral(ast: ValueNode): Date {
    if (ast.kind === Kind.INT) {
      return new Date(ast.value);
    }
    return null;
  }
}
```

이후, 공급자로 `DateScalar`를 등록해야합니다.

```typescript
@Module({
  providers: [DateScalar],
})
export class CommonModule {}
```

이제 타입 정의에 `Date` 스칼라를 사용할 수 있습니다.

```java
scalar Date
```

## Code first

날짜 스칼라를 만들려면 간단히 새 클래스를 만드십시오.

```typescript
import { Scalar, CustomScalar } from '@nestjs/graphql';
import { Kind, ValueNode } from 'graphql';

@Scalar('Date', type => Date)
export class DateScalar implements CustomScalar<number, Date> {
  description = 'Date custom scalar type';

  parseValue(value: number): Date {
    return new Date(value); // value from the client
  }

  serialize(value: Date): number {
    return value.getTime(); // value sent to the client
  }

  parseLiteral(ast: ValueNode): Date {
    if (ast.kind === Kind.INT) {
      return new Date(ast.value);
    }
    return null;
  }
}
```

준비가되면 `DateScalar`를 공급자로 등록하십시오.

```typescript
@Module({
  providers: [DateScalar],
})
export class CommonModule {}
```

이제 클래스에서 날짜 유형을 사용할 수 있습니다.

```typescript
@Field()
creationDate: Date;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jakekwak.gitbook.io/nestjs/graphql/scalars.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
