Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Unable to mutate enum value through visitEnumValue #47

Closed
danielrearden opened this issue Apr 13, 2020 · 2 comments
Closed

Unable to mutate enum value through visitEnumValue #47

danielrearden opened this issue Apr 13, 2020 · 2 comments

Comments

@danielrearden
Copy link

Just hit this issue while trying to implement a simple directive that would let you change the value of an enum value:

export class ValueDirective extends SchemaDirectiveVisitor {
  visitEnumValue(
    enumValue: GraphQLEnumValue,
  ): GraphQLEnumValue {
  return {
    ...enumValue,
    value: this.args.string,
  }  
}

The intent here is to have an SDL-only way to deal with "internal" values of enum values without having to utilize the resolver map.

The issues is that GraphQLEnumType utilizes several private properties that are derived from the initial set of values. I think the entire GraphQLEnumType would need to be recreated with an updated config whenever the visitor is called or after all directives are ran, but off-hand I'm not sure how feasible either approach is.

@yaacovCR
Copy link
Owner

Good catch, visitEnumValue has been broken since v14, see related issues fixed in ardatan#953 which did not include this fix.

Can you reopen this issue on upstream graphql-tools repository? Fork will soon be deprecated, see ardatan#1308

You can help out also by creating a PR with a failing test!

The fix will involve recreating the entire type with every changed value, as you suggest, see lines around https://github.com/apollographql/graphql-tools/blob/next/src/utils/visitSchema.ts#L200

Feel free to take a stab at it.

Not sure if your intent with this directive is best practice, as it does leak some private implementation details. But sure seems convenient.

@danielrearden
Copy link
Author

Excellent! I'm elated to see all this work finally get merged upstream. I'll close this out and open a PR upstream with a failing test when I get a moment.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants