facebook / facebook/relay

typename discriminator not properly set if fragment on object type is spread onto union

Open
#4,886 3 comments 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

```ts
graphql`
fragment someUnion on Union {
__typename
...object1
...object2
}
`;

graphql`
fragment object1 on Object1 {
field1
}
`;

graphql`
fragment object2 on Object2 {
field2
}
`;
```

The above fragments currently leads to the following TypeScript definition for the `someUnion` fragment:

```ts
export type someUnion$data = {
readonly __typename: string;
readonly " $fragmentSpreads": FragmentRefs<"object1" | "object2">;
readonly " $fragmentType": "someUnion";
};
```

I would've expected it to be the following:

```ts
export type someUnion$data = {
readonly __typename: "Object1";
readonly " $fragmentSpreads": FragmentRefs<"object1">;
readonly " $fragmentType": "someUnion";
} | {
readonly __typename: "Object2";
readonly " $fragmentSpreads": FragmentRefs<"object2">;
readonly " $fragmentType": "someUnion";
} | {
readonly __typename: "%other";
readonly " $fragmentType": "someUnion";
};
```

To achieve the same result you currently have to define your fragment like this:

```graphql
fragment someUnion on Union {
__typename
... on Object1 {
...object1
}
... on Object2 {
...object2
}
}
```

Having to use an additional inline fragment seems unnecessary, since the fragments are already defined on a concrete object type.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.