Skip to content

Commit 30bab14

Browse files
committed
Add 'getAppliedDirectives' function
1 parent c4f2656 commit 30bab14

File tree

6 files changed

+61
-56
lines changed

6 files changed

+61
-56
lines changed

src/execution/execute.js

Lines changed: 18 additions & 34 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+
getAppliedDirectives,
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,19 @@ 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 directives = getAppliedDirectives(
574+
[ GraphQLSkipDirective, GraphQLIncludeDirective ],
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 (directives.skip && directives.skip.if === true) {
579+
return false;
583580
}
584-
585-
const includeNode = directives && find(
586-
directives,
587-
directive => directive.name.value === GraphQLIncludeDirective.name
588-
);
589-
if (includeNode) {
590-
const { if: includeIf } = getArgumentValues(
591-
GraphQLIncludeDirective,
592-
includeNode,
593-
exeContext.variableValues
594-
);
595-
if (includeIf === false) {
596-
return false;
597-
}
581+
if (directives.include && directives.include.if === false) {
582+
return false;
598583
}
599-
600584
return true;
601585
}
602586

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 { getAppliedDirectives } from './values';
1011
export { execute, defaultFieldResolver, responsePathAsArray } from './execute';
1112

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

src/execution/values.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,31 @@ export function getArgumentValues(
164164
return coercedValues;
165165
}
166166

167+
/**
168+
* Prepares an object map of directive name to argument values given a list of
169+
* directive definitions and AST node.
170+
*/
171+
export function getAppliedDirectives(
172+
directiveDefs: Array<GraphQLDirective>,
173+
node: { directives?: ?Array<DirectiveNode> },
174+
variableValues?: ?{ [key: string]: mixed }
175+
): { [key: string]: { [key: string]: mixed } } {
176+
if (!node.directives) {
177+
return {};
178+
}
179+
180+
const result = {};
181+
node.directives.forEach(directive => {
182+
directiveDefs.forEach(def => {
183+
const name = directive.name.value;
184+
if (name === def.name) {
185+
result[name] = getArgumentValues(def, directive, variableValues);
186+
}
187+
});
188+
});
189+
return result;
190+
}
191+
167192
/**
168193
* Given a type and any value, return a runtime value coerced to match the type.
169194
*/

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+
getAppliedDirectives,
240241
} from './execution';
241242

242243
export type {

src/utilities/buildASTSchema.js

Lines changed: 12 additions & 19 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 { getAppliedDirectives } 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(
483-
GraphQLDeprecatedDirective,
484-
deprecatedAST
485-
);
486-
return (reason: any);
475+
const deprecated = getAppliedDirectives(
476+
[ GraphQLDeprecatedDirective ],
477+
node
478+
)[GraphQLDeprecatedDirective.name];
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)