Skip to content

Commit 6db1a34

Browse files
authored
document the exhaustiveness directive (#576)
1 parent 729a368 commit 6db1a34

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# master
22

3+
- Add support for `@exhaustive` - a directive to trigger exhaustiveness checks for unions at the GraphQL operation level.
4+
35
# 3.3.0
46

57
- Add support for top level `@catch` on fragments on unions.

packages/relay

Submodule relay updated 30 files

rescript-relay-documentation/docs/getting-started.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ module.exports = {
8282
schema: "./schema.graphql", // Path to the schema.graphql you've exported from your API. Don't know what this is? It's a saved introspection of what your schema looks like. You can run `npx get-graphql-schema http://path/to/my/graphql/server > schema.graphql` in your root to generate it
8383
artifactDirectory: "./src/__generated__", // The directory where all generated files will be emitted
8484

85+
// Enable this if you want mutations that return unions to always force selecting all union members. Read more in the docs on unions.
86+
autoExhaustiveMutations: true,
87+
8588
// You can add type definitions for custom scalars here.
8689
// Whenever a custom scalar is encountered, the type emitted will correspond to the definition defined here. You can then deal with the type as needed when accessing the data.
8790
customScalarTypes: {

rescript-relay-documentation/docs/unions.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,83 @@ Let's break down what's going on here:
8383
2. We fetch our query data, and we switch on `roomOwner` to make sure it's actually there in the data.
8484
3. We then switch again, but this time on the variant representing the union. This polymorphic variant will have each possible type of the union, and the fields selected on that type. It also adds `UnselectedUnionMember(string)` to every union, which will force you to handle _what happens if there's another member added to the union before you have time to update your app_. The `string` payload is the `__typename` of the retrieved member that wasn't selected. This is pretty neat way to ensure you gracefully handle your schema evolving.
8585

86+
## Exhaustiveness Checking with `@exhaustive`
87+
88+
RescriptRelay provides an `@exhaustive` directive that helps ensure you've selected all available union members in your GraphQL queries. This directive can be applied to fields or fragment definitions and will trigger exhaustiveness checks at compile time.
89+
This is useful when you there are unions in your schema where you'll want to be alerted at compile time that the server added new possible return values. This makes unions work just like enums in this regard.
90+
91+
### Usage
92+
93+
The `@exhaustive` directive is defined as:
94+
95+
```graphql
96+
@exhaustive(ignore: [String!], disabled: Boolean) on FIELD | FRAGMENT_DEFINITION
97+
```
98+
99+
Here's how to use it with our previous example:
100+
101+
```rescript
102+
/* RoomOwner.res */
103+
module Query = %relay(
104+
`
105+
query RoomOwnerQuery($roomId: ID!) {
106+
roomOwner(roomId: $roomId) @exhaustive {
107+
__typename
108+
109+
... on User {
110+
firstName
111+
lastName
112+
}
113+
114+
... on Group {
115+
name
116+
}
117+
}
118+
}
119+
`
120+
)
121+
```
122+
123+
When you use `@exhaustive`, RescriptRelay will check that you've included fragments for all possible union members. If you're missing any, you'll get a compile-time error telling you which union members you haven't selected.
124+
125+
### Parameters
126+
127+
- **`ignore: [String!]`** - An array of union member type names to ignore during exhaustiveness checking. Use this when you intentionally don't want to handle certain union members.
128+
129+
```rescript
130+
/* Ignore the Group type in exhaustiveness checking */
131+
roomOwner(roomId: $roomId) @exhaustive(ignore: ["Group"]) {
132+
__typename
133+
134+
... on User {
135+
firstName
136+
lastName
137+
}
138+
}
139+
```
140+
141+
- **`disabled: Boolean`** - Set to `true` to completely disable exhaustiveness checking for this field while keeping the directive for documentation purposes.
142+
143+
```rescript
144+
/* Temporarily disable exhaustiveness checking */
145+
roomOwner(roomId: $roomId) @exhaustive(disabled: true) {
146+
__typename
147+
148+
... on User {
149+
firstName
150+
lastName
151+
}
152+
}
153+
```
154+
155+
### Auto-enabling for Mutations
156+
157+
You can also configure RescriptRelay to automatically apply exhaustiveness checking to mutation fields that return unions. Add `autoExhaustiveMutations: true` to your Relay configuration to enable this behavior for any top-level mutation field that returns a union type.
158+
86159
## Wrapping up
87160

88161
And that's that! Keep the following in mind about unions and everything will be fine:
89162

90163
- Remember to select `__typename`
91164
- Remember to handle `UnselectedUnionMember`
165+
- Consider using `@exhaustive` to ensure you handle all union members

0 commit comments

Comments
 (0)