|
| 1 | +# Apollo Federation Playground |
| 2 | + |
| 3 | +This playground demonstrates how to create an Apollo Federation subgraph using Nitro GraphQL. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Federation Enabled**: Configured as a subgraph with Apollo Federation v2 |
| 8 | +- **Entity Definitions**: User entity with `@key` directive for federation |
| 9 | +- **Reference Resolvers**: Implementation of `__resolveReference` for federated entities |
| 10 | +- **Schema-First Approach**: Using GraphQL schema files with federation directives |
| 11 | + |
| 12 | +## Running the Playground |
| 13 | + |
| 14 | +```bash |
| 15 | +# Install dependencies |
| 16 | +pnpm install |
| 17 | + |
| 18 | +# Start Apollo Server federation subgraph |
| 19 | +pnpm dev:apollo |
| 20 | + |
| 21 | +# Or start GraphQL Yoga federation subgraph |
| 22 | +pnpm dev:yoga |
| 23 | +``` |
| 24 | + |
| 25 | +The subgraph will be available at: http://localhost:3000/api/graphql |
| 26 | + |
| 27 | +## Federation Configuration |
| 28 | + |
| 29 | +### Apollo Server Federation |
| 30 | + |
| 31 | +Configured in `nitro.config.apollo.ts`: |
| 32 | + |
| 33 | +```ts |
| 34 | +export default defineNitroConfig({ |
| 35 | + graphql: { |
| 36 | + framework: 'apollo-server', |
| 37 | + federation: { |
| 38 | + enabled: true, |
| 39 | + serviceName: 'users-service', |
| 40 | + serviceVersion: '1.0.0', |
| 41 | + serviceUrl: 'http://localhost:3000/api/graphql' |
| 42 | + } |
| 43 | + } |
| 44 | +}) |
| 45 | +``` |
| 46 | + |
| 47 | +### GraphQL Yoga Federation |
| 48 | + |
| 49 | +Configured in `nitro.config.yoga.ts`: |
| 50 | + |
| 51 | +```ts |
| 52 | +export default defineNitroConfig({ |
| 53 | + graphql: { |
| 54 | + framework: 'graphql-yoga', |
| 55 | + federation: { |
| 56 | + enabled: true, |
| 57 | + serviceName: 'users-service-yoga', |
| 58 | + serviceVersion: '1.0.0', |
| 59 | + serviceUrl: 'http://localhost:3000/api/graphql' |
| 60 | + } |
| 61 | + } |
| 62 | +}) |
| 63 | +``` |
| 64 | + |
| 65 | +## Schema Structure |
| 66 | + |
| 67 | +- **User Entity**: Defined with `@key(fields: "id")` for federation |
| 68 | +- **Federation Directives**: Uses `@external`, `@provides`, `@extends` directives |
| 69 | +- **Reference Resolvers**: Implements `__resolveReference` for User entity |
| 70 | + |
| 71 | +## Testing Federation |
| 72 | + |
| 73 | +You can test the subgraph introspection to verify federation support: |
| 74 | + |
| 75 | +```graphql |
| 76 | +query { |
| 77 | + _service { |
| 78 | + sdl |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +To use this subgraph in a federation gateway, include it in your gateway configuration with the service URL and schema. |
0 commit comments