Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce TransformEnumValues #1769

Merged
merged 6 commits into from Jul 16, 2020
Merged

introduce TransformEnumValues #1769

merged 6 commits into from Jul 16, 2020

Conversation

yaacovCR
Copy link
Collaborator

@yaacovCR yaacovCR commented Jul 14, 2020

See #1634

This PR implements TransformEnumValues that allows conversion of individual enum values.

This PR does not yet rigorously support addition/deletion of values, just conversion.

Additional test cases are possibly required.

@theguild-bot
Copy link
Collaborator

theguild-bot commented Jul 14, 2020

The latest changes of this PR are available as alpha in npm: 6.0.14-alpha-1f3b14a1.0

Quickly update your package.json by running:

npx match-version @graphql-tools 6.0.14-alpha-1f3b14a1.0

@@ -1487,3 +1488,69 @@ describe('replaces field with processed fragment node', () => {
expect(result.data.test.field2).toBe('field2');
});
});

describe('transform TransformEnumValues', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Would it make sense to also add a test for changing the value of an GraphQLEnumValue?

If I'm grokking the code correctly, outside of renaming an existing value this transform would also let the user modify the "internal value" associated with an enum value without necessarily changing the name. It might be a good idea to add a test for that as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually do that already without this transform simply by adding the enum values to the resolver map within stitchSchemas just as within makeExecutableSchema. Although you can change the internal value via this transform also, it's not the easiest way to do it.

I believe we test the easier approach, but can't find it at the moment. :(

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But makes sense to add test that works in this manner anyway, good idea!

@yaacovCR
Copy link
Collaborator Author

yaacovCR commented Jul 14, 2020

To think about, and to discuss, for all potentially interested parties :)

TransformInputObjectFields allows you to pass a generic inputFieldNodeTransformer that works on top of or instead of the field renaming logic.

Might it make sense to do that here?

It would be nice also when adding enum values (whether simply by extend within additional typeDefs or via a transform), to provide a way to convert those values to a value that actually exists within the target schema. So far, we only automatically handle conversion, but not new enum values.

Parallel issue is when deleting enum values instead of renaming, how to add logic to supply a value for that enum value within the source schema if the target schema returns that value. Passing an additional (reverse) converter might also be beneficial.

These issues, however, seem to affect scalars as well. Is there a more general/better solution, like an additional transform for converting leaf values without modification of the underlying types?

@yaacovCR
Copy link
Collaborator Author

Note that the EnumValueMapper type should really have the old external value be the second argument to parallel GenericFieldMapper, but that would be a breaking change, so might make sense to change in the next major version.

export type GenericFieldMapper<F extends GraphQLFieldConfig<any, any> | GraphQLInputFieldConfig> = (
fieldConfig: F,
fieldName: string,
typeName: string,
schema: GraphQLSchema
) => F | [string, F] | null | undefined;

also refactors rebuilding ast nodes in preparation for adding enum
support
refactor TransformEnumValues to use MapLeafValues for enum value conversion, with automatic converting of changed external enum values
the passed inputTransformer should fire prior to the automatic value conversion, because otherwise when a value is removed, there would be nothing to convert.

the same is true about output transformers

when deleting the
@yaacovCR yaacovCR merged commit 1f3b14a into master Jul 16, 2020
@yaacovCR yaacovCR deleted the transform-enum-values branch July 16, 2020 12:13
@ardatan ardatan added the feature New addition or enhancement to existing solutions label Jul 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New addition or enhancement to existing solutions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants