File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments