|
| 1 | +import { gateway, DataSource } from '../gateway/gateway' |
| 2 | +import { yoga as service1 } from '../service/yoga' |
| 3 | +import { createServer, Server } from 'http' |
| 4 | +import { AddressInfo } from 'net' |
| 5 | +import { fetch } from '@whatwg-node/fetch' |
| 6 | +import type { GatewayConfig } from '@apollo/gateway' |
| 7 | + |
| 8 | +describe('apollo-federation example integration', () => { |
| 9 | + let gatewayServer: Server |
| 10 | + let serviceServer: Server |
| 11 | + let gatewayPort: number |
| 12 | + let servicePort: number |
| 13 | + |
| 14 | + beforeAll(async () => { |
| 15 | + serviceServer = createServer(service1) |
| 16 | + await new Promise<void>((resolve) => serviceServer.listen(0, resolve)) |
| 17 | + servicePort = (serviceServer.address() as AddressInfo).port |
| 18 | + |
| 19 | + const gatewayConfig: GatewayConfig = { |
| 20 | + serviceList: [ |
| 21 | + { name: 'accounts', url: `http://localhost:${servicePort}/graphql` }, |
| 22 | + ], |
| 23 | + introspectionHeaders: { |
| 24 | + accept: 'application/json', |
| 25 | + }, |
| 26 | + buildService({ url }) { |
| 27 | + return new DataSource({ url }) |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + const gatewayService = await gateway(gatewayConfig) |
| 32 | + gatewayServer = createServer(gatewayService) |
| 33 | + await new Promise<void>((resolve) => gatewayServer.listen(0, resolve)) |
| 34 | + gatewayPort = (gatewayServer.address() as AddressInfo).port |
| 35 | + }) |
| 36 | + |
| 37 | + afterAll(async () => { |
| 38 | + await new Promise((resolve) => gatewayServer.close(resolve)) |
| 39 | + await new Promise((resolve) => serviceServer.close(resolve)) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should execute field on subgraph', async () => { |
| 43 | + const response = await fetch( |
| 44 | + `http://localhost:${gatewayPort}/graphql?query=query{me { id }}`, |
| 45 | + ) |
| 46 | + const body = await response.json() |
| 47 | + expect(body.errors).toBeUndefined() |
| 48 | + expect(body.data).toEqual({ |
| 49 | + me: { |
| 50 | + id: '1', |
| 51 | + }, |
| 52 | + }) |
| 53 | + }) |
| 54 | +}) |
0 commit comments