|
| 1 | +import { GraphQLSchemaModule } from 'apollo-graphql'; |
| 2 | +import { buildFederatedSchema } from '@apollo/federation'; |
| 3 | +import { ApolloServer } from 'apollo-server'; |
| 4 | +import fetch from 'node-fetch'; |
| 5 | +import { ApolloGateway } from '../..'; |
| 6 | +import { fixtures } from 'apollo-federation-integration-testsuite'; |
| 7 | +import { ApolloServerPluginInlineTrace } from 'apollo-server-core'; |
| 8 | + |
| 9 | +async function startFederatedServer(modules: GraphQLSchemaModule[]) { |
| 10 | + const schema = buildFederatedSchema(modules); |
| 11 | + const server = new ApolloServer({ |
| 12 | + schema, |
| 13 | + // Manually installing the inline trace plugin means it doesn't log a message. |
| 14 | + plugins: [ApolloServerPluginInlineTrace()], |
| 15 | + }); |
| 16 | + const { url } = await server.listen({ port: 0 }); |
| 17 | + return { url, server }; |
| 18 | +} |
| 19 | + |
| 20 | +describe('end-to-end', () => { |
| 21 | + let backendServers: ApolloServer[]; |
| 22 | + let gatewayServer: ApolloServer; |
| 23 | + let gatewayUrl: string; |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + backendServers = []; |
| 27 | + const serviceList = []; |
| 28 | + for (const fixture of fixtures) { |
| 29 | + const { server, url } = await startFederatedServer([fixture]); |
| 30 | + backendServers.push(server); |
| 31 | + serviceList.push({ name: fixture.name, url }); |
| 32 | + } |
| 33 | + |
| 34 | + const gateway = new ApolloGateway({ serviceList }); |
| 35 | + gatewayServer = new ApolloServer({ |
| 36 | + gateway, |
| 37 | + }); |
| 38 | + ({ url: gatewayUrl } = await gatewayServer.listen({ port: 0 })); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(async () => { |
| 42 | + for (const server of backendServers) { |
| 43 | + await server.stop(); |
| 44 | + } |
| 45 | + if (gatewayServer) { |
| 46 | + await gatewayServer.stop(); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + it(`cache control`, async () => { |
| 51 | + const query = ` |
| 52 | + query { |
| 53 | + me { |
| 54 | + name { |
| 55 | + first |
| 56 | + last |
| 57 | + } |
| 58 | + } |
| 59 | + topProducts { |
| 60 | + name |
| 61 | + } |
| 62 | + } |
| 63 | + `; |
| 64 | + |
| 65 | + const response = await fetch(gatewayUrl, { |
| 66 | + method: 'POST', |
| 67 | + headers: { |
| 68 | + 'Content-Type': 'application/json', |
| 69 | + }, |
| 70 | + body: JSON.stringify({ query }), |
| 71 | + }); |
| 72 | + const result = await response.json(); |
| 73 | + expect(result).toMatchInlineSnapshot(` |
| 74 | + Object { |
| 75 | + "data": Object { |
| 76 | + "me": Object { |
| 77 | + "name": Object { |
| 78 | + "first": "Ada", |
| 79 | + "last": "Lovelace", |
| 80 | + }, |
| 81 | + }, |
| 82 | + "topProducts": Array [ |
| 83 | + Object { |
| 84 | + "name": "Table", |
| 85 | + }, |
| 86 | + Object { |
| 87 | + "name": "Couch", |
| 88 | + }, |
| 89 | + Object { |
| 90 | + "name": "Chair", |
| 91 | + }, |
| 92 | + Object { |
| 93 | + "name": "Structure and Interpretation of Computer Programs (1996)", |
| 94 | + }, |
| 95 | + Object { |
| 96 | + "name": "Object Oriented Software Construction (1997)", |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + } |
| 101 | + `); |
| 102 | + expect(response.headers.get('cache-control')).toBe('max-age=30, private'); |
| 103 | + }); |
| 104 | + |
| 105 | + it(`cache control, uncacheable`, async () => { |
| 106 | + const query = ` |
| 107 | + query { |
| 108 | + me { |
| 109 | + name { |
| 110 | + first |
| 111 | + last |
| 112 | + } |
| 113 | + } |
| 114 | + topProducts { |
| 115 | + name |
| 116 | + ... on Book { |
| 117 | + details { # This field has no cache policy. |
| 118 | + pages |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + `; |
| 124 | + |
| 125 | + const response = await fetch(gatewayUrl, { |
| 126 | + method: 'POST', |
| 127 | + headers: { |
| 128 | + 'Content-Type': 'application/json', |
| 129 | + }, |
| 130 | + body: JSON.stringify({ query }), |
| 131 | + }); |
| 132 | + const result = await response.json(); |
| 133 | + expect(result).toMatchInlineSnapshot(` |
| 134 | + Object { |
| 135 | + "data": Object { |
| 136 | + "me": Object { |
| 137 | + "name": Object { |
| 138 | + "first": "Ada", |
| 139 | + "last": "Lovelace", |
| 140 | + }, |
| 141 | + }, |
| 142 | + "topProducts": Array [ |
| 143 | + Object { |
| 144 | + "name": "Table", |
| 145 | + }, |
| 146 | + Object { |
| 147 | + "name": "Couch", |
| 148 | + }, |
| 149 | + Object { |
| 150 | + "name": "Chair", |
| 151 | + }, |
| 152 | + Object { |
| 153 | + "details": null, |
| 154 | + "name": "Structure and Interpretation of Computer Programs (1996)", |
| 155 | + }, |
| 156 | + Object { |
| 157 | + "details": null, |
| 158 | + "name": "Object Oriented Software Construction (1997)", |
| 159 | + }, |
| 160 | + ], |
| 161 | + }, |
| 162 | + } |
| 163 | + `); |
| 164 | + expect(response.headers.get('cache-control')).toBe(null); |
| 165 | + }); |
| 166 | +}); |
0 commit comments