Skip to content

Commit

Permalink
Fix visitEnumValue to allow modifying the enum value (#1391)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrearden committed Apr 23, 2020
1 parent c84d1d5 commit daed81b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@

### Next

- Fix visitEnumValue to allow modifying the enum value <br/>
[@danielrearden](https://github.com/danielrearden) in [#1003](https://github.com/ardatan/graphql-tools/pull/1391)

### 5.0.0

That is a major release where we went through all issues and PRs and got the library back into great shape.
Expand Down
35 changes: 35 additions & 0 deletions src/test/directives.test.ts
Expand Up @@ -1358,6 +1358,41 @@ describe('@directives', () => {
]);
});

test("can modify enum value's value", () => {
const schema = makeExecutableSchema({
typeDefs: `
directive @value(new: String!) on ENUM_VALUE
type Query {
device: Device
}
enum Device {
PHONE
TABLET
LAPTOP @value(new: "COMPUTER")
}`,

schemaDirectives: {
value: class extends SchemaDirectiveVisitor {
public visitEnumValue(value: GraphQLEnumValue): GraphQLEnumValue {
return {
...value,
value: this.args.new,
};
}
},
},
});

const Device = schema.getType('Device') as GraphQLEnumType;
expect(Device.getValues().map((value) => value.value)).toEqual([
'PHONE',
'TABLET',
'COMPUTER',
]);
});

test('can swap names of GraphQLNamedType objects', () => {
const schema = makeExecutableSchema({
typeDefs: `
Expand Down
37 changes: 33 additions & 4 deletions src/utils/visitSchema.ts
Expand Up @@ -14,6 +14,8 @@ import {
isUnionType,
isEnumType,
isInputType,
GraphQLEnumValue,
GraphQLEnumType,
} from 'graphql';

import {
Expand All @@ -24,6 +26,7 @@ import {
SchemaVisitorMap,
} from '../Interfaces';
import updateEachKey from '../esUtils/updateEachKey';
import keyValMap from '../esUtils/keyValMap';

import { healSchema } from './heal';
import { SchemaVisitor } from './SchemaVisitor';
Expand Down Expand Up @@ -193,14 +196,40 @@ export function visitSchema(
}

if (isEnumType(type)) {
const newEnum = callMethod('visitEnum', type);
let newEnum = callMethod('visitEnum', type);

if (newEnum != null) {
updateEachKey(newEnum.getValues(), (value) =>
callMethod('visitEnumValue', value, {
const newValues: Array<GraphQLEnumValue> = [];

updateEachKey(newEnum.getValues(), (value) => {
const newValue = callMethod('visitEnumValue', value, {
enumType: newEnum,
}),
});
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (newValue) {
newValues.push(newValue);
}
});

// Recreate the enum type if any of the values changed
const valuesUpdated = newValues.some(
(value, index) => value !== newEnum.getValues()[index],
);
if (valuesUpdated) {
newEnum = new GraphQLEnumType({
...(newEnum as GraphQLEnumType).toConfig(),
values: keyValMap(
newValues,
(value) => value.name,
(value) => ({
value: value.value,
deprecationReason: value.deprecationReason,
description: value.description,
astNode: value.astNode,
}),
),
}) as GraphQLEnumType & T;
}
}

return newEnum;
Expand Down

0 comments on commit daed81b

Please sign in to comment.