-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add documentation for discriminator (#531)
Co-authored-by: Tushar Mathur <[email protected]>
- Loading branch information
1 parent
5d5328a
commit 6ba9a29
Showing
4 changed files
with
415 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
title: "@discriminate" | ||
description: The `@discriminate` directive is used to customize decoding of union types. | ||
slug: ../discriminate | ||
--- | ||
|
||
By default a union type expects an object with a wrapper key representing the value type. For example say we have the following GraphQL schema: | ||
|
||
```graphql showLineNumbers | ||
type Query { | ||
fooBar: [FooBar] | ||
@http(url: "https://api.example.com/foobar") | ||
} | ||
|
||
union FooBar = Foo | Bar | ||
|
||
type Foo { | ||
foo: String! | ||
} | ||
|
||
type Bar { | ||
bar: String! | ||
} | ||
``` | ||
|
||
The API is expected to respond with an object that is wrapped with a key representing the type of the value. For example for `Foo` the response should look like: | ||
|
||
```json | ||
[ | ||
// API Response | ||
{"Foo": {"foo": "Hello"}}, | ||
{"Bar": {"bar": "World"}} | ||
] | ||
``` | ||
|
||
:::note | ||
The **key** is always case sensitive and should match the type name. | ||
::: | ||
|
||
This allows Tailcall to correctly decode the response and resolve with the exact variant of the union type. However its also a common practice to have a special field to specify the type. For example: | ||
|
||
```json | ||
[ | ||
{"type": "Foo", "foo": "Hello"}, | ||
{"type": "Boo", "bar": "World"} | ||
] | ||
``` | ||
|
||
This can be achieved by modifying the schema to leverage the `@discriminate` directive: | ||
|
||
```graphql {4} | ||
type Query { | ||
fooBar: FooBar | ||
@http(url: "https://api.example.com/foobar") | ||
@discriminate | ||
} | ||
``` | ||
|
||
The `@discriminate` directive is used to indicate explicitly that the union type should be resolved using a discriminator field. | ||
|
||
The directive can be further customized by providing the discriminator field `name`: | ||
|
||
```graphql {4} | ||
type Query { | ||
fooBar: FooBar | ||
@http(url: "https://api.example.com/foobar") | ||
@discriminate(name: "ty") | ||
} | ||
``` | ||
|
||
In this case the API is expected to respond with an object that has a key `ty` representing the type of the value. For example for `Foo` the response should look like: | ||
|
||
```json | ||
{"ty": "Foo","foo": "Hello"} | ||
{"ty": "Bar","bar": "World"} | ||
``` | ||
|
||
:::note | ||
The value of the discriminator field should match the type name in a case sensitive manner. | ||
::: | ||
|
||
Great! Congratulations on learning how to use the `@discriminate` directive to customize decoding of union types. Now you can confidently work with union types in your GraphQL schema. 🎉 |
Oops, something went wrong.