Skip to content

Commit

Permalink
fix bugs in EnumValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Rinehart authored and jnwng committed Jan 15, 2018
1 parent 8c5052d commit 2f91223
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/rules.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLError } from 'graphql';
import { GraphQLError, getNamedType } from 'graphql';

export function OperationsMustHaveNames(context) {
return {
Expand Down Expand Up @@ -76,12 +76,14 @@ export function noDeprecatedFields(context) {
}
},
EnumValue(node) {
const enumVal = context.getEnumValue();
// context is of type ValidationContext which doesn't export getEnumValue.
// Bypass the public API to grab that information directly from _typeInfo.
const enumVal = context._typeInfo.getEnumValue();
if (enumVal && enumVal.isDeprecated) {
const type = getNamedType(context.getInputType());
if (type) {
const reason = enumVal.deprecationReason;
errors.push(new GraphQLError(
context.reportError(new GraphQLError(
`The enum value ${type.name}.${enumVal.name} is deprecated.` +
(reason ? ' ' + reason : ''),
[ node ]
Expand Down
36 changes: 35 additions & 1 deletion test/makeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,19 @@ const noDeprecatedFieldsCases = {
}
})
class HelloApp extends React.Component {}
`,
`
@relay({
fragments: {
greetings: () => Relay.QL\`
fragment on Greetings {
image(size: LARGE) { size }
}
\`,
}
})
class HelloApp extends React.Component {}
`,
],
fail: [
{
Expand All @@ -952,7 +964,29 @@ const noDeprecatedFieldsCases = {
line: 6,
column: 17
}]
}
},
{
options,
parser: 'babel-eslint',
code: `
@relay({
fragments: {
greetings: () => Relay.QL\`
fragment on Greetings {
image(size: SMALL) { size }
}
\`,
}
})
class HelloApp extends React.Component {}
`,
errors: [{
message: "The enum value ImageSize.SMALL is deprecated. Use 'LARGE' instead",
type: 'TaggedTemplateExpression',
line: 6,
column: 29
}]
},
]
};

Expand Down
10 changes: 10 additions & 0 deletions test/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ type Greetings {
id: ID
hello: String
hi: String @deprecated(reason: "Please use the more formal greeting 'hello'")
image(size: ImageSize!): Image
}

type Image {
size: ImageSize!
}

enum ImageSize {
SMALL @deprecated(reason: "Use 'LARGE' instead")
LARGE
}

type Story {
Expand Down

0 comments on commit 2f91223

Please sign in to comment.