From 5c3528eec41c35e1b6a09fd54751fc69f744f7a2 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Thu, 14 Mar 2024 18:35:01 +0300 Subject: [PATCH] Resolve enums correctly --- .changeset/shaggy-berries-compare.md | 5 +++++ example/types.ts | 7 ++++++- src/router.ts | 22 +++++++++++++++------- 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 .changeset/shaggy-berries-compare.md diff --git a/.changeset/shaggy-berries-compare.md b/.changeset/shaggy-berries-compare.md new file mode 100644 index 00000000..8e714308 --- /dev/null +++ b/.changeset/shaggy-berries-compare.md @@ -0,0 +1,5 @@ +--- +"sofa-api": patch +--- + +Resolve enums correctly diff --git a/example/types.ts b/example/types.ts index 230ff09f..f792477f 100644 --- a/example/types.ts +++ b/example/types.ts @@ -1,6 +1,11 @@ export const typeDefs = /* GraphQL */ ` + enum Dough { + THIN + THICK + } + type Pizza { - dough: String! + dough: Dough! toppings: [String!] } diff --git a/src/router.ts b/src/router.ts index 9f358f3d..58dcca4f 100644 --- a/src/router.ts +++ b/src/router.ts @@ -6,6 +6,7 @@ import { OperationTypeNode, isIntrospectionType, isInputObjectType, + isEnumType, } from 'graphql'; import { buildOperationNodeForField, createGraphQLError } from '@graphql-tools/utils'; import { getOperationInfo, OperationInfo } from './ast.js'; @@ -95,13 +96,20 @@ export function createRouter(sofa: Sofa) { for (const typeName in types) { const type = types[typeName]; - if ( - (isObjectType(type) || isInputObjectType(type)) && - !isIntrospectionType(type) - ) { - sofa.openAPI!.components!.schemas![typeName] = buildSchemaObjectFromType(type, { - customScalars: sofa.customScalars, - }); + if (!isIntrospectionType(type)) { + if ( + (isObjectType(type) || isInputObjectType(type)) + ) { + sofa.openAPI!.components!.schemas![typeName] = buildSchemaObjectFromType(type, { + customScalars: sofa.customScalars, + }); + } else if (isEnumType(type)) { + sofa.openAPI!.components!.schemas![typeName] = { + title: type.name, + type: 'string', + enum: type.getValues().map((value) => value.name), + } + } } }