async-graphql/docs/en/src/define_one_of_object.md
Edward Rudd 3b7ed74d11 correct doc examples so they compile
- examples to fix still
  - error_extensions.md ResultExt example does not compile!
     - trait ErrorExtensions is not implemented for ParseIntError
  - dataloader
     - requires sqlx to work. So we either "stub" it OR we rewrite them simpler to use a  simple "faux" db library
2022-06-02 17:32:12 -04:00

41 lines
1.1 KiB
Markdown

# 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
# extern crate async_graphql;
# #[derive(SimpleObject)]
# struct User { a: i32 }
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 ...
# todo!()
}
}
```
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.