Skip to content

Commit f4887ba

Browse files
committed
Add documentation for the union helper macro
1 parent 8a51c33 commit f4887ba

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/macros/interface.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@ See the documentation for [`graphql_object!`][1] on the general item and type
1616
syntax. `graphql_interface!` requires an additional `instance_resolvers` item,
1717
and does _not_ support the `interfaces` item.
1818
19-
`instance_resolvers` is a list/lambda hybrid used to resolve the concrete
19+
`instance_resolvers` is a match like structure used to resolve the concrete
2020
instance type of the interface. It starts with a context argument and continues
21-
with an array of expressions, each resolving into an `Option<T>` of the possible
22-
instances:
21+
with a number of match arms; on the left side is the indicated type, and on the
22+
right an expression that resolve into `Option<T>` of the type indicated:
2323
2424
```rust,ignore
2525
instance_resolvers: |&context| {
26-
Human => context.get_human(self.id()), // returns Option<Human>
27-
Droid => context.get_droid(self.id()), // returns Option<Droid>
26+
&Human => context.get_human(self.id()), // returns Option<&Human>
27+
&Droid => context.get_droid(self.id()), // returns Option<&Droid>
2828
},
2929
```
3030
31-
Each item in the array will be executed in order when the concrete type is
32-
required.
31+
This is used for both the `__typename` field and when resolving a specialized
32+
fragment, e.g. `...on Human`. For `__typename`, the resolvers will be executed
33+
in order - the first one returning `Some` will be the determined type name. When
34+
resolving fragment type conditions, only the corresponding match arm will be
35+
executed.
3336
3437
## Example
3538
@@ -71,8 +74,8 @@ graphql_interface!(<'a> &'a Character: Database as "Character" |&self| {
7174
field id() -> &str { self.id() }
7275
7376
instance_resolvers: |&context| {
74-
Human => context.humans.get(self.id()),
75-
Droid => context.droids.get(self.id()),
77+
&Human => context.humans.get(self.id()),
78+
&Droid => context.droids.get(self.id()),
7679
}
7780
});
7881

src/macros/union.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/**
2+
Expose GraphQL unions
3+
4+
Like interfaces, mapping unions can be tricky in idiomatic Rust. Because of
5+
their similarity, the helper macros are similar, too: you provide a set of
6+
expressions that resolve the union into the actual concrete type.
7+
8+
## Syntax
9+
10+
See the documentation for [`graphql_object!`][1] on the general item and type
11+
syntax. `graphql_union!` supports only `description` and `interface_resolvers`
12+
items, no fields or interfaces can be declared.
13+
14+
See the documentation for [`graphql_interface!`][2] on the syntax for interface
15+
resolvers.
16+
17+
[1]: macro.graphql_object!.html
18+
[2]: macro.graphql_interface!.html
19+
*/
120
#[macro_export]
221
macro_rules! graphql_union {
322
( @as_item, $i:item) => { $i };

0 commit comments

Comments
 (0)