Logo

dev-resources.site

for different kinds of informations.

How to convert a TypeScript built-in enum to a GraphQL enum

Published at
2/5/2024
Categories
graphql
typescript
enum
programming
Author
samueldurante
Author
13 person written this
samueldurante
open
How to convert a TypeScript built-in enum to a GraphQL enum

At Woovi we are GraphQL lovers, hence we develop many helpers around this tool to bring a good developer experience.

A helper that we developed is graphqlEnumBuilder, this helper allows us to convert a TypeScript built-in enum into a GraphQLEnum without difficulty. Thus, if you add a new value in your TypeScript enum, you don't need to edit your GraphQL enum, because it's generated from your TypeScript enum.

How it works

enum Gender {
  Male = 'Male',
  Female = 'Female',
}

const GenderEnumType = graphqlEnumBuilder(
  'GenderType',
  Gender,
);
Enter fullscreen mode Exit fullscreen mode

When you call this GenderEnumType in some query or mutation as an argument and generate your schema file, note that the GenderEnumType is declared as an enum in the GraphQL notation.

Here is an example:

enum GenderType {
  Male
  Female
}
Enter fullscreen mode Exit fullscreen mode

Code solution

import { GraphQLEnumType } from 'graphql';

type EnumObject = {
  [index: string]: string;
};

type EnumObjectResult = {
  [index: string]: {
    value: string;
  };
};
export const enumBuilderValues = <T = EnumObject>(
  constants: T,
): EnumObjectResult =>
  Object.keys(constants).reduce(
    (prev, curr) => ({
      ...prev,
      [curr]: {
        value: constants[curr],
      },
    }),
    {},
  );

export const graphqlEnumBuilder = <T = EnumObject>(name: string, values: T) =>
  new GraphQLEnumType({
    name,
    values: enumBuilderValues(values),
  });
Enter fullscreen mode Exit fullscreen mode

Woovi is a Startup that enables shoppers to pay as they like. To make this possible, Woovi provides instant payment solutions for merchants to accept orders.

If you want to work with us, we are hiring!


Photo by Tom Hermans on Unsplash

enum Article's
30 articles in total
Favicon
Mastering ENUMs in Go
Favicon
The 3 kinds of Enum in Rails
Favicon
Enhancing Enum Constants in Laravel with Description and Label Attributes
Favicon
Should I stay or should I go? Enums in TypeScript - error case study
Favicon
Effective Enum
Favicon
Result in Rust: another way of error handling
Favicon
How to convert a TypeScript built-in enum to a GraphQL enum
Favicon
Leveraging Enums in Angular: Enhancing Code Clarity and Reliability
Favicon
Typescript enum vs. "as const"
Favicon
Enum Fábrica Solitão em Java
Favicon
Implementing Enums in Golang
Favicon
Elixirでリストの最初の要素を取得
Favicon
[JS TS] How to create an object with keys based on enum
Favicon
Enum ou Enumerations no PHP
Favicon
Replacing If-Else Statements with Enums in Java
Favicon
Dart enhanced enum with custom values
Favicon
Working with Enums in Rust
Favicon
Modelando algoritmos complexos com enum
Favicon
Managing Task Status Transitions in TypeScript with Enums and Object Mapping
Favicon
TypeScript Enum's vs Discriminated Unions
Favicon
Strategy Pattern no Spring Boot Usando Enum
Favicon
Problem in C Pointer
Favicon
Merge TypeScript Enums
Favicon
Agora sim, o grande ganho do enum no Dart 2.17
Favicon
How to proper use ambient enum from Definition file
Favicon
O Poder da Classe Enum com métodos abstratos
Favicon
Refactoring code with the Strate Pattern
Favicon
🔥Input Enums for a better Development Experience 🔥 😋✂️
Favicon
O pequeno grande ganho do enum no Dart 2.15
Favicon
Typescript enums drawbacks and solutions

Featured ones: