|
4 | 4 |
|
5 | 5 | To quote the [Apollo docs](https://blog.apollographql.com/apollo-federation-f260cf525d21), Federation is designed with these core principles:
|
6 | 6 |
|
7 |
| -- **Building a graph should be declarative.** With federation, you compose a graph declaratively from within your schema instead of writing imperative schema stitching code. |
8 |
| -- **Code should be separated by concern, not by types.** Often no single team controls every aspect of an important type like a User or Product, so the definition of these types should be distributed across teams and codebases, rather than centralized. |
9 |
| -- **The graph should be simple for clients to consume.** Together, federated services can form a complete, product-focused graph that accurately reflects how it’s being consumed on the client. |
10 |
| -- **It’s just GraphQL, using only spec-compliant features of the language.** Any language, not just JavaScript, can implement federation. |
| 7 | +- Building a graph should be **declarative.** With federation, you compose a graph declaratively from within your schema instead of writing imperative schema stitching code. |
| 8 | +- Code should be separated by **concern**, not by types. Often no single team controls every aspect of an important type like a User or Product, so the definition of these types should be distributed across teams and codebases, rather than centralized. |
| 9 | +- The graph should be simple for clients to consume. Together, federated services can form a complete, product-focused graph that accurately reflects how it’s being consumed on the client. |
| 10 | +- It’s just **GraphQL**, using only spec-compliant features of the language. Any language, not just JavaScript, can implement federation. |
11 | 11 |
|
12 |
| -> warn **Note** Apollo Federation currently does not support subscriptions, and only the "Schema first" approach is currently supported due to limitations with the decorators not yet supporting GraphQL directives<sup>[1](https://github.com/MichalLytek/type-graphql/issues/351)</sup> |
| 12 | +> warning **Note** Apollo Federation currently does not support subscriptions, and only the schema-first approach is currently supported due to limitations with the decorators not yet supporting GraphQL directives<sup>[1](https://github.com/MichalLytek/type-graphql/issues/351)</sup> |
13 | 13 |
|
14 | 14 | In the next example, we'll set up a demo application with a gateway and two federated endpoints: a user- and posts service.
|
15 | 15 |
|
16 | 16 | #### Federated example: Users
|
17 | 17 |
|
18 |
| -First install the optional dependency for federation: `npm install --save @apollo/federation`. |
| 18 | +First install the optional dependency for federation: |
| 19 | + |
| 20 | +```bash |
| 21 | +$ npm install --save @apollo/federation` |
| 22 | +``` |
19 | 23 |
|
20 | 24 | The User service has a simple schema. Note the `@key` directive, it tells the Apollo query planner that a particular instance of User can be fetched if you have its `id`. Also note that we extend the Query type.
|
21 | 25 |
|
22 |
| -```java |
| 26 | +```graphql |
23 | 27 | type User @key(fields: "id") {
|
24 | 28 | id: ID!
|
25 | 29 | name: String!
|
@@ -74,7 +78,7 @@ export class AppModule {}
|
74 | 78 |
|
75 | 79 | The Posts service references the User type in its schema by marking it with the `extend` keyword. It also adds one property to the User type. Note the `@key` directive used for matching instances of User, and the `@external` directive indicating that the `id` field is managed elsewhere.
|
76 | 80 |
|
77 |
| -```java |
| 81 | +```graphql |
78 | 82 | type Post @key(fields: "id") {
|
79 | 83 | id: ID!
|
80 | 84 | title: String!
|
@@ -162,7 +166,7 @@ import { GraphQLGatewayModule } from "@nestjs/graphql";
|
162 | 166 | export class AppModule {}
|
163 | 167 | ```
|
164 | 168 |
|
165 |
| -> warn **Warning** Apollo recommends that you don't rely on the service discovery in a production environment but use their [Graph Manager](https://www.apollographql.com/docs/graph-manager/federation/) instead. |
| 169 | +> info **Hint** Apollo recommends that you don't rely on the service discovery in a production environment but use their [Graph Manager](https://www.apollographql.com/docs/graph-manager/federation/) instead. |
166 | 170 |
|
167 | 171 | #### Sharing context
|
168 | 172 |
|
|
0 commit comments