Spreading fragment on a union type might cause runtime error because ref can be undefined
- Dominant language
- Rust
- Stars
- 19k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
lets say i have this union type defined in the schema
```
union U = A | B
type Query {
unionType: U
}
```
and used by a query
```
query SomeQuery {
unionType {
... on A {
...someFragment
}
... on B {
id
}
}
}
```
The Relay compiler wont flag any issue of the above query but if the `unionType` resolves to `B`, we will get a runtime error:
```Invariant Violation: Relay: Expected to receive an object where `...someFragment` was spread, but the fragment reference was not found`. This is most likely the result of:
- Forgetting to spread `someFragment` in `useFragment()`'s parent's fragment.
- Conditionally fetching `someFragment` but unconditionally passing a fragment reference prop to `useFragment()`. If the parent fragment only fetches the fragment conditionally - with e.g. `@include`, `@skip`, or inside a `... on SomeType { }` spread - then the fragment reference will not exist. In this case, pass `null` if the conditions for evaluating the fragment are not met (e.g. if the `@include(if)` value is false.)```.
There is no easy way to detect this issue until runtime so wondering if the relay compiler can give a warning message.
Contributor guide
Research direction
Start with Relay compiler validation for inline fragments on union types and reproduce the query using U, A, and B from the issue. Check how the compiler handles the fragment spread when the runtime value is B. Done means the compiler reports this unsafe spread before runtime, with coverage for the shown query shape.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100