Skip to content

Commit

Permalink
Resolve enums correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Mar 14, 2024
1 parent 435e6f6 commit 5c3528e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-berries-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sofa-api": patch
---

Resolve enums correctly
7 changes: 6 additions & 1 deletion example/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export const typeDefs = /* GraphQL */ `
enum Dough {
THIN
THICK
}
type Pizza {
dough: String!
dough: Dough!
toppings: [String!]
}
Expand Down
22 changes: 15 additions & 7 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
OperationTypeNode,
isIntrospectionType,
isInputObjectType,
isEnumType,
} from 'graphql';
import { buildOperationNodeForField, createGraphQLError } from '@graphql-tools/utils';
import { getOperationInfo, OperationInfo } from './ast.js';
Expand Down Expand Up @@ -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),
}
}
}
}

Expand Down

0 comments on commit 5c3528e

Please sign in to comment.