Skip to content

Commit ab5f5ca

Browse files
committed
Add 'getDirectiveArgs' function
1 parent c4f2656 commit ab5f5ca

File tree

7 files changed

+60
-53
lines changed

7 files changed

+60
-53
lines changed

src/execution/execute.js

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
import { forEach, isCollection } from 'iterall';
1212

1313
import { GraphQLError, locatedError } from '../error';
14-
import find from '../jsutils/find';
1514
import invariant from '../jsutils/invariant';
1615
import isNullish from '../jsutils/isNullish';
1716
import { typeFromAST } from '../utilities/typeFromAST';
1817
import * as Kind from '../language/kinds';
19-
import { getVariableValues, getArgumentValues } from './values';
18+
import {
19+
getVariableValues,
20+
getArgumentValues,
21+
getDirectiveArgs,
22+
} from './values';
2023
import {
2124
GraphQLObjectType,
2225
GraphQLList,
@@ -44,11 +47,11 @@ import {
4447
GraphQLSkipDirective,
4548
} from '../type/directives';
4649
import type {
47-
DirectiveNode,
4850
DocumentNode,
4951
OperationDefinitionNode,
5052
SelectionSetNode,
5153
FieldNode,
54+
FragmentSpreadNode,
5255
InlineFragmentNode,
5356
FragmentDefinitionNode,
5457
} from '../language/ast';
@@ -512,7 +515,7 @@ export function collectFields(
512515
const selection = selectionSet.selections[i];
513516
switch (selection.kind) {
514517
case Kind.FIELD:
515-
if (!shouldIncludeNode(exeContext, selection.directives)) {
518+
if (!shouldIncludeNode(exeContext, selection)) {
516519
continue;
517520
}
518521
const name = getFieldEntryKey(selection);
@@ -522,7 +525,7 @@ export function collectFields(
522525
fields[name].push(selection);
523526
break;
524527
case Kind.INLINE_FRAGMENT:
525-
if (!shouldIncludeNode(exeContext, selection.directives) ||
528+
if (!shouldIncludeNode(exeContext, selection) ||
526529
!doesFragmentConditionMatch(exeContext, selection, runtimeType)) {
527530
continue;
528531
}
@@ -537,7 +540,7 @@ export function collectFields(
537540
case Kind.FRAGMENT_SPREAD:
538541
const fragName = selection.name.value;
539542
if (visitedFragmentNames[fragName] ||
540-
!shouldIncludeNode(exeContext, selection.directives)) {
543+
!shouldIncludeNode(exeContext, selection)) {
541544
continue;
542545
}
543546
visitedFragmentNames[fragName] = true;
@@ -565,38 +568,25 @@ export function collectFields(
565568
*/
566569
function shouldIncludeNode(
567570
exeContext: ExecutionContext,
568-
directives: ?Array<DirectiveNode>
571+
node: FragmentSpreadNode | FieldNode | InlineFragmentNode,
569572
): boolean {
570-
const skipNode = directives && find(
571-
directives,
572-
directive => directive.name.value === GraphQLSkipDirective.name
573+
const skip = getDirectiveArgs(
574+
GraphQLSkipDirective,
575+
node,
576+
exeContext.variableValues
573577
);
574-
if (skipNode) {
575-
const { if: skipIf } = getArgumentValues(
576-
GraphQLSkipDirective,
577-
skipNode,
578-
exeContext.variableValues
579-
);
580-
if (skipIf === true) {
581-
return false;
582-
}
578+
if (skip && skip.if === true) {
579+
return false;
583580
}
584581

585-
const includeNode = directives && find(
586-
directives,
587-
directive => directive.name.value === GraphQLIncludeDirective.name
582+
const include = getDirectiveArgs(
583+
GraphQLIncludeDirective,
584+
node,
585+
exeContext.variableValues
588586
);
589-
if (includeNode) {
590-
const { if: includeIf } = getArgumentValues(
591-
GraphQLIncludeDirective,
592-
includeNode,
593-
exeContext.variableValues
594-
);
595-
if (includeIf === false) {
596-
return false;
597-
}
587+
if (include && include.if === false) {
588+
return false;
598589
}
599-
600590
return true;
601591
}
602592

src/execution/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10+
export { getDirectiveArgs } from './values';
1011
export { execute, defaultFieldResolver, responsePathAsArray } from './execute';
1112

1213
export type { ExecutionResult } from './execute';
14+

src/execution/values.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import { createIterator, isCollection } from 'iterall';
1212

1313
import { GraphQLError } from '../error';
14+
import find from '../jsutils/find';
1415
import invariant from '../jsutils/invariant';
1516
import isNullish from '../jsutils/isNullish';
1617
import isInvalid from '../jsutils/isInvalid';
@@ -164,6 +165,26 @@ export function getArgumentValues(
164165
return coercedValues;
165166
}
166167

168+
/**
169+
* Prepares an object map of argument values given a directive definitions
170+
* and AST node.
171+
*/
172+
export function getDirectiveArgs(
173+
directiveDef: GraphQLDirective,
174+
node: { directives?: ?Array<DirectiveNode> },
175+
variableValues?: ?{ [key: string]: mixed }
176+
): ?{ [key: string]: mixed } {
177+
const directiveNode = node.directives && find(
178+
node.directives,
179+
directive => directive.name.value === directiveDef.name
180+
);
181+
182+
if (directiveNode) {
183+
return getArgumentValues(directiveDef, directiveNode, variableValues);
184+
}
185+
return null;
186+
}
187+
167188
/**
168189
* Given a type and any value, return a runtime value coerced to match the type.
169190
*/

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ export {
237237
execute,
238238
defaultFieldResolver,
239239
responsePathAsArray,
240+
getDirectiveArgs,
240241
} from './execution';
241242

242243
export type {

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ type Query {
543543
name: 'VALUE',
544544
description: '',
545545
isDeprecated: false,
546-
deprecationReason: undefined,
546+
deprecationReason: null,
547547
value: 'VALUE'
548548
},
549549
{

src/utilities/buildASTSchema.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
* of patent rights can be found in the PATENTS file in the same directory.
99
*/
1010

11-
import find from '../jsutils/find';
1211
import invariant from '../jsutils/invariant';
1312
import keyValMap from '../jsutils/keyValMap';
1413
import { valueFromAST } from './valueFromAST';
1514
import { TokenKind } from '../language/lexer';
1615
import { parse } from '../language/parser';
1716
import type { Source } from '../language/source';
18-
import { getArgumentValues } from '../execution/values';
17+
import { getDirectiveArgs } from '../execution/values';
1918

2019
import {
2120
LIST_TYPE,
@@ -34,17 +33,18 @@ import {
3433
import type {
3534
Location,
3635
DocumentNode,
37-
DirectiveNode,
3836
TypeNode,
3937
NamedTypeNode,
4038
SchemaDefinitionNode,
4139
TypeDefinitionNode,
4240
ScalarTypeDefinitionNode,
4341
ObjectTypeDefinitionNode,
42+
FieldDefinitionNode,
4443
InputValueDefinitionNode,
4544
InterfaceTypeDefinitionNode,
4645
UnionTypeDefinitionNode,
4746
EnumTypeDefinitionNode,
47+
EnumValueDefinitionNode,
4848
InputObjectTypeDefinitionNode,
4949
DirectiveDefinitionNode,
5050
} from '../language/ast';
@@ -381,7 +381,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
381381
type: produceOutputType(field.type),
382382
description: getDescription(field),
383383
args: makeInputValues(field.arguments),
384-
deprecationReason: getDeprecationReason(field.directives)
384+
deprecationReason: getDeprecationReason(field)
385385
})
386386
);
387387
}
@@ -425,7 +425,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
425425
enumValue => enumValue.name.value,
426426
enumValue => ({
427427
description: getDescription(enumValue),
428-
deprecationReason: getDeprecationReason(enumValue.directives)
428+
deprecationReason: getDeprecationReason(enumValue)
429429
})
430430
),
431431
});
@@ -466,24 +466,17 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
466466
}
467467

468468
/**
469-
* Given a collection of directives, returns the string value for the
469+
* Given a field or enum value node, returns the string value for the
470470
* deprecation reason.
471471
*/
472472
export function getDeprecationReason(
473-
directives: ?Array<DirectiveNode>,
473+
node: EnumValueDefinitionNode | FieldDefinitionNode
474474
): ?string {
475-
const deprecatedAST = directives && find(
476-
directives,
477-
directive => directive.name.value === GraphQLDeprecatedDirective.name
478-
);
479-
if (!deprecatedAST) {
480-
return;
481-
}
482-
const { reason } = getArgumentValues(
475+
const deprecated = getDirectiveArgs(
483476
GraphQLDeprecatedDirective,
484-
deprecatedAST
477+
node
485478
);
486-
return (reason: any);
479+
return deprecated && (deprecated.reason: any);
487480
}
488481

489482
/**

src/utilities/extendSchema.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ export function extendSchema(
430430
description: getDescription(field),
431431
type: buildOutputFieldType(field.type),
432432
args: buildInputValues(field.arguments),
433-
deprecationReason: getDeprecationReason(field.directives),
433+
deprecationReason: getDeprecationReason(field),
434434
};
435435
});
436436
});
@@ -511,7 +511,7 @@ export function extendSchema(
511511
enumValue => enumValue.name.value,
512512
enumValue => ({
513513
description: getDescription(enumValue),
514-
deprecationReason: getDeprecationReason(enumValue.directives),
514+
deprecationReason: getDeprecationReason(enumValue),
515515
}),
516516
),
517517
});
@@ -551,7 +551,7 @@ export function extendSchema(
551551
type: buildOutputFieldType(field.type),
552552
description: getDescription(field),
553553
args: buildInputValues(field.arguments),
554-
deprecationReason: getDeprecationReason(field.directives),
554+
deprecationReason: getDeprecationReason(field),
555555
})
556556
);
557557
}

0 commit comments

Comments
 (0)