0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

NestJS+GraphQLでISO8601DateTimeを使う

Posted at

Apollo Routerを使う場合、一致する型は完全に揃える必要がある(コメントまで含めて)

src/iso8601-date-time.scalar.ts
import { Scalar, CustomScalar } from '@nestjs/graphql';
import { Kind, ValueNode } from 'graphql';

@Scalar('ISO8601DateTime')
export class ISO8601DateTime implements CustomScalar<string, Date> {
  description = 'An ISO 8601-encoded datetime';

  parseValue(value: string): Date {
    return new Date(value); // Value from the client input variables
  }

  serialize(value: Date): string {
    return value.toISOString(); // Value sent to the client
  }

  parseLiteral(ast: ValueNode): Date | null {
    if (ast.kind === Kind.STRING) {
      return new Date(ast.value);
    }
    return null; // Invalid hard-coded value (not client input)
  }
}

src/app.module.ts
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { PostsModule } from './posts/posts.module';
import { UsersModule } from './users/users.module';
import { ISO8601DateTime } from './iso8601-date-time.scalar';

@Module({
  imports: [
    PostsModule,
    UsersModule,
    GraphQLModule.forRoot({
      autoSchemaFile: true,
      installSubscriptionHandlers: true,
    }),
  ],
  providers: [ISO8601DateTime],
})
export class AppModule {}

まぁAIが作ってくれたんですけどね

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?