facebook / facebook/relay

[Modern] Wrong flow type generated when embedding fragments - Non nullable become nullable

Open
#2,324 2 comments 1 reaction 0 assignees View on GitHub
wontfix
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

version: 1.4.1

I have a Component that does pagination and mutation. The values inside each edge of a connection are the same between the mutation (which returns a new edge) and the pagination.
The connection is a list of Friends inside a User.

To avoid duplication, I created a Repro_FriendListItemFragment that is referenced in the Repro_user fragment of the Pagination and in the Repro_createFriendMutation of the mutation. To make this work I need the @relay(mask:false) so that the Repro_FriendListItemFragment are visible in the Pagination and Mutation results (which is normal relay behaviour).

However the generated flow types for Repro_user are not correct. While all my properties in my schema are non-nullable, the node and cursor properties of the edges are marked as nullable.

When replacing the Repro_FriendListItemFragment with its content in the pagination and mutation queries I get proper flow type so I know that it is not due to some schema issue.

[Here is a reproducer](https://github.com/tanneess/relay-mask-false-type-error-reproducer)

In the linked reproducer, the generated flow type when running npm run relay for Repro_user.graphql.js reads
```
export type Repro_user = {|
+id: string;
+name: string;
+email: string;
+friends: {|
+edges: $ReadOnlyArray<{|
+node?: {|
+id: string;
+owner: {|
+name: string;
|};
|};
+cursor?: string;
|}>;
|};
|};
```

instead of
```
export type Repro_user = {|
+id: string;
+name: string;
+email: string;
+friends: {|
+edges: $ReadOnlyArray<{|
+node: {|
+id: string;
+owner: {|
+name: string;
|};
|};
+cursor: string;
|}>;
|};
|};
```

Contributor guide

Open the contributing guide

Research direction

Start by running npm run relay in the linked reproducer and inspect the generated Repro_user.graphql.js output. Compare the fragment-based pagination and mutation types with the inline-fragment version, then trace where node and cursor nullability is derived. Done means the generated fields remain non-nullable when the schema and inline version are non-nullable.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, javascript, react
Domain
frontend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.