Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

returning null for a custom scalar's serialize #3532

Closed
cnwangjie opened this issue Apr 11, 2022 · 5 comments
Closed

returning null for a custom scalar's serialize #3532

cnwangjie opened this issue Apr 11, 2022 · 5 comments
Labels

Comments

@cnwangjie
Copy link

There is an old issue #1057 for that.

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

function completeLeafValue(
returnType: GraphQLLeafType,
result: unknown,
): unknown {
const serializedResult = returnType.serialize(result);
if (serializedResult == null) {
throw new Error(
`Expected \`${inspect(returnType)}.serialize(${inspect(result)})\` to ` +
`return non-nullable value, returned: ${inspect(serializedResult)}`,
);
}
return serializedResult;
}

@leebyron Is it a mistake or as expected?

@cnwangjie
Copy link
Author

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.

@IvanGoncharov
Copy link
Member

@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.

@Yifeng-Sigma
Copy link

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

@ylaguna
Copy link

ylaguna commented Nov 24, 2023

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

@Yifeng-Sigma
Copy link

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants