|
| 1 | +import { existsSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { createServer } from 'node:http'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { ProjectType } from 'testkit/gql/graphql'; |
| 6 | +import { initSeed } from 'testkit/seed'; |
| 7 | +import { getServiceHost } from 'testkit/utils'; |
| 8 | +import { execa } from '@esm2cjs/execa'; |
| 9 | + |
| 10 | +describe('Apollo Router Integration', () => { |
| 11 | + const getBaseEndpoint = () => |
| 12 | + getServiceHost('server', 8082).then(v => `http://${v}/artifacts/v1/`); |
| 13 | + const getAvailablePort = () => |
| 14 | + new Promise<number>(resolve => { |
| 15 | + const server = createServer(); |
| 16 | + server.listen(0, () => { |
| 17 | + const address = server.address(); |
| 18 | + if (address && typeof address === 'object') { |
| 19 | + const port = address.port; |
| 20 | + server.close(() => resolve(port)); |
| 21 | + } |
| 22 | + }); |
| 23 | + }); |
| 24 | + it('fetches the supergraph and sends usage reports', async () => { |
| 25 | + const routerConfigPath = join(tmpdir(), `apollo-router-config-${Date.now()}.yaml`); |
| 26 | + const endpointBaseUrl = await getBaseEndpoint(); |
| 27 | + const { createOrg } = await initSeed().createOwner(); |
| 28 | + const { createProject } = await createOrg(); |
| 29 | + const { createTargetAccessToken, createCdnAccess, target, waitForOperationsCollected } = |
| 30 | + await createProject(ProjectType.Federation); |
| 31 | + const writeToken = await createTargetAccessToken({}); |
| 32 | + |
| 33 | + // Publish Schema |
| 34 | + const publishSchemaResult = await writeToken |
| 35 | + .publishSchema({ |
| 36 | + author: 'Arda', |
| 37 | + commit: 'abc123', |
| 38 | + sdl: /* GraphQL */ ` |
| 39 | + type Query { |
| 40 | + me: User |
| 41 | + } |
| 42 | + type User { |
| 43 | + id: ID! |
| 44 | + name: String! |
| 45 | + } |
| 46 | + `, |
| 47 | + service: 'users', |
| 48 | + url: 'https://federation-demo.theguild.workers.dev/users', |
| 49 | + }) |
| 50 | + .then(r => r.expectNoGraphQLErrors()); |
| 51 | + |
| 52 | + expect(publishSchemaResult.schemaPublish.__typename).toBe('SchemaPublishSuccess'); |
| 53 | + const cdnAccessResult = await createCdnAccess(); |
| 54 | + |
| 55 | + const usageAddress = await getServiceHost('usage', 8081); |
| 56 | + |
| 57 | + const routerBinPath = join(__dirname, '../../../target/debug/router'); |
| 58 | + if (!existsSync(routerBinPath)) { |
| 59 | + throw new Error( |
| 60 | + `Apollo Router binary not found at path: ${routerBinPath}, make sure to build it first with 'cargo build'`, |
| 61 | + ); |
| 62 | + } |
| 63 | + const routerPort = await getAvailablePort(); |
| 64 | + const routerConfigContent = ` |
| 65 | +supergraph: |
| 66 | + listen: 0.0.0.0:${routerPort} |
| 67 | +plugins: |
| 68 | + hive.usage: {} |
| 69 | +`.trim(); |
| 70 | + writeFileSync(routerConfigPath, routerConfigContent, 'utf-8'); |
| 71 | + const routerProc = execa(routerBinPath, ['--dev', '--config', routerConfigPath], { |
| 72 | + all: true, |
| 73 | + env: { |
| 74 | + HIVE_CDN_ENDPOINT: endpointBaseUrl + target.id, |
| 75 | + HIVE_CDN_KEY: cdnAccessResult.secretAccessToken, |
| 76 | + HIVE_ENDPOINT: `http://${usageAddress}`, |
| 77 | + HIVE_TOKEN: writeToken.secret, |
| 78 | + HIVE_TARGET_ID: target.id, |
| 79 | + }, |
| 80 | + }); |
| 81 | + await new Promise((resolve, reject) => { |
| 82 | + routerProc.catch(err => { |
| 83 | + if (!err.isCanceled) { |
| 84 | + reject(err); |
| 85 | + } |
| 86 | + }); |
| 87 | + const routerProcOut = routerProc.all; |
| 88 | + if (!routerProcOut) { |
| 89 | + return reject(new Error('No stdout from Apollo Router process')); |
| 90 | + } |
| 91 | + let log = ''; |
| 92 | + routerProcOut.on('data', data => { |
| 93 | + log += data.toString(); |
| 94 | + if (log.includes('GraphQL endpoint exposed at')) { |
| 95 | + resolve(true); |
| 96 | + } |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + try { |
| 101 | + const url = `http://localhost:${routerPort}/`; |
| 102 | + const response = await fetch(url, { |
| 103 | + method: 'POST', |
| 104 | + headers: { |
| 105 | + accept: 'application/json', |
| 106 | + 'content-type': 'application/json', |
| 107 | + }, |
| 108 | + body: JSON.stringify({ |
| 109 | + query: ` |
| 110 | + query TestQuery { |
| 111 | + me { |
| 112 | + id |
| 113 | + name |
| 114 | + } |
| 115 | + } |
| 116 | + `, |
| 117 | + }), |
| 118 | + }); |
| 119 | + |
| 120 | + expect(response.status).toBe(200); |
| 121 | + const result = await response.json(); |
| 122 | + expect(result).toEqual({ |
| 123 | + data: { |
| 124 | + me: { |
| 125 | + id: '1', |
| 126 | + name: 'Ada Lovelace', |
| 127 | + }, |
| 128 | + }, |
| 129 | + }); |
| 130 | + await waitForOperationsCollected(1); |
| 131 | + } finally { |
| 132 | + routerProc.cancel(); |
| 133 | + rmSync(routerConfigPath); |
| 134 | + } |
| 135 | + }); |
| 136 | +}); |
0 commit comments