docs: Add imports to fed doc examples
This commit is contained in:
parent
ed9329cc41
commit
39b6565497
|
@ -9,9 +9,12 @@ Apollo Federation is a GraphQL architecture for combining multiple GraphQL servi
|
||||||
`async-graphql` supports all the functionality of Apollo Federation v2. Support will be enabled automatically if any `#[graphql(entity)]` resolvers are found in the schema. To enable it manually, use the `enable_federation` method on the `SchemaBuilder`.
|
`async-graphql` supports all the functionality of Apollo Federation v2. Support will be enabled automatically if any `#[graphql(entity)]` resolvers are found in the schema. To enable it manually, use the `enable_federation` method on the `SchemaBuilder`.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
# extern crate async_graphql;
|
||||||
|
# extern crate tokio;
|
||||||
|
# use async_graphql::*;
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let schema = Schema::build(Query, EmptyMutation, EmptySubscription)
|
let schema = Schema::build(EmptyQuery, EmptyMutation, EmptySubscription)
|
||||||
.enable_federation()
|
.enable_federation()
|
||||||
.finish();
|
.finish();
|
||||||
// ... Start your server of choice
|
// ... Start your server of choice
|
||||||
|
@ -130,6 +133,8 @@ Apply the [`@shareable` directive](https://www.apollographql.com/docs/federation
|
||||||
|
|
||||||
### `@shareable` fields
|
### `@shareable` fields
|
||||||
```rust
|
```rust
|
||||||
|
# extern crate async_graphql;
|
||||||
|
# use async_graphql::*;
|
||||||
#[SimpleObject]
|
#[SimpleObject]
|
||||||
#[graphql(complex)]
|
#[graphql(complex)]
|
||||||
struct Position {
|
struct Position {
|
||||||
|
@ -159,6 +164,8 @@ type Position {
|
||||||
### `@shareable` type
|
### `@shareable` type
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
# extern crate async_graphql;
|
||||||
|
# use async_graphql::*;
|
||||||
#[SimpleObject]
|
#[SimpleObject]
|
||||||
#[graphql(shareable)]
|
#[graphql(shareable)]
|
||||||
struct Position {
|
struct Position {
|
||||||
|
@ -181,6 +188,8 @@ type Position @shareable {
|
||||||
The [`@inaccessible` directive](https://www.apollographql.com/docs/federation/federated-types/federated-directives#inaccessible) is used to omit something from the supergraph schema (e.g., if it's not yet added to all subgraphs which share a `@shareable` type).
|
The [`@inaccessible` directive](https://www.apollographql.com/docs/federation/federated-types/federated-directives#inaccessible) is used to omit something from the supergraph schema (e.g., if it's not yet added to all subgraphs which share a `@shareable` type).
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
# extern crate async_graphql;
|
||||||
|
# use async_graphql::*;
|
||||||
#[SimpleObject]
|
#[SimpleObject]
|
||||||
#[graphql(shareable)]
|
#[graphql(shareable)]
|
||||||
struct Position {
|
struct Position {
|
||||||
|
@ -208,6 +217,8 @@ The [`@override` directive](https://www.apollographql.com/docs/federation/federa
|
||||||
For example, if you add a new "Inventory" subgraph which should take over responsibility for the `inStock` field currently provided by the "Products" subgraph, you might have something like this:
|
For example, if you add a new "Inventory" subgraph which should take over responsibility for the `inStock` field currently provided by the "Products" subgraph, you might have something like this:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
# extern crate async_graphql;
|
||||||
|
# use async_graphql::*;
|
||||||
#[SimpleObject]
|
#[SimpleObject]
|
||||||
struct Product {
|
struct Product {
|
||||||
id: ID,
|
id: ID,
|
||||||
|
@ -230,6 +241,8 @@ type Product @key(fields: "id") {
|
||||||
The [`@external` directive](https://www.apollographql.com/docs/federation/federated-types/federated-directives#external) is used to indicate that a field is usually provided by another subgraph, but is sometimes required by this subgraph (when combined with `@requires`) or provided by this subgraph (when combined with `@provides`).
|
The [`@external` directive](https://www.apollographql.com/docs/federation/federated-types/federated-directives#external) is used to indicate that a field is usually provided by another subgraph, but is sometimes required by this subgraph (when combined with `@requires`) or provided by this subgraph (when combined with `@provides`).
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
# extern crate async_graphql;
|
||||||
|
# use async_graphql::*;
|
||||||
#[SimpleObject]
|
#[SimpleObject]
|
||||||
struct Product {
|
struct Product {
|
||||||
id: ID,
|
id: ID,
|
||||||
|
@ -254,6 +267,8 @@ type Product {
|
||||||
The [`@provides` directive](https://www.apollographql.com/docs/federation/federated-types/federated-directives#provides) is used to indicate that a field is provided by this subgraph, but only sometimes.
|
The [`@provides` directive](https://www.apollographql.com/docs/federation/federated-types/federated-directives#provides) is used to indicate that a field is provided by this subgraph, but only sometimes.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
# extern crate async_graphql;
|
||||||
|
# use async_graphql::*;
|
||||||
#[SimpleObject]
|
#[SimpleObject]
|
||||||
struct Product {
|
struct Product {
|
||||||
id: ID,
|
id: ID,
|
||||||
|
@ -326,6 +341,8 @@ type Product @key(fields: "id") {
|
||||||
In order to implement this in Rust, we can use the `#[graphql(requires)]` attribute:
|
In order to implement this in Rust, we can use the `#[graphql(requires)]` attribute:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
# extern crate async_graphql;
|
||||||
|
# use async_graphql::*;
|
||||||
#[SimpleObject]
|
#[SimpleObject]
|
||||||
#[graphql(complex)]
|
#[graphql(complex)]
|
||||||
struct Product {
|
struct Product {
|
||||||
|
@ -340,8 +357,8 @@ struct Product {
|
||||||
impl Product {
|
impl Product {
|
||||||
#[graphql(requires = "size weightInPounds")]
|
#[graphql(requires = "size weightInPounds")]
|
||||||
async fn shipping_estimate(&self) -> String {
|
async fn shipping_estimate(&self) -> String {
|
||||||
let price = self.size? * self.weight_in_pounds?;
|
let price = self.size * self.weight_in_pounds;
|
||||||
Some(format!(${}, price))
|
format!(${}, price)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -349,6 +366,18 @@ impl Product {
|
||||||
Note that we use the GraphQL field name `weightInPounds`, not the Rust field name `weight_in_pounds` in `requires`. To populate those external fields, we add them as arguments in the entity resolver:
|
Note that we use the GraphQL field name `weightInPounds`, not the Rust field name `weight_in_pounds` in `requires`. To populate those external fields, we add them as arguments in the entity resolver:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
# extern crate async_graphql;
|
||||||
|
# use async_graphql::*;
|
||||||
|
# #[SimpleObject]
|
||||||
|
# #[graphql(complex)]
|
||||||
|
# struct Product {
|
||||||
|
# id: ID,
|
||||||
|
# #[graphql(external)]
|
||||||
|
# size: u32,
|
||||||
|
# #[graphql(external)]
|
||||||
|
# weight_in_pounds: u32,
|
||||||
|
# }
|
||||||
|
# struct Query;
|
||||||
#[Object]
|
#[Object]
|
||||||
impl Query {
|
impl Query {
|
||||||
#[graphql(entity)]
|
#[graphql(entity)]
|
||||||
|
@ -376,11 +405,16 @@ We have to put _something_ in place for `size` and `weight_in_pounds` as they ar
|
||||||
A case where the `@requires` directive can be confusing is when there are nested entities. For example, if we had an `Order` type which contained a `Product`, then we would need an entity resolver like this:
|
A case where the `@requires` directive can be confusing is when there are nested entities. For example, if we had an `Order` type which contained a `Product`, then we would need an entity resolver like this:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
# extern crate async_graphql;
|
||||||
|
# use async_graphql::*;
|
||||||
|
# #[derive(SimpleObject)]
|
||||||
|
# pub struct Order { id: ID }
|
||||||
|
# struct Query;
|
||||||
#[Object]
|
#[Object]
|
||||||
impl Query {
|
impl Query {
|
||||||
#[graphql(entity)]
|
#[graphql(entity)]
|
||||||
async fn find_order_by_id(&self, id: ID) -> Option<Order> {
|
async fn find_order_by_id(&self, id: ID) -> Option<Order> {
|
||||||
db::find_order_by_id(id)
|
Some(Order { id })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -401,6 +435,8 @@ type User @tag(name: "team-accounts") {
|
||||||
You can write code like this:
|
You can write code like this:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
# extern crate async_graphql;
|
||||||
|
# use async_graphql::*;
|
||||||
#[SimpleObject]
|
#[SimpleObject]
|
||||||
#[graphql(tag = "team-accounts")]
|
#[graphql(tag = "team-accounts")]
|
||||||
struct User {
|
struct User {
|
||||||
|
|
Loading…
Reference in New Issue