Skip to content

Commit 1b862c6

Browse files
committed
Add getDeprecationReason.js
1 parent 668a994 commit 1b862c6

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* @flow */
2+
3+
import { parse } from 'graphql/language';
4+
import getDeprecationReason from '../getDeprecationReason';
5+
6+
describe('getDeprecationReason()', () => {
7+
const generateNode = reason => (parse(`
8+
type Type {
9+
deprecatedInt: Int ${reason === undefined ? '' : `@deprecated(reason: "${reason}")`}
10+
}
11+
`).definitions[0]: Object).fields[0].directives;
12+
13+
test('gets deprecation reason', () => {
14+
const reason = 'A reason.';
15+
expect(getDeprecationReason(generateNode(reason))).toBe(reason);
16+
});
17+
18+
test('returns undefined if there\'s no deprecation reason', () => {
19+
expect(getDeprecationReason(generateNode())).toBeUndefined();
20+
});
21+
});

src/build/getDeprecationReason.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* @flow */
2+
3+
import type { DirectiveNode } from 'graphql/language';
4+
import { GraphQLDeprecatedDirective } from 'graphql/type';
5+
import { getArgumentValues } from 'graphql/execution/values';
6+
7+
/**
8+
* Retrieves the deprecation reason given the directives of an AST.
9+
*
10+
* Mirror of [getDeprecationReason]{@link https://github.com/graphql/graphql-js/blob/master/src/utilities/buildASTSchema.js#L468}.
11+
*/
12+
export default function getDeprecationReason(directives: ?Array<DirectiveNode>): ?string {
13+
const deprecatedNode = directives && directives
14+
.find(directive => directive.name.value === GraphQLDeprecatedDirective.name);
15+
16+
if (!deprecatedNode) return undefined;
17+
18+
return (getArgumentValues(GraphQLDeprecatedDirective, deprecatedNode).reason: any);
19+
}

0 commit comments

Comments
 (0)