From cb18c609890ed642282486b9b67d94d52104fac2 Mon Sep 17 00:00:00 2001 From: D1plo1d Date: Mon, 10 Aug 2020 23:21:15 -0400 Subject: [PATCH] Add GQLMergedObject to docs --- docs/en/src/SUMMARY.md | 1 + docs/en/src/define_complex_object.md | 20 ------------ docs/en/src/merging_objects.md | 48 ++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 docs/en/src/merging_objects.md diff --git a/docs/en/src/SUMMARY.md b/docs/en/src/SUMMARY.md index 02c44f57..6748557f 100644 --- a/docs/en/src/SUMMARY.md +++ b/docs/en/src/SUMMARY.md @@ -7,6 +7,7 @@ - [Object](define_complex_object.md) - [Context](context.md) - [Error handling](error_handling.md) + - [Merging Objects](merging_objects.md) - [Enum](define_enum.md) - [Interface](define_interface.md) - [Union](define_union.md) diff --git a/docs/en/src/define_complex_object.md b/docs/en/src/define_complex_object.md index 2590eac1..7893c128 100644 --- a/docs/en/src/define_complex_object.md +++ b/docs/en/src/define_complex_object.md @@ -33,23 +33,3 @@ impl MyObject { } } ``` - -## Implement Object multiple times for the same type - -Usually we can create multiple implementations for the same type in Rust, but due to the limitation of procedural macros, we can not create multiple Object implementations for the same type. For example, the following code will fail to compile. - -```rust -#[Object] -impl MyObject { - async fn field1(&self) -> i32 { - todo!() - } -} - -#[Object] -impl MyObject { - async fn field2(&self) -> i32 { - todo!() - } -} -``` diff --git a/docs/en/src/merging_objects.md b/docs/en/src/merging_objects.md new file mode 100644 index 00000000..bc94a416 --- /dev/null +++ b/docs/en/src/merging_objects.md @@ -0,0 +1,48 @@ +# Merging Objects + +Usually we can create multiple implementations for the same type in Rust, but due to the limitation of procedural macros, we can not create multiple Object implementations for the same type. For example, the following code will fail to compile. + +```rust +#[Object] +impl Query { + async fn users(&self) -> Vec { + todo!() + } +} + +#[Object] +impl Query { + async fn movies(&self) -> Vec { + todo!() + } +} +``` + +Instead, the `#[derive(GQLMergedObject)]` macro allows you to split an object's resolvers across multiple file by merging 2 or more `#[Object]` implementations into one. + +**Tip:** Every `#[Object]` needs a unique name even in a GQLMergedObject so make sure to give each object your merging it's own name. + +```rust +#[Object] +impl UserQuery { + async fn users(&self) -> Vec { + todo!() + } +} + +#[Object] +impl MovieQuery { + async fn movies(&self) -> Vec { + todo!() + } +} + +#[derive(GQLMergedObject, Default)] +struct Query(UserQuery, MovieQuery); + +let schema = Schema::new( + Query::default(), + EmptyMutation, + EmptySubscription +); +```