Skip to content

Commit 75ce8b1

Browse files
committed
Early errors for misuse of deprecation
It was too easy to accidentally misuse the field and enum deprecation API. This adds an early error explaining the correct form. Closes #185
1 parent 6f2b66d commit 75ce8b1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/type/__tests__/validation.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,4 +1653,37 @@ describe('Objects must adhere to Interface they implement', () => {
16531653
);
16541654
});
16551655

1656+
it('does not allow isDeprecated without deprecationReason on field', () => {
1657+
expect(() => {
1658+
var OldObject = new GraphQLObjectType({
1659+
name: 'OldObject',
1660+
fields: {
1661+
field: {
1662+
type: GraphQLString,
1663+
isDeprecated: true
1664+
}
1665+
}
1666+
});
1667+
1668+
return schemaWithFieldType(OldObject);
1669+
}).to.throw(
1670+
'OldObject.field should provide "deprecationReason" instead ' +
1671+
'of "isDeprecated".'
1672+
);
1673+
});
1674+
1675+
it('does not allow isDeprecated without deprecationReason on enum', () => {
1676+
expect(() =>
1677+
new GraphQLEnumType({
1678+
name: 'SomeEnum',
1679+
values: {
1680+
value: { isDeprecated: true }
1681+
}
1682+
})
1683+
).to.throw(
1684+
'SomeEnum.value should provide "deprecationReason" instead ' +
1685+
'of "isDeprecated".'
1686+
);
1687+
});
1688+
16561689
});

src/type/definition.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ function defineFieldMap(
388388
...fieldMap[fieldName],
389389
name: fieldName
390390
};
391+
invariant(
392+
!field.hasOwnProperty('isDeprecated'),
393+
`${type}.${fieldName} should provide "deprecationReason" instead ` +
394+
`of "isDeprecated".`
395+
);
391396
invariant(
392397
isOutputType(field.type),
393398
`${type}.${fieldName} field type must be Output Type but ` +
@@ -827,6 +832,11 @@ function defineEnumValues(
827832
`${type}.${valueName} must refer to an object with a "value" key ` +
828833
`representing an internal value but got: ${value}.`
829834
);
835+
invariant(
836+
!value.hasOwnProperty('isDeprecated'),
837+
`${type}.${valueName} should provide "deprecationReason" instead ` +
838+
`of "isDeprecated".`
839+
);
830840
value.name = valueName;
831841
if (isNullish(value.value)) {
832842
value.value = valueName;

0 commit comments

Comments
 (0)