From d5e0dbd85ad2c349fc316f19782aa4cd8d45efa0 Mon Sep 17 00:00:00 2001 From: gilgardosh Date: Wed, 13 Sep 2023 18:54:17 +0300 Subject: [PATCH 1/2] Create spec for Union types --- src/open-api/types.ts | 7 +++ tests/open-api/operations.spec.ts | 72 +++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/src/open-api/types.ts b/src/open-api/types.ts index f29b4ba0..9d0c31f4 100644 --- a/src/open-api/types.ts +++ b/src/open-api/types.ts @@ -9,6 +9,7 @@ import { isScalarType, isEnumType, GraphQLType, + isUnionType, } from 'graphql'; import { mapToPrimitive, mapToRef } from './utils'; @@ -89,6 +90,12 @@ export function resolveFieldType( }; } + if (isUnionType(type)) { + return { + oneOf: type.getTypes().map((type) => resolveFieldType(type, opts)), + }; + } + return { type: 'object', }; diff --git a/tests/open-api/operations.spec.ts b/tests/open-api/operations.spec.ts index 446b3685..0e53fcc2 100644 --- a/tests/open-api/operations.spec.ts +++ b/tests/open-api/operations.spec.ts @@ -20,6 +20,18 @@ const schema = buildSchema(/* GraphQL */ ` authorId: Int } + type Image { + url: String + } + + type Gallery { + id: ID + name: String + images: [Image!]! + } + + union Inspiration = Post | Gallery + type Query { """ Feed of posts @@ -30,6 +42,11 @@ const schema = buildSchema(/* GraphQL */ ` """ type: PostType ): [Post] + + """ + Random post or gallery + """ + inspiration: Inspiration } scalar Date @@ -232,3 +249,58 @@ test('handle query params in POST requests', async () => { }, }); }); + +test('handle union type', async () => { + const operation = buildOperationNodeForField({ + schema, + kind: 'query' as OperationTypeNode, + field: 'inspiration', + models: [], + ignore: [], + }); + + const result = buildPathFromOperation({ + url: '/api/inspiration', + operation: { + kind: Kind.DOCUMENT, + definitions: [operation], + }, + schema, + useRequestBody: false, + customScalars: { + Date: { type: 'string', format: 'date' }, + }, + }); + expect(result.operationId).toEqual('inspiration_query'); + expect(result.parameters?.length).toEqual(2); + expect(result.parameters?.[0]).toEqual({ + in: 'query', + name: 'inspiration_comments_filter', + required: true, + schema: { + type: 'string', + }, + }); + expect(result.parameters?.[1]).toEqual({ + in: 'query', + name: 'inspiration_comments_date', + required: false, + schema: { + type: 'string', + format: 'date', + }, + }); + + expect((result.responses[200] as any).description).toMatch( + 'Random post or gallery' + ); + + const response = (result.responses[200] as any).content['application/json'] + .schema; + expect(response).toEqual({ + oneOf: [ + { $ref: '#/components/schemas/Post' }, + { $ref: '#/components/schemas/Gallery' }, + ], + }); +}); From 893165714b099f3e5a067bc471f71455670bbc61 Mon Sep 17 00:00:00 2001 From: gilgardosh Date: Wed, 13 Sep 2023 19:07:34 +0300 Subject: [PATCH 2/2] changeset --- .changeset/itchy-kids-retire.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/itchy-kids-retire.md diff --git a/.changeset/itchy-kids-retire.md b/.changeset/itchy-kids-retire.md new file mode 100644 index 00000000..abb8bb9a --- /dev/null +++ b/.changeset/itchy-kids-retire.md @@ -0,0 +1,5 @@ +--- +'sofa-api': patch +--- + +Bug fix: Generate spec for Union types