Skip to content

Allow support for custom directives on enum values #1521

Open
@harsh-2711

Description

@harsh-2711

Is your feature request related to a problem? Please describe.
I encountered a challenge when I attempted to use type-graphql to generate a schema file with types defined therein and use this schema file in another project, written in Golang. The Golang project utilizes this schema to generate types using gqlgen. The inability of type-graphql to add custom directives to enum or enum values creates a specific problem when sharing an enum between the two projects.

For instance, type-graphql allows the definition of an enum as follows:

enum TestEnum {
  FIELD_A = "fieldA",
  FIELD_B = "fieldB",
}

However, the generated schema lacks the associated string values:

enum TestEnum {
  FIELD_A,
  FIELD_B
}

Because of this, the generated schema makes the resolution of FIELD_A and FIELD_B in Golang problematic, as it lacks the direct means to resolve these fields to their associated string values. This situation is problematic, especially when the Golang service depends on these string values for various logic, including federation logic.

Describe the solution you'd like
I suggest enabling the support for custom directive string in registerEnumType function as a part of valuesConfig. This change will parallel the availability of the @directive decorator available on classes and class fields. Following is the API enhancement that I suggest -

interface DirectiveOptions {
  sdl?: string
}

type EnumValuesConfig<TEnum extends object> = Partial<Record<keyof TEnum, DescriptionOptions & DeprecationOptions & DirectiveOptions>>;

The above enhancement will help in enabling clearer and more versatile enum definitions across diverse projects and technologies.

Importantly, GraphQL schemas already support custom directives; I have implemented this functionality in the Golang project for other enums. Therefore, enabling this support in the schema file generated by type-graphql should be feasible; it's the restriction imposed by the type-graphql wrapper that’s the impediment.

Describe alternatives you've considered
I am currently maintaining enums in both services to work around this limitation. However, this approach is not scalable and is susceptible to inconsistencies when modifications occur in the enums.

Additional context
NA

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Backlog

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions