Skip to content

Commit

Permalink
Added support for enums with incompatible with GraphQL naming limitat…
Browse files Browse the repository at this point in the history
…ion values
  • Loading branch information
Sukairo-02 committed May 28, 2024
1 parent 2986f94 commit 338fb72
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "drizzle-graphql",
"type": "module",
"author": "Drizzle Team",
"version": "0.8.0",
"version": "0.8.1",
"description": "Automatically generate GraphQL schema or customizable schema config fields from Drizzle ORM schema",
"scripts": {
"build": "pnpm tsx scripts/build.ts",
Expand Down
11 changes: 6 additions & 5 deletions src/util/type-converter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ import type { PgArray } from 'drizzle-orm/pg-core';
import { pascalize } from '../case-ops';
import type { ConvertedColumn } from './types';

const enumMap = new WeakMap<Record<string, GraphQLEnumType>>();
const allowedNameChars = /^[a-zA-Z0-9_]+$/;

const enumMap = new WeakMap<Object, GraphQLEnumType>();
const generateEnumCached = (column: Column, columnName: string, tableName: string): GraphQLEnumType => {
// @ts-expect-error - mapping to object's address
if (enumMap.has(column)) return enumMap.get(column);
if (enumMap.has(column)) return enumMap.get(column)!;

const gqlEnum = new GraphQLEnumType({
name: `${pascalize(tableName)}${pascalize(columnName)}Enum`,
values: Object.fromEntries(column.enumValues!.map((e) => [e, {
values: Object.fromEntries(column.enumValues!.map((e, index) => [allowedNameChars.test(e) ? e : `Option${index}`, {
value: e,
description: `Value: ${e}`,
}])),
});

// @ts-expect-error - mapping to object's address
enumMap.set(column, gqlEnum);

return gqlEnum;
Expand Down

0 comments on commit 338fb72

Please sign in to comment.