diff --git a/tests/basics/unionTypesFragment.ts b/tests/basics/unionTypesFragment.ts new file mode 100644 index 00000000..ab5e7788 --- /dev/null +++ b/tests/basics/unionTypesFragment.ts @@ -0,0 +1,28 @@ +import { readFileSync } from 'fs'; +import * as path from 'path'; +import { graphqlToOpenApi } from '../../index'; +import * as assert from 'assert'; +import * as stringify from 'json-stable-stringify'; + +describe('unionTypes', function () { + it('should produce a valid openapi spec', function () { + const schema = readFileSync( + path.join(__dirname, 'unionTypesSchema.graphql') + ).toString(); + const inputQueryFilename = path.join( + __dirname, + 'unionTypesQueryFragment.graphql' + ); + const query = readFileSync(inputQueryFilename).toString(); + // eslint-disable-next-line @typescript-eslint/no-var-requires + const expectedOutput = require('./unionTypes.json'); + const actualOutput = graphqlToOpenApi({ + schema, + query, + }).openApiSchema; + const normalizedActualOutput = stringify(actualOutput, { space: ' ' }); + const normalizedExpectedOutput = stringify(expectedOutput, { space: ' ' }); + assert.ok(!!actualOutput); + assert.equal(normalizedActualOutput, normalizedExpectedOutput); + }); +}); diff --git a/tests/basics/unionTypesQueryFragment.graphql b/tests/basics/unionTypesQueryFragment.graphql new file mode 100644 index 00000000..b0d73be7 --- /dev/null +++ b/tests/basics/unionTypesQueryFragment.graphql @@ -0,0 +1,35 @@ +fragment FurOrHairFragment on FurOrHair { + ... on Fur { + color + } + ... on Hair { + color + texture + } +} + +query queryInvolvingUnionTypes($name: String!) { + favoritePets( + name: $name + ) { + ... on Cat { + adultFur { + color + texture + } + } + ... on Dog { + puppyFur { + ...FurOrHairFragment + } + adultFurs { + ... on Fur { + texture + } + ... on Hair { + color + } + } + } + } +}