added docs for OneofObject

This commit is contained in:
Bram van Neerven 2022-05-07 13:37:11 +02:00
parent 46c24b9436
commit 0879976df2
2 changed files with 68 additions and 31 deletions

View File

@ -3,39 +3,40 @@
- [Introduction](introduction.md)
- [Quickstart](quickstart.md)
- [Type System](typesystem.md)
- [SimpleObject](define_simple_object.md)
- [Object](define_complex_object.md)
- [Context](context.md)
- [Error handling](error_handling.md)
- [Merging Objects / Subscriptions](merging_objects.md)
- [Derived fields](derived_fields.md)
- [Enum](define_enum.md)
- [Interface](define_interface.md)
- [Union](define_union.md)
- [InputObject](define_input_object.md)
- [Default value](default_value.md)
- [SimpleObject](define_simple_object.md)
- [Object](define_complex_object.md)
- [Context](context.md)
- [Error handling](error_handling.md)
- [Merging Objects / Subscriptions](merging_objects.md)
- [Derived fields](derived_fields.md)
- [Enum](define_enum.md)
- [Interface](define_interface.md)
- [Union](define_union.md)
- [InputObject](define_input_object.md)
- [OneofObject](define_one_of_object.md)
- [Default value](default_value.md)
- [Schema](define_schema.md)
- [Query and Mutation](query_and_mutation.md)
- [Subscription](subscription.md)
- [SDL Export](sdl_export.md)
- [Query and Mutation](query_and_mutation.md)
- [Subscription](subscription.md)
- [SDL Export](sdl_export.md)
- [Utilities](utilities.md)
- [Field guard](field_guard.md)
- [Input value validators](input_value_validators.md)
- [Cache control](cache_control.md)
- [Cursor connections](cursor_connections.md)
- [Error extensions](error_extensions.md)
- [Apollo Tracing](apollo_tracing.md)
- [Query complexity and depth](depth_and_complexity.md)
- [Hide content in introspection](visibility.md)
- [Field guard](field_guard.md)
- [Input value validators](input_value_validators.md)
- [Cache control](cache_control.md)
- [Cursor connections](cursor_connections.md)
- [Error extensions](error_extensions.md)
- [Apollo Tracing](apollo_tracing.md)
- [Query complexity and depth](depth_and_complexity.md)
- [Hide content in introspection](visibility.md)
- [Extensions](extensions.md)
- [How extensions are working](extensions_inner_working.md)
- [Available extensions](extensions_available.md)
- [How extensions are working](extensions_inner_working.md)
- [Available extensions](extensions_available.md)
- [Integrations](integrations.md)
- [Poem](integrations_to_poem.md)
- [Warp](integrations_to_warp.md)
- [Actix-web](integrations_to_actix_web.md)
- [Poem](integrations_to_poem.md)
- [Warp](integrations_to_warp.md)
- [Actix-web](integrations_to_actix_web.md)
- [Advanced topics](advanced_topics.md)
- [Custom scalars](custom_scalars.md)
- [Optimizing N+1 queries](dataloader.md)
- [Custom directive](custom_directive.md)
- [Apollo Federation](apollo_federation.md)
- [Custom scalars](custom_scalars.md)
- [Optimizing N+1 queries](dataloader.md)
- [Custom directive](custom_directive.md)
- [Apollo Federation](apollo_federation.md)

View File

@ -0,0 +1,36 @@
# OneofObject
A `OneofObject` is a special type of `InputObject`, in which only one of its fields must be set and is not-null.
It is especially useful when you want a user to be able to choose between several potential input types.
This feature is still an [RFC](https://github.com/graphql/graphql-spec/pull/825) and therefore not yet officially part of the GraphQL spec, but `Async-graphql` already supports it!
```rust
use async_graphql::*;
#[derive(OneofObject)]
enum UserBy {
Email(String),
RegistrationNumber(i64),
Address(Address)
}
#[derive(InputObject)]
struct Address {
street: String,
house_number: String,
city: String,
zip: String,
}
struct Query {}
#[Object]
impl Query {
async fn search_users(&self, by: Vec<UserBy>) -> Vec<User> {
// ... Searches and returns a list of users ...
}
}
```
As you can see, a `OneofObject` is represented by an `enum` in which each variant contains another `InputType`. This means that you can use [`InputObject`](define_input_object.md) as variant too.