Skip to content

Commit

Permalink
feat: add a check for deprecation errors (#93)
Browse files Browse the repository at this point in the history
* feat: add a check for deprecation errors

* move `no-deprecated-fields` into own rule

* add entry to change log for `no-deprecated-fields`

* add info on `no-depreacted-fields` to README
  • Loading branch information
Kristján Oddsson authored and jnwng committed Nov 29, 2017
1 parent d3de0db commit a04d457
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Change log
### vNEXT
- Add new rule `no-deprecated-fields` in [Kristján Oddsson](https://github.com/koddsson/)[#92](https://github.com/apollographql/eslint-plugin-graphql/pull/93)

### v1.4.1
Skipped v1.4.0 because of incorrect version tag in `package.json`
Expand Down
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ If you want to lint your GraphQL schema, rather than queries, check out [cjoudre

### Importing schema JSON

You'll need to import your [introspection query result](https://github.com/graphql/graphql-js/blob/master/src/utilities/introspectionQuery.js) or the schema as a string in the Schema Language format. This can be done if you define your ESLint config in a JS file.
You'll need to import your [introspection query result](https://github.com/graphql/graphql-js/blob/master/src/utilities/introspectionQuery.js) or the schema as a string in the Schema Language format. This can be done if you define your ESLint config in a JS file.

### Retrieving a remote GraphQL schema

Expand Down Expand Up @@ -502,3 +502,47 @@ module.exports = {
]
}
```

### No Deprecated Fields Validation Rule

The No Deprecated Fields rule validates that no deprecated fields are part of the query. This is useful to discover fields that have been marked as deprecated and shouldn't be used.

**Fail**
```
// 'id' requested and marked as deprecated in the schema
schema {
query {
viewer {
id: Int @deprecated(reason: "Use the 'uuid' field instead")
uuid: String
}
}
}
query ViewerName {
viewer {
id
}
}
```

The rule is defined as `graphql/no-deprecated-fields`.

```js
// In a file called .eslintrc.js
module.exports = {
rules: {
'graphql/no-deprecated-fields': [
'error',
{
env: 'relay',
schemaJson: require('./schema.json')
},
],
},
plugins: [
'graphql'
]
}
```
20 changes: 19 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
validate,
buildClientSchema,
buildSchema,
specifiedRules as allGraphQLValidators,
specifiedRules as allGraphQLValidators
} from 'graphql';

import {
Expand Down Expand Up @@ -222,6 +222,24 @@ export const rules = {
}));
},
},
'no-deprecated-fields': {
meta: {
schema: {
type: 'array',
items: {
additionalProperties: false,
properties: { ...defaultRuleProperties },
...schemaPropsExclusiveness,
},
},
},
create: (context) => {
return createRule(context, (optionGroup) => parseOptions({
validators: ['noDeprecatedFields'],
...optionGroup,
}));
},
},
};

function parseOptions(optionGroup) {
Expand Down
33 changes: 33 additions & 0 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,36 @@ export function typeNamesShouldBeCapitalized(context) {
}
}
}

export function noDeprecatedFields(context) {
return {
Field(node) {
const fieldDef = context.getFieldDef();
if (fieldDef && fieldDef.isDeprecated) {
const parentType = context.getParentType();
if (parentType) {
const reason = fieldDef.deprecationReason;
context.reportError(new GraphQLError(
`The field ${parentType.name}.${fieldDef.name} is deprecated.` +
(reason ? ' ' + reason : ''),
[ node ]
));
}
}
},
EnumValue(node) {
const enumVal = context.getEnumValue();
if (enumVal && enumVal.isDeprecated) {
const type = getNamedType(context.getInputType());
if (type) {
const reason = enumVal.deprecationReason;
errors.push(new GraphQLError(
`The enum value ${type.name}.${enumVal.name} is deprecated.` +
(reason ? ' ' + reason : ''),
[ node ]
));
}
}
}
}
}
55 changes: 54 additions & 1 deletion test/makeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ const parserOptions = {
greetings: () => Relay.QL\`
fragment on Greetings {
hello,
hi,
}
\`,
}
Expand Down Expand Up @@ -526,7 +527,6 @@ const parserOptions = {
column: 19
}]
},

// Example from issue report:
// https://github.com/apollostack/eslint-plugin-graphql/issues/12#issuecomment-215445880
{
Expand Down Expand Up @@ -914,6 +914,48 @@ const typeNameCapValidatorCases = {
},
]
};

const noDeprecatedFieldsCases = {
pass: [
`
@relay({
fragments: {
greetings: () => Relay.QL\`
fragment on Greetings {
hello,
}
\`,
}
})
class HelloApp extends React.Component {}
`
],
fail: [
{
options,
parser: 'babel-eslint',
code: `
@relay({
fragments: {
greetings: () => Relay.QL\`
fragment on Greetings {
hi,
}
\`,
}
})
class HelloApp extends React.Component {}
`,
errors: [{
message: "The field Greetings.hi is deprecated. Please use the more formal greeting 'hello'",
type: 'TaggedTemplateExpression',
line: 6,
column: 17
}]
}
]
};

{
let options = [{
schemaJson, tagName: 'gql',
Expand Down Expand Up @@ -1000,3 +1042,14 @@ ruleTester.run('testing capitalized-type-name rule', rules['capitalized-type-nam
valid: typeNameCapValidatorCases.pass.map((code) => ({options, parserOptions, code})),
invalid: typeNameCapValidatorCases.fail.map(({code, errors}) => ({options, parserOptions, code, errors})),
});

options = [
{
schemaJson,
env: 'relay',
},
];
ruleTester.run('testing no-deprecated-fields rule', rules['no-deprecated-fields'], {
valid: noDeprecatedFieldsCases.pass.map((code) => ({options, parser: 'babel-eslint', code})),
invalid: noDeprecatedFieldsCases.fail.map(({code, errors}) => ({options, parser: 'babel-eslint', code, errors})),
});
1 change: 1 addition & 0 deletions test/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Film {
type Greetings {
id: ID
hello: String
hi: String @deprecated(reason: "Please use the more formal greeting 'hello'")
}

type Story {
Expand Down

0 comments on commit a04d457

Please sign in to comment.