From 11c7dc2f48170c3080b8600e3c23727b7ff877e3 Mon Sep 17 00:00:00 2001 From: herrernst Date: Wed, 24 Sep 2025 18:59:38 +0200 Subject: [PATCH] add test case for fragment spread on union types --- tests/basics/unionTypesFragment.ts | 28 ++++++++++++++++ tests/basics/unionTypesQueryFragment.graphql | 35 ++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/basics/unionTypesFragment.ts create mode 100644 tests/basics/unionTypesQueryFragment.graphql 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 + } + } + } + } +}