Skip to content

Commit 62532a5

Browse files
committed
Simplify findDeprecatedUsages since "deprecationReason" is always present
Also make tests more readable
1 parent 6efefae commit 62532a5

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

src/utilities/__tests__/findDeprecatedUsages-test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ import { findDeprecatedUsages } from '../findDeprecatedUsages';
1111
describe('findDeprecatedUsages', () => {
1212
const schema = buildSchema(`
1313
enum EnumType {
14-
ONE
15-
TWO @deprecated(reason: "Some enum reason.")
14+
NORMAL_VALUE
15+
DEPRECATED_VALUE @deprecated(reason: "Some enum reason.")
1616
}
1717
1818
type Query {
19-
normalField(enumArg: EnumType): String
19+
normalField(enumArg: [EnumType]): String
2020
deprecatedField: String @deprecated(reason: "Some field reason.")
2121
}
2222
`);
2323

2424
it('should report empty set for no deprecated usages', () => {
2525
const errors = findDeprecatedUsages(
2626
schema,
27-
parse('{ normalField(enumArg: ONE) }'),
27+
parse('{ normalField(enumArg: [NORMAL_VALUE]) }'),
2828
);
2929

3030
expect(errors.length).to.equal(0);
@@ -46,13 +46,17 @@ describe('findDeprecatedUsages', () => {
4646
it('should report usage of deprecated enums', () => {
4747
const errors = findDeprecatedUsages(
4848
schema,
49-
parse('{ normalField(enumArg: TWO) }'),
49+
parse(`
50+
{
51+
normalField(enumArg: [NORMAL_VALUE, DEPRECATED_VALUE])
52+
}
53+
`),
5054
);
5155

5256
const errorMessages = errors.map(err => err.message);
5357

5458
expect(errorMessages).to.deep.equal([
55-
'The enum value "EnumType.TWO" is deprecated. Some enum reason.',
59+
'The enum value "EnumType.DEPRECATED_VALUE" is deprecated. Some enum reason.',
5660
]);
5761
});
5862
});

src/utilities/findDeprecatedUsages.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,29 @@ export function findDeprecatedUsages(
2626
ast,
2727
visitWithTypeInfo(typeInfo, {
2828
Field(node) {
29+
const parentType = typeInfo.getParentType();
2930
const fieldDef = typeInfo.getFieldDef();
30-
if (fieldDef && fieldDef.isDeprecated) {
31-
const parentType = typeInfo.getParentType();
32-
if (parentType) {
33-
const reason = fieldDef.deprecationReason;
34-
errors.push(
35-
new GraphQLError(
36-
`The field "${parentType.name}.${fieldDef.name}" is deprecated.` +
37-
(reason != null ? ' ' + reason : ''),
38-
node,
39-
),
40-
);
41-
}
31+
if (parentType && fieldDef && fieldDef.deprecationReason != null) {
32+
errors.push(
33+
new GraphQLError(
34+
`The field "${parentType.name}.${fieldDef.name}" is deprecated. ` +
35+
fieldDef.deprecationReason,
36+
node,
37+
),
38+
);
4239
}
4340
},
4441
EnumValue(node) {
42+
const type = getNamedType(typeInfo.getInputType());
4543
const enumVal = typeInfo.getEnumValue();
46-
if (enumVal && enumVal.isDeprecated) {
47-
const type = getNamedType(typeInfo.getInputType());
48-
if (type) {
49-
const reason = enumVal.deprecationReason;
50-
errors.push(
51-
new GraphQLError(
52-
`The enum value "${type.name}.${enumVal.name}" is deprecated.` +
53-
(reason != null && reason !== '' ? ' ' + reason : ''),
54-
node,
55-
),
56-
);
57-
}
44+
if (type && enumVal && enumVal.deprecationReason != null) {
45+
errors.push(
46+
new GraphQLError(
47+
`The enum value "${type.name}.${enumVal.name}" is deprecated. ` +
48+
enumVal.deprecationReason,
49+
node,
50+
),
51+
);
5852
}
5953
},
6054
}),

0 commit comments

Comments
 (0)