Skip to content

Commit 279f97f

Browse files
committed
Closes #914
Squashed commit of the following: commit c4bc312d59215ca09f0b612b559bd240e87361e6 Author: Lee Byron <[email protected]> Date: Mon Jun 19 11:45:48 2017 -0700 Follow-ups, rename method to match others, fix broken test. commit 84a58a1e3e5df3d1cb9c402f4b12aa24d56ee5a7 Merge: 93c0b25 ab5f5ca Author: Lee Byron <[email protected]> Date: Mon Jun 19 11:35:37 2017 -0700 Merge branch 'getDirectiveArgsMap' of https://github.com/APIs-guru/graphql-js into APIs-guru-getDirectiveArgsMap commit ab5f5ca Author: Ivan Goncharov <[email protected]> Date: Mon Jun 12 13:31:10 2017 +0300 Add 'getDirectiveArgs' function
1 parent 93c0b25 commit 279f97f

File tree

6 files changed

+59
-54
lines changed

6 files changed

+59
-54
lines changed

src/execution/execute.js

+22-32
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+
getDirectiveValues,
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 = getDirectiveValues(
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 = getDirectiveValues(
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

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
*/
99

1010
export { execute, defaultFieldResolver, responsePathAsArray } from './execute';
11+
export { getDirectiveValues } from './values';
1112

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

src/execution/values.js

+23
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,28 @@ export function getArgumentValues(
164165
return coercedValues;
165166
}
166167

168+
/**
169+
* Prepares an object map of argument values given a directive definition
170+
* and a AST node which may contain directives. Optionally also accepts a map
171+
* of variable values.
172+
*
173+
* If the directive does not exist on the node, returns undefined.
174+
*/
175+
export function getDirectiveValues(
176+
directiveDef: GraphQLDirective,
177+
node: { directives?: ?Array<DirectiveNode> },
178+
variableValues?: ?{ [key: string]: mixed }
179+
): void | { [key: string]: mixed } {
180+
const directiveNode = node.directives && find(
181+
node.directives,
182+
directive => directive.name.value === directiveDef.name
183+
);
184+
185+
if (directiveNode) {
186+
return getArgumentValues(directiveDef, directiveNode, variableValues);
187+
}
188+
}
189+
167190
/**
168191
* Given a type and any value, return a runtime value coerced to match the type.
169192
*/

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ export {
237237
execute,
238238
defaultFieldResolver,
239239
responsePathAsArray,
240+
getDirectiveValues,
240241
} from './execution';
241242

242243
export type {

src/utilities/buildASTSchema.js

+9-19
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@
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 { getDirectiveValues } from '../execution/values';
1918

2019
import * as Kind from '../language/kinds';
2120

2221
import type {
2322
Location,
2423
DocumentNode,
25-
DirectiveNode,
2624
TypeNode,
2725
NamedTypeNode,
2826
SchemaDefinitionNode,
2927
TypeDefinitionNode,
3028
ScalarTypeDefinitionNode,
3129
ObjectTypeDefinitionNode,
30+
FieldDefinitionNode,
3231
InputValueDefinitionNode,
3332
InterfaceTypeDefinitionNode,
3433
UnionTypeDefinitionNode,
3534
EnumTypeDefinitionNode,
35+
EnumValueDefinitionNode,
3636
InputObjectTypeDefinitionNode,
3737
DirectiveDefinitionNode,
3838
} from '../language/ast';
@@ -372,7 +372,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
372372
type: produceOutputType(field.type),
373373
description: getDescription(field),
374374
args: makeInputValues(field.arguments),
375-
deprecationReason: getDeprecationReason(field.directives)
375+
deprecationReason: getDeprecationReason(field)
376376
})
377377
);
378378
}
@@ -416,7 +416,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
416416
enumValue => enumValue.name.value,
417417
enumValue => ({
418418
description: getDescription(enumValue),
419-
deprecationReason: getDeprecationReason(enumValue.directives)
419+
deprecationReason: getDeprecationReason(enumValue)
420420
})
421421
),
422422
});
@@ -457,24 +457,14 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
457457
}
458458

459459
/**
460-
* Given a collection of directives, returns the string value for the
460+
* Given a field or enum value node, returns the string value for the
461461
* deprecation reason.
462462
*/
463463
export function getDeprecationReason(
464-
directives: ?Array<DirectiveNode>,
464+
node: EnumValueDefinitionNode | FieldDefinitionNode
465465
): ?string {
466-
const deprecatedAST = directives && find(
467-
directives,
468-
directive => directive.name.value === GraphQLDeprecatedDirective.name
469-
);
470-
if (!deprecatedAST) {
471-
return;
472-
}
473-
const { reason } = getArgumentValues(
474-
GraphQLDeprecatedDirective,
475-
deprecatedAST
476-
);
477-
return (reason: any);
466+
const deprecated = getDirectiveValues(GraphQLDeprecatedDirective, node);
467+
return deprecated && (deprecated.reason: any);
478468
}
479469

480470
/**

src/utilities/extendSchema.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ export function extendSchema(
418418
description: getDescription(field),
419419
type: buildOutputFieldType(field.type),
420420
args: buildInputValues(field.arguments),
421-
deprecationReason: getDeprecationReason(field.directives),
421+
deprecationReason: getDeprecationReason(field),
422422
};
423423
});
424424
});
@@ -500,7 +500,7 @@ export function extendSchema(
500500
enumValue => enumValue.name.value,
501501
enumValue => ({
502502
description: getDescription(enumValue),
503-
deprecationReason: getDeprecationReason(enumValue.directives),
503+
deprecationReason: getDeprecationReason(enumValue),
504504
}),
505505
),
506506
});
@@ -540,7 +540,7 @@ export function extendSchema(
540540
type: buildOutputFieldType(field.type),
541541
description: getDescription(field),
542542
args: buildInputValues(field.arguments),
543-
deprecationReason: getDeprecationReason(field.directives),
543+
deprecationReason: getDeprecationReason(field),
544544
})
545545
);
546546
}

0 commit comments

Comments
 (0)