Skip to content

Support TResult | null for TypedDocumentNode when top-level data may be null #10391

@y-hsgw

Description

@y-hsgw

Is your feature request related to a problem? Please describe.

According to the GraphQL specification, it's possible for the top-level data field in the response to be null in case of certain errors (e.g. when a non-nullable root field throws an unexpected error).

Describe the solution you'd like

I’d like to have an option added to the @graphql-codegen/typescript plugin that allows it to generate TResult | null in operation result types.

graphql.ts

export const GetHogeDocument = {
...
} as TypedDocumentNode<GetHogeQuery | null, GetHogeQueryVariables>

instead of the current:

export const GetHogeDocument = {
...
} as TypedDocumentNode<GetHogeQuery, GetHogeQueryVariables>

This would allow safer and more accurate typing for clients where data: null is a valid result according to GraphQL spec.

Describe alternatives you've considered

No response

Any additional important details?

I asked the urql team about this, and they suggested handling it by altering the TypedDocumentNode type generated/produced.

Reference: urql-graphql/urql#3803 (comment)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions