graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

returning null for a custom scalar's serialize

Open cnwangjie opened this issue 4 years ago • 7 comments

There is an old issue #1057 for that.

It has been fixed in #1104. But It does not work for now (v16.3.0).

https://github.com/graphql/graphql-js/blob/b38429f4d46b17f23d15406a2ca0d1eb201864b6/src/execution/execute.ts#L783-L795

@leebyron Is it a mistake or as expected?

cnwangjie avatar Apr 11 '22 10:04 cnwangjie

Hi @IvanGoncharov,

Since this issue is unresponsive for a long time. I noticed that you are a recent committer for this project. May I ask what you think about this issue?

Due to this problem. I cannot returns a null value manually for a resolver. And the void scalar like this doesn't work.

I really hope this can be fixed if it's not intentional.

cnwangjie avatar May 06 '22 12:05 cnwangjie

@cnwangjie Spec says: https://spec.graphql.org/draft/#sel-IANRLHHCDCECCGBke

This internal method must return a valid value for the type and not null.

"internal method" is serialize in graphql-js API.

So it's intentional. All fields in GraphQL are nullable by default which makes it impossible to distinguish between null as GraphQL null and null produced by a scalar. And for input types, it is even worse.

IvanGoncharov avatar May 12 '22 15:05 IvanGoncharov

Hey @IvanGoncharov, what's the recommended workaround for this?

Yifeng-Sigma avatar Oct 25 '23 17:10 Yifeng-Sigma

@Yifeng-Sigma did you find a workaround for this?

ylaguna avatar Nov 24 '23 10:11 ylaguna

@Yifeng-Sigma did you find a workaround for this?

No, probably I shouldn't use scalar if it's nullable.

Yifeng-Sigma avatar Nov 24 '23 14:11 Yifeng-Sigma

I just ran into this in production after upgrading to graphql 16.x and it appears to be a regression to me.

We absolutely want our custom Scalar to decide whether to return null or not – if it's a nullable field that should be valid.

ephemer avatar May 16 '24 16:05 ephemer

This is a bit of an unexpected and very annoying behavior, even if it's intentional, as it means you can't really use custom Scalars on nullable fields.

The best workaround seems to be to return an empty string, but it is of my opinion that such behavior should be reavaliated.

Sharkaon avatar Aug 30 '24 12:08 Sharkaon