Skip to content

Do not output undefined for nullable fields within @throwOnFieldError and @catch #4974

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tobias-tengler
Copy link
Contributor

@tobias-tengler tobias-tengler commented Apr 27, 2025

Since @throwOnFieldError or @catch will catch missing data errors, we can output nullable fields in the $data type as field: Type | null instead of field: Type | null | undefined (unless optional).

Closes #4971

Supersedes #4972

@tobias-tengler tobias-tengler force-pushed the exclude-undefined-from-nullable-types-in-semantic-context branch from eb5b277 to 8b282f3 Compare April 27, 2025 17:23
@tobias-tengler
Copy link
Contributor Author

@captbaritone I've opted into always generating the nullable types in the raw response type without undefined (unless optional of course), but I'm wondering if this change should be rolled out together with the potentially new @rawResponseType directive that always outputs the semantic type instead?

@tobias-tengler tobias-tengler changed the title Exclude undefined from nullable types in semantic context Remove unnecessary undefined in type generation of nullable fields / variables Apr 27, 2025
@captbaritone
Copy link
Contributor

@tobias-tengler Yeah, while I think you are correct that we should not allow optional types in raw response (since the server will never omit fields) in practice since they are currently typed that way it will be a major breaking change to update that behavior. At the very least we will need a feature flag, but I think bundling that flag (and therefor the PR) with a larger rethinking of raw response types makes more sense to me.

@tobias-tengler tobias-tengler force-pushed the exclude-undefined-from-nullable-types-in-semantic-context branch 3 times, most recently from 06de7f7 to ac450a8 Compare May 1, 2025 17:44
@tobias-tengler
Copy link
Contributor Author

@captbaritone That's what I thought. I've updated the PR to only contain the change that undefined is omitted for nullable types within @throwOnFieldError and @catch.

@tobias-tengler tobias-tengler changed the title Remove unnecessary undefined in type generation of nullable fields / variables Do not output undefined for nullable fields within @throwOnFieldError and @catch May 1, 2025
@captbaritone
Copy link
Contributor

I'm going to import this and see what breaking type changes is produces for us.

@facebook-github-bot
Copy link
Contributor

@captbaritone has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@tobias-tengler
Copy link
Contributor Author

tobias-tengler commented May 1, 2025

Interesting. At least with TypeScript it shouldn't be an issue, since the user defined type would have to be the same as Relay's current type (Type | null | undefined). Which means even if Relay is making the type narrower (Type | null), what we're assigning to is still broader. Playground
I've ran the type generation on our TypeScript code base and didn't encounter any issue.

Regarding Flow: The example you've made doesn't seem to reflect my changes. In your example the original Relay type is somehow just string | undefined and doesn't include null, which doesn't make much sense to me:

- +someField?: string
+ +someField: string | null

But looking at the snapshots, the original Relay type of a nullable field should be string | null | undefined (if I read up on the ?Type syntax correctly):

- +someField: ?string,
+ +someField: string | null,

Do you know why your original types do not include null for a nullable field?

@captbaritone
Copy link
Contributor

Hmm. You're right. I think my example is not quite representative. Let me try to get one that's a direct reduction of the issue I saw internally.

@tobias-tengler tobias-tengler force-pushed the exclude-undefined-from-nullable-types-in-semantic-context branch from ed92970 to 33f2f9e Compare May 5, 2025 09:09
@tobias-tengler
Copy link
Contributor Author

@captbaritone did you manage to find a better example?

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

Successfully merging this pull request may close these issues.

Do not type nullable fields with undefined within @throwOnFieldError and @catch
3 participants