Skip to content

Commit 50ead07

Browse files
Add change types for added/removed deprecation reason on enum values (#1635)
Co-authored-by: Kamil Kisiela <[email protected]>
1 parent 02830a1 commit 50ead07

File tree

4 files changed

+110
-8
lines changed

4 files changed

+110
-8
lines changed

packages/core/__tests__/diff/enum.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,70 @@ describe('enum', () => {
142142
`Enum value 'enumA.A' deprecation reason changed from 'Old Reason' to 'New Reason'`,
143143
);
144144
});
145+
146+
test('deprecation reason added', () => {
147+
const a = buildSchema(/* GraphQL */ `
148+
type Query {
149+
fieldA: String
150+
}
151+
152+
enum enumA {
153+
A
154+
B
155+
}
156+
`);
157+
158+
const b = buildSchema(/* GraphQL */ `
159+
type Query {
160+
fieldA: String
161+
}
162+
163+
enum enumA {
164+
A @deprecated(reason: "New Reason")
165+
B
166+
}
167+
`);
168+
169+
const changes = diff(a, b);
170+
const change = findFirstChangeByPath(changes, 'enumA.A');
171+
172+
expect(changes.length).toEqual(1);
173+
expect(change.criticality.level).toEqual(CriticalityLevel.NonBreaking);
174+
expect(change.message).toEqual(
175+
`Enum value 'enumA.A' was deprecated with reason 'New Reason'`,
176+
);
177+
});
178+
179+
test('deprecation reason removed', () => {
180+
const a = buildSchema(/* GraphQL */ `
181+
type Query {
182+
fieldA: String
183+
}
184+
185+
enum enumA {
186+
A @deprecated(reason: "New Reason")
187+
B
188+
}
189+
`);
190+
191+
const b = buildSchema(/* GraphQL */ `
192+
type Query {
193+
fieldA: String
194+
}
195+
196+
enum enumA {
197+
A
198+
B
199+
}
200+
`);
201+
202+
const changes = diff(a, b);
203+
const change = findFirstChangeByPath(changes, 'enumA.A');
204+
205+
expect(changes.length).toEqual(1);
206+
expect(change.criticality.level).toEqual(CriticalityLevel.NonBreaking);
207+
expect(change.message).toEqual(
208+
`Deprecation reason was removed from enum value 'enumA.A'`,
209+
);
210+
});
145211
});

packages/core/src/diff/changes/change.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export enum ChangeType {
1919
EnumValueAdded = 'ENUM_VALUE_ADDED',
2020
EnumValueDescriptionChanged = 'ENUM_VALUE_DESCRIPTION_CHANGED',
2121
EnumValueDeprecationReasonChanged = 'ENUM_VALUE_DEPRECATION_REASON_CHANGED',
22+
EnumValueDeprecationReasonAdded = 'ENUM_VALUE_DEPRECATION_REASON_ADDED',
23+
EnumValueDeprecationReasonRemoved = 'ENUM_VALUE_DEPRECATION_REASON_REMOVED',
2224
// Field
2325
FieldRemoved = 'FIELD_REMOVED',
2426
FieldAdded = 'FIELD_ADDED',

packages/core/src/diff/changes/enum.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,38 @@ export function enumValueDeprecationReasonChanged(
5959
criticality: {
6060
level: CriticalityLevel.NonBreaking,
6161
},
62-
type: ChangeType.EnumValueDescriptionChanged,
63-
message: oldValue.deprecationReason
64-
? `Enum value '${newEnum.name}.${newValue.name}' deprecation reason changed from '${oldValue.deprecationReason}' to '${newValue.deprecationReason}'`
65-
: `Enum value '${newEnum.name}.${newValue.name}' was deprecated with reason '${newValue.deprecationReason}'`,
62+
type: ChangeType.EnumValueDeprecationReasonChanged,
63+
message: `Enum value '${newEnum.name}.${newValue.name}' deprecation reason changed from '${oldValue.deprecationReason}' to '${newValue.deprecationReason}'`,
64+
path: [newEnum.name, oldValue.name].join('.'),
65+
};
66+
}
67+
68+
export function enumValueDeprecationReasonAdded(
69+
newEnum: GraphQLEnumType,
70+
oldValue: GraphQLEnumValue,
71+
newValue: GraphQLEnumValue,
72+
): Change {
73+
return {
74+
criticality: {
75+
level: CriticalityLevel.NonBreaking,
76+
},
77+
type: ChangeType.EnumValueDeprecationReasonAdded,
78+
message: `Enum value '${newEnum.name}.${newValue.name}' was deprecated with reason '${newValue.deprecationReason}'`,
79+
path: [newEnum.name, oldValue.name].join('.'),
80+
};
81+
}
82+
83+
export function enumValueDeprecationReasonRemoved(
84+
newEnum: GraphQLEnumType,
85+
oldValue: GraphQLEnumValue,
86+
newValue: GraphQLEnumValue,
87+
): Change {
88+
return {
89+
criticality: {
90+
level: CriticalityLevel.NonBreaking,
91+
},
92+
type: ChangeType.EnumValueDeprecationReasonRemoved,
93+
message: `Deprecation reason was removed from enum value '${newEnum.name}.${newValue.name}'`,
6694
path: [newEnum.name, oldValue.name].join('.'),
6795
};
6896
}

packages/core/src/diff/enum.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {GraphQLEnumType, GraphQLEnumValue} from 'graphql';
22

3-
import {isNotEqual} from './common/compare';
3+
import {isNotEqual, isVoid} from './common/compare';
44
import {
55
enumValueRemoved,
66
enumValueAdded,
77
enumValueDescriptionChanged,
88
enumValueDeprecationReasonChanged,
9+
enumValueDeprecationReasonAdded,
10+
enumValueDeprecationReasonRemoved,
911
} from './changes/enum';
1012
import {Change} from './changes/change';
1113
import {unionArrays, diffArrays} from '../utils/arrays';
@@ -38,9 +40,13 @@ export function changesInEnum(
3840
}
3941

4042
if (isNotEqual(oldValue.deprecationReason, newValue.deprecationReason)) {
41-
changes.push(
42-
enumValueDeprecationReasonChanged(newEnum, oldValue, newValue),
43-
);
43+
if (isVoid(oldValue.deprecationReason)) {
44+
changes.push(enumValueDeprecationReasonAdded(newEnum, oldValue, newValue));
45+
} else if (isVoid(newValue.deprecationReason)) {
46+
changes.push(enumValueDeprecationReasonRemoved(newEnum, oldValue, newValue));
47+
} else {
48+
changes.push(enumValueDeprecationReasonChanged(newEnum, oldValue, newValue));
49+
}
4450
}
4551
});
4652

0 commit comments

Comments
 (0)