@nestjs/graphql#CustomScalar TypeScript Examples

The following examples show how to use @nestjs/graphql#CustomScalar. 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 vote down vote up
@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: global-id.scalar.ts    From nestjs-relay with MIT License 6 votes vote down vote up
@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 });
  }
}