Graphql: CustomScalar parseLiteral doesn't allow null return type

Created on 23 Apr 2019  路  3Comments  路  Source: nestjs/graphql

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


The samples show that you should return null from parseLiteral if the AST kind doesn't match the expected kind.

// From https://github.com/nestjs/nest/blob/master/sample/23-type-graphql/src/common/scalars/date.scalar.ts
parseLiteral(ast: any): Date {
  if (ast.kind === Kind.INT) {
    return new Date(ast.value);
  }
  return null;
}

Doing this with TS strictNullChecks enabled, results in an error.

Expected behavior


CustomScalar should allow parseLiteral to return null or undefined as demonstrated by the samples.

What is the motivation / use case for changing the behavior?


The pattern recommended by the samples should work under TS strict mode.

Environment


@nestjs/core: 6.1.1
@nestjs/graphql: 6.0.5
typescript: 3.4.4

Most helpful comment

Somehow related do this, eslint rule @typescript-eslint/no-explicit-any forces me to specify a type for ast, what should it be?

All 3 comments

Hi !

I had the same issue with my tsconfig.json file and the property _strictNullChecks_ set to true.
You can simply do this to resolve your issue :

parseLiteral(ast: any): Date | null {
  return ast.kind === Kind.INT ? new Date(ast.value) : null;
}

Because if you check this file : _https://github.com/nestjs/graphql/blob/master/lib/interfaces/custom-scalar.interface.ts_ you can see that the returned types of the methods of the interfaces are generics :

export interface CustomScalar<T, K> {
  description: string;
  parseValue(value: T): K;
  serialize(value: K): T;
  parseLiteral(ast): K;
}

I hope this helps you :)

Somehow related do this, eslint rule @typescript-eslint/no-explicit-any forces me to specify a type for ast, what should it be?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings