|
| 1 | +import { exec, execSync } from 'child_process'; |
| 2 | +import { createReadStream, readFile, readFileSync, write, writeFileSync } from 'fs'; |
| 3 | +import { createServer } from 'http'; |
| 4 | +import { join } from 'path'; |
| 5 | + |
| 6 | +jest.setTimeout(30000); |
| 7 | +describe('Polling Test', () => { |
| 8 | + let cleanupCallbacks: (() => void)[] = []; |
| 9 | + afterAll(() => { |
| 10 | + cleanupCallbacks.forEach(cb => cb()); |
| 11 | + }); |
| 12 | + it('should pass', async () => { |
| 13 | + const cwd = join(__dirname, 'fixtures/polling'); |
| 14 | + const supergraphSdlPath = join(cwd, 'supergraph.graphql'); |
| 15 | + const supergraphSdl = readFileSync(supergraphSdlPath, 'utf-8'); |
| 16 | + let changedSupergraph = false; |
| 17 | + const supergraphSdlServer = createServer((req, res) => { |
| 18 | + res.statusCode = 200; |
| 19 | + res.setHeader('Content-Type', 'text/plain'); |
| 20 | + console.log('Serving supergraph SDL'); |
| 21 | + if (changedSupergraph) { |
| 22 | + res.end(supergraphSdl.replace('topProducts', 'topProductsNew')); |
| 23 | + } else { |
| 24 | + res.end(supergraphSdl); |
| 25 | + } |
| 26 | + }); |
| 27 | + await new Promise<void>(resolve => supergraphSdlServer.listen(0, resolve)); |
| 28 | + cleanupCallbacks.push(() => supergraphSdlServer.close()); |
| 29 | + const SUPERGRAPH_SOURCE = `http://localhost:${(supergraphSdlServer.address() as any).port}`; |
| 30 | + console.info('Supergraph SDL server is running on ' + SUPERGRAPH_SOURCE); |
| 31 | + const buildCmd = exec(`${join(__dirname, '../node_modules/.bin/mesh')} build`, { |
| 32 | + cwd, |
| 33 | + env: { |
| 34 | + ...process.env, |
| 35 | + SUPERGRAPH_SOURCE, |
| 36 | + }, |
| 37 | + }); |
| 38 | + await new Promise<void>(resolve => { |
| 39 | + buildCmd.stdout?.on('data', function stdoutListener(data: string) { |
| 40 | + if (data.includes('Done!')) { |
| 41 | + buildCmd.stdout?.off('data', stdoutListener); |
| 42 | + resolve(); |
| 43 | + } |
| 44 | + }); |
| 45 | + }); |
| 46 | + const serveCmd = exec(`${join(__dirname, '../node_modules/.bin/mesh')} start`, { |
| 47 | + cwd, |
| 48 | + env: { |
| 49 | + ...process.env, |
| 50 | + SUPERGRAPH_SOURCE, |
| 51 | + }, |
| 52 | + }); |
| 53 | + cleanupCallbacks.push(() => serveCmd.kill()); |
| 54 | + await new Promise<void>(resolve => { |
| 55 | + serveCmd.stdout?.on('data', function stdoutListener(data: string) { |
| 56 | + console.log(data); |
| 57 | + if (data.includes('Serving GraphQL Mesh')) { |
| 58 | + serveCmd.stdout?.off('data', stdoutListener); |
| 59 | + resolve(); |
| 60 | + } |
| 61 | + }); |
| 62 | + }); |
| 63 | + const resp = await fetch('http://127.0.0.1:4000/graphql', { |
| 64 | + method: 'POST', |
| 65 | + headers: { |
| 66 | + 'Content-Type': 'application/json', |
| 67 | + }, |
| 68 | + body: JSON.stringify({ |
| 69 | + query: ` |
| 70 | + { |
| 71 | + __type(name:"Query") { |
| 72 | + fields { |
| 73 | + name |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + `, |
| 78 | + }), |
| 79 | + }); |
| 80 | + const data = await resp.json(); |
| 81 | + expect(data).toEqual({ |
| 82 | + data: { |
| 83 | + __type: { |
| 84 | + fields: [ |
| 85 | + { |
| 86 | + name: 'me', |
| 87 | + }, |
| 88 | + { |
| 89 | + name: 'user', |
| 90 | + }, |
| 91 | + { |
| 92 | + name: 'users', |
| 93 | + }, |
| 94 | + { |
| 95 | + name: 'topProducts', |
| 96 | + }, |
| 97 | + ], |
| 98 | + }, |
| 99 | + }, |
| 100 | + }); |
| 101 | + changedSupergraph = true; |
| 102 | + await new Promise(resolve => setTimeout(resolve, 3000)); |
| 103 | + const resp2 = await fetch('http://127.0.0.1:4000/graphql', { |
| 104 | + method: 'POST', |
| 105 | + headers: { |
| 106 | + 'Content-Type': 'application/json', |
| 107 | + }, |
| 108 | + body: JSON.stringify({ |
| 109 | + query: ` |
| 110 | + { |
| 111 | + __type(name:"Query") { |
| 112 | + fields { |
| 113 | + name |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + `, |
| 118 | + }), |
| 119 | + }); |
| 120 | + const data2 = await resp2.json(); |
| 121 | + expect(data2).toEqual({ |
| 122 | + data: { |
| 123 | + __type: { |
| 124 | + fields: [ |
| 125 | + { |
| 126 | + name: 'me', |
| 127 | + }, |
| 128 | + { |
| 129 | + name: 'user', |
| 130 | + }, |
| 131 | + { |
| 132 | + name: 'users', |
| 133 | + }, |
| 134 | + { |
| 135 | + name: 'topProductsNew', |
| 136 | + }, |
| 137 | + ], |
| 138 | + }, |
| 139 | + }, |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments