|
| 1 | +import { readdirSync, readFileSync } from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { parse, validate } from 'graphql'; |
| 4 | +import { normalizedExecutor } from '@graphql-tools/executor'; |
| 5 | +import { getStitchedSchemaFromSupergraphSdl } from '../src/supergraph'; |
| 6 | + |
| 7 | +describe('Federation Compatibility', () => { |
| 8 | + const fixturesDir = join(__dirname, 'fixtures', 'federation-compatibility'); |
| 9 | + readdirSync(fixturesDir).forEach(supergraphName => { |
| 10 | + describe(supergraphName, () => { |
| 11 | + const supergraphFixturesDir = join(fixturesDir, supergraphName); |
| 12 | + const stitchedSchema = getStitchedSchemaFromSupergraphSdl({ |
| 13 | + supergraphSdl: readFileSync(join(supergraphFixturesDir, 'supergraph.graphql'), 'utf-8'), |
| 14 | + }); |
| 15 | + const tests: { query: string; expected: any }[] = JSON.parse( |
| 16 | + readFileSync(join(supergraphFixturesDir, 'tests.json'), 'utf-8'), |
| 17 | + ); |
| 18 | + tests.forEach((test, i) => { |
| 19 | + it(`test-query-${i}`, async () => { |
| 20 | + let result; |
| 21 | + const document = parse(test.query, { noLocation: true }); |
| 22 | + const validationErrors = validate(stitchedSchema, document); |
| 23 | + if (validationErrors.length > 0) { |
| 24 | + result = { errors: validationErrors }; |
| 25 | + } else { |
| 26 | + result = await normalizedExecutor({ |
| 27 | + schema: stitchedSchema, |
| 28 | + document, |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + if (test.expected.errors === true) { |
| 33 | + if (test.expected.data) { |
| 34 | + expect(result).toMatchObject({ |
| 35 | + data: test.expected.data, |
| 36 | + errors: expect.any(Array), |
| 37 | + }); |
| 38 | + } else { |
| 39 | + expect(result).toMatchObject({ |
| 40 | + errors: expect.any(Array), |
| 41 | + }); |
| 42 | + } |
| 43 | + } else { |
| 44 | + if ('errors' in result && result.errors) { |
| 45 | + for (const error of result.errors) { |
| 46 | + console.error(error.message); |
| 47 | + } |
| 48 | + } |
| 49 | + expect(result).toMatchObject({ |
| 50 | + data: test.expected.data, |
| 51 | + }); |
| 52 | + } |
| 53 | + }); |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
| 57 | +}); |
0 commit comments