Skip to content

Commit 6d4a0a8

Browse files
committed
Fill up missing docs for #[graphql_interface] macro (#682)
1 parent b1a0366 commit 6d4a0a8

File tree

3 files changed

+390
-24
lines changed

3 files changed

+390
-24
lines changed

docs/book/content/types/interfaces.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ trait Character {
235235

236236
### Custom context
237237

238-
If a context is required in a trait method to resolve a [GraphQL interface][1] field, specify it as an argument.
238+
If a [`Context`][6] is required in a trait method to resolve a [GraphQL interface][1] field, specify it as an argument.
239239

240240
```rust
241241
# extern crate juniper;
@@ -288,22 +288,21 @@ impl Character for Human {
288288

289289
### Using executor and explicit generic scalar
290290

291-
If an executor is required in a trait method to resolve a [GraphQL interface][1] field, specify it as an argument.
291+
If an [`Executor`][4] is required in a trait method to resolve a [GraphQL interface][1] field, specify it as an argument.
292292

293293
This requires to explicitly parametrize over [`ScalarValue`][3], as [`Executor`][4] does so.
294294

295295
```rust
296296
# extern crate juniper;
297297
use juniper::{graphql_interface, Executor, GraphQLObject, LookAheadMethods as _, ScalarValue};
298298

299-
#[graphql_interface(for = Human, Scalar = S)] // notice specifying scalar as existing type parameter
299+
#[graphql_interface(for = Human, Scalar = S)] // notice specifying `ScalarValue` as existing type parameter
300300
trait Character<S: ScalarValue> {
301301
// If a field argument is named `executor`, it's automatically assumed
302302
// as an executor argument.
303303
async fn id<'a>(&self, executor: &'a Executor<'_, '_, (), S>) -> &'a str
304304
where
305305
S: Send + Sync; // required by `#[async_trait]` transformation ¯\_(ツ)_/¯
306-
307306

308307
// Otherwise, you may mark it explicitly as an executor argument.
309308
async fn name<'b>(
@@ -343,7 +342,7 @@ impl<S: ScalarValue> Character<S> for Human {
343342

344343
### Downcasting
345344

346-
By default, the [GraphQL interface][1] value is downcast to one of its implementer types via matching the enum variant or downcasting the trait object (if `dyn` is used).
345+
By default, the [GraphQL interface][1] value is downcast to one of its implementer types via matching the enum variant or downcasting the trait object (if `dyn` macro argument is used).
347346

348347
However, if some custom logic is needed to downcast a [GraphQL interface][1] implementer, you may specify either an external function or a trait method to do so.
349348

@@ -406,6 +405,8 @@ fn get_droid<'db>(ch: &CharacterValue, db: &'db Database) -> Option<&'db Droid>
406405
# fn main() {}
407406
```
408407

408+
The attribute syntax `#[graphql_interface(on ImplementerType = resolver_fn)]` follows the [GraphQL syntax for downcasting interface implementer](https://spec.graphql.org/June2018/#example-5cc55).
409+
409410

410411

411412

@@ -424,7 +425,7 @@ trait Character {
424425
}
425426

426427
#[derive(GraphQLObject)]
427-
#[graphql(Scalar = DefaultScalarValue)]
428+
#[graphql(impl = CharacterValue, Scalar = DefaultScalarValue)]
428429
struct Human {
429430
id: String,
430431
home_planet: String,
@@ -437,6 +438,7 @@ impl Character for Human {
437438
}
438439

439440
#[derive(GraphQLObject)]
441+
#[graphql(impl = CharacterValue, Scalar = DefaultScalarValue)]
440442
struct Droid {
441443
id: String,
442444
primary_function: String,
@@ -460,3 +462,4 @@ impl Character for Droid {
460462
[3]: https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html
461463
[4]: https://docs.rs/juniper/latest/juniper/struct.Executor.html
462464
[5]: https://spec.graphql.org/June2018/#sec-Objects
465+
[6]: https://docs.rs/juniper/0.14.2/juniper/trait.Context.html

docs/book/content/types/unions.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Unions
22
======
33

4-
From the server's point of view, [GraphQL unions][1] are similar to interfaces - the only exception is that they don't contain fields on their own.
4+
From the server's point of view, [GraphQL unions][1] are somewhat similar to [interfaces][5] - the main difference is that they don't contain fields on their own.
55

6-
For implementing [GraphQL unions][1] Juniper provides:
6+
The most obvious and straightforward way to represent a [GraphQL union][1] in Rust is enum. However, we also can do so either with trait or a regular struct. That's why, for implementing [GraphQL unions][1] Juniper provides:
77
- `#[derive(GraphQLUnion)]` macro for enums and structs.
88
- `#[graphql_union]` for traits.
99

@@ -288,7 +288,7 @@ impl Character for Droid {
288288

289289
### Custom context
290290

291-
If a context is required in a trait method to resolve a [GraphQL union][1] variant, specify it as an argument.
291+
If a [`Context`][6] is required in a trait method to resolve a [GraphQL union][1] variant, specify it as an argument.
292292

293293
```rust
294294
# #![allow(unused_variables)]
@@ -487,3 +487,5 @@ enum Character {
487487

488488
[1]: https://spec.graphql.org/June2018/#sec-Unions
489489
[2]: https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html
490+
[5]: https://spec.graphql.org/June2018/#sec-Interfaces
491+
[6]: https://docs.rs/juniper/0.14.2/juniper/trait.Context.html

0 commit comments

Comments
 (0)