async-graphql/docs/en/src/apollo_federation.md

54 lines
2.1 KiB
Markdown
Raw Normal View History

2020-04-15 03:15:30 +00:00
# Apollo Federation
2020-05-09 22:37:31 +00:00
2020-09-01 05:47:22 +00:00
`Apollo Federation` is a `GraphQL` API gateway which can combine multiple GraphQL services, allowing each service to implement the subset of the API it is responsible for. You can read more in the [official documentation](https://www.apollographql.com/docs/apollo-server/federation/introduction).
2020-05-09 22:37:31 +00:00
2020-09-01 05:47:22 +00:00
`Async-graphql` supports all the functionality of `Apollo Federation`, but some modifications to your `Schema` are required.
2020-05-09 22:37:31 +00:00
- You can use the `extends` property declaration on `async_graphql::Object` and `async_graphql::Interface` to extend a type offered by another implementing service.
- The `external` property declares that a field comes from another service。
2021-02-06 08:38:45 +00:00
- The `provides` directive is used to annotate the expected returned fieldset from a field on a base type that is guaranteed to be selectable by the gateway.
- The `requires` directive is used to annotate the required input fieldset from a base type for a resolver. It is used to develop a query plan where the required fields may not be needed by the client, but the service may need additional information from other services.
2020-05-09 22:37:31 +00:00
2020-06-19 05:03:17 +00:00
## Entity lookup function
2020-05-09 22:37:31 +00:00
```rust
struct Query;
#[Object]
2020-05-09 22:37:31 +00:00
impl Query {
#[entity]
async fn find_user_by_id(&self, id: ID) -> User {
2020-06-19 05:03:17 +00:00
User { ... }
2020-05-09 22:37:31 +00:00
}
2020-06-19 05:03:17 +00:00
#[entity]
2020-09-28 09:44:00 +00:00
async fn find_user_by_id_with_username(&self, #[graphql(key)] id: ID, username: String) -> User {
2020-06-19 05:03:17 +00:00
User { ... }
}
2020-05-09 22:37:31 +00:00
2020-06-19 05:03:17 +00:00
#[entity]
async fn find_user_by_id_and_username(&self, id: ID, username: String) -> User {
User { ... }
}
2020-05-09 22:37:31 +00:00
}
```
2020-06-19 05:03:17 +00:00
**Notice the difference between these three lookup functions, which are all looking for the `User` object.**
2020-09-01 05:47:22 +00:00
- `find_user_by_id`
2020-06-19 05:03:17 +00:00
2020-09-01 05:47:22 +00:00
Use `id` to find an `User` object, the key for `User` is `id`.
2020-06-19 05:03:17 +00:00
2020-09-01 05:47:22 +00:00
- `find_user_by_id_with_username`
2020-06-19 05:03:17 +00:00
2020-09-01 05:47:22 +00:00
Use `id` to find an `User` object, the key for `User` is `id`, and the `username` field value of the `User` object is requested.
2020-06-19 05:03:17 +00:00
2020-09-01 05:47:22 +00:00
- `find_user_by_id_and_username`
2020-06-19 05:03:17 +00:00
2020-09-01 05:47:22 +00:00
Use `id` and `username` to find an `User` object, the keys for `User` are `id` and `username`.
2020-06-19 05:03:17 +00:00
2020-09-01 05:47:22 +00:00
For a complete example, refer to: <https://github.com/async-graphql/examples/tree/master/federation>.