@nestjs/graphql#Scalar TypeScript Examples
The following examples show how to use
@nestjs/graphql#Scalar.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: hash.scalar.ts From nestjs-mercurius with MIT License | 6 votes |
@Scalar('Hash', () => HashScalar)
export class HashScalar implements CustomScalar<string, string> {
description = 'Hash scalar';
parseValue(value: string): string {
console.log('hash parseV');
return crypto
.createDecipheriv(algo, key, iv)
.update(value, 'base64')
.toString();
}
serialize(value: string): string {
console.log('hash serialize');
return crypto
.createCipheriv(algo, key, iv)
.update(value)
.toString('base64');
}
parseLiteral(ast: ValueNode): string {
console.log('hash parseL');
if (ast.kind === Kind.STRING) {
return this.parseValue(ast.value);
}
return null;
}
}
Example #2
Source File: date.scalar.ts From nestjs-mercurius with MIT License | 6 votes |
@Scalar('Date')
export class DateScalar {
description = 'Date custom scalar type';
parseValue(value) {
return new Date(value); // value from the client
}
serialize(value) {
return value.getTime(); // value sent to the client
}
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
return parseInt(ast.value, 10); // ast value is always in string format
}
return null;
}
}
Example #3
Source File: global-id.scalar.ts From nestjs-relay with MIT License | 6 votes |
@Scalar('ID', typeResolvedGlobalId)
export class GlobalIdScalar implements CustomScalar<string, ResolvedGlobalId> {
parseValue(value: string): ResolvedGlobalId {
const { id, type } = fromGlobalId(value);
if (!id || !type) {
throw new GraphQLError(`Invalid ID: ${value}`);
}
return new ResolvedGlobalId({ type, id });
}
serialize(value: ResolvedGlobalId): string {
return toGlobalId(value.type, value.id);
}
parseLiteral(ast: ValueNode): ResolvedGlobalId {
if (ast.kind !== Kind.STRING) {
throw new GraphQLError(`Invalid ID type: ${ast.kind}`);
}
const { id, type } = fromGlobalId(ast.value);
if (!id || !type) {
throw new GraphQLError(`Invalid ID: ${ast.value}`);
}
return new ResolvedGlobalId({ type, id });
}
}