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が作ってくれたんですけどね