From 4a31feedfba51c0fd500b956fac8550cf660b2a8 Mon Sep 17 00:00:00 2001 From: Charlie Bamford Date: Mon, 11 Sep 2023 16:30:35 -0700 Subject: [PATCH 1/3] Add enum support --- src/createSchema.ts | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/createSchema.ts b/src/createSchema.ts index c0cb426..2f93f83 100644 --- a/src/createSchema.ts +++ b/src/createSchema.ts @@ -1,5 +1,6 @@ import { GraphQLBoolean, + GraphQLEnumType, GraphQLFieldConfigArgumentMap, GraphQLFieldConfigMap, GraphQLFloat, @@ -18,10 +19,11 @@ import { GraphQLUnionType, GraphQLError, Kind, + ValueNode, } from 'graphql'; import * as ts from 'typescript'; import {DateType} from './date'; -import {typeAST, AllTypes, Interface, Primitive, Union, InterfaceLiteral, UnionLiteral} from 'ts-type-ast'; +import {typeAST, AllTypes, Interface, Primitive, Union, InterfaceLiteral, UnionLiteral, Enum} from 'ts-type-ast'; type CustomScalarFactory = (type: Primitive) => GraphQLScalarType | undefined; export function createSchema( @@ -78,8 +80,8 @@ export function createSchema( case 'interface': case 'interfaceLiteral': return createGQLType(type, isInput); - // case 'enum': - // return add(type, createGQLEnum(type)); + case 'enum': + return add(type, createGQLEnum(type)); case 'union': case 'unionLiteral': if (isInput) return add(type, createGQLInputUnion(type)); @@ -179,7 +181,7 @@ export function createSchema( description: type.kind === 'union' ? type.doc : undefined, serialize: validate, parseValue: validate, - parseLiteral(ast) { + parseLiteral(ast: ValueNode) { if (ast.kind === Kind.STRING) { return validate(ast.value); } @@ -205,6 +207,28 @@ export function createSchema( } throw new Error('Unexpected type: ' + JSON.stringify(type)); } + + function createGQLEnum(type: Enum) { + const values = type.types.reduce((acc, item, index) => { + if (item.kind !== "primitive") { + throw new Error(`Only string enum values are supported: ${JSON.stringify(type)}`) + } + else { + if (item.type !== "string") { + throw new Error(`Only string enum values are supported: ${JSON.stringify(type)}`) + } + else { + acc[item.literal] = {value: index} + } + } + return acc; + }, {} as GraphQLEnumType["values"]) + + return new GraphQLEnumType({ + name: type.name, + values + }) + } } function never(never: never): never { From 8b26c684e8f187ed9f9f8a16b268cc7edc137a8f Mon Sep 17 00:00:00 2001 From: Charlie Bamford Date: Mon, 11 Sep 2023 17:06:47 -0700 Subject: [PATCH 2/3] Fix type of values. --- src/createSchema.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/createSchema.ts b/src/createSchema.ts index 2f93f83..c9fcbcf 100644 --- a/src/createSchema.ts +++ b/src/createSchema.ts @@ -1,6 +1,7 @@ import { GraphQLBoolean, GraphQLEnumType, + GraphQLEnumTypeConfig, GraphQLFieldConfigArgumentMap, GraphQLFieldConfigMap, GraphQLFloat, @@ -222,7 +223,7 @@ export function createSchema( } } return acc; - }, {} as GraphQLEnumType["values"]) + }, {} as GraphQLEnumTypeConfig["values"]) return new GraphQLEnumType({ name: type.name, From b09dd4ed389cfa82e73bd5cc25b15b2ebe6c451b Mon Sep 17 00:00:00 2001 From: Charlie Bamford Date: Mon, 11 Sep 2023 17:08:58 -0700 Subject: [PATCH 3/3] Revert type assertion. --- src/createSchema.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/createSchema.ts b/src/createSchema.ts index c9fcbcf..4c3a4b2 100644 --- a/src/createSchema.ts +++ b/src/createSchema.ts @@ -218,9 +218,7 @@ export function createSchema( if (item.type !== "string") { throw new Error(`Only string enum values are supported: ${JSON.stringify(type)}`) } - else { - acc[item.literal] = {value: index} - } + acc[item.literal as string] = {value: index} } return acc; }, {} as GraphQLEnumTypeConfig["values"])