Skip to content

Commit ea94ee7

Browse files
committed
Utility function getDirectiveValues + related refactoring
1 parent a79a51d commit ea94ee7

File tree

3 files changed

+65
-50
lines changed

3 files changed

+65
-50
lines changed

src/Executor/Executor.php

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use GraphQL\Language\AST\DocumentNode;
99
use GraphQL\Language\AST\FieldNode;
1010
use GraphQL\Language\AST\FragmentDefinitionNode;
11+
use GraphQL\Language\AST\FragmentSpreadNode;
12+
use GraphQL\Language\AST\InlineFragmentNode;
1113
use GraphQL\Language\AST\NodeKind;
1214
use GraphQL\Language\AST\OperationDefinitionNode;
1315
use GraphQL\Language\AST\SelectionSetNode;
@@ -508,7 +510,7 @@ private function collectFields(
508510
foreach ($selectionSet->selections as $selection) {
509511
switch ($selection->kind) {
510512
case NodeKind::FIELD:
511-
if (!$this->shouldIncludeNode($selection->directives)) {
513+
if (!$this->shouldIncludeNode($selection)) {
512514
continue;
513515
}
514516
$name = self::getFieldEntryKey($selection);
@@ -518,7 +520,7 @@ private function collectFields(
518520
$fields[$name][] = $selection;
519521
break;
520522
case NodeKind::INLINE_FRAGMENT:
521-
if (!$this->shouldIncludeNode($selection->directives) ||
523+
if (!$this->shouldIncludeNode($selection) ||
522524
!$this->doesFragmentConditionMatch($selection, $runtimeType)
523525
) {
524526
continue;
@@ -532,7 +534,7 @@ private function collectFields(
532534
break;
533535
case NodeKind::FRAGMENT_SPREAD:
534536
$fragName = $selection->name->value;
535-
if (!empty($visitedFragmentNames[$fragName]) || !$this->shouldIncludeNode($selection->directives)) {
537+
if (!empty($visitedFragmentNames[$fragName]) || !$this->shouldIncludeNode($selection)) {
536538
continue;
537539
}
538540
$visitedFragmentNames[$fragName] = true;
@@ -558,43 +560,35 @@ private function collectFields(
558560
* Determines if a field should be included based on the @include and @skip
559561
* directives, where @skip has higher precedence than @include.
560562
*
561-
* @param $directives
563+
* @param FragmentSpreadNode | FieldNode | InlineFragmentNode $node
562564
* @return bool
563565
*/
564-
private function shouldIncludeNode($directives)
566+
private function shouldIncludeNode($node)
565567
{
566-
$exeContext = $this->exeContext;
568+
$variableValues = $this->exeContext->variableValues;
567569
$skipDirective = Directive::skipDirective();
568-
$includeDirective = Directive::includeDirective();
569570

570-
/** @var \GraphQL\Language\AST\DirectiveNode $skipNode */
571-
$skipNode = $directives
572-
? Utils::find($directives, function(\GraphQL\Language\AST\DirectiveNode $directive) use ($skipDirective) {
573-
return $directive->name->value === $skipDirective->name;
574-
})
575-
: null;
571+
$skip = Values::getDirectiveValues(
572+
$skipDirective,
573+
$node,
574+
$variableValues
575+
);
576576

577-
if ($skipNode) {
578-
$argValues = Values::getArgumentValues($skipDirective, $skipNode, $exeContext->variableValues);
579-
if (isset($argValues['if']) && $argValues['if'] === true) {
580-
return false;
581-
}
577+
if (isset($skip['if']) && $skip['if'] === true) {
578+
return false;
582579
}
583580

584-
/** @var \GraphQL\Language\AST\DirectiveNode $includeNode */
585-
$includeNode = $directives
586-
? Utils::find($directives, function(\GraphQL\Language\AST\DirectiveNode $directive) use ($includeDirective) {
587-
return $directive->name->value === $includeDirective->name;
588-
})
589-
: null;
581+
$includeDirective = Directive::includeDirective();
590582

591-
if ($includeNode) {
592-
$argValues = Values::getArgumentValues($includeDirective, $includeNode, $exeContext->variableValues);
593-
if (isset($argValues['if']) && $argValues['if'] === false) {
594-
return false;
595-
}
596-
}
583+
$include = Values::getDirectiveValues(
584+
$includeDirective,
585+
$node,
586+
$variableValues
587+
);
597588

589+
if (isset($include['if']) && $include['if'] === false) {
590+
return false;
591+
}
598592
return true;
599593
}
600594

src/Executor/Values.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
use GraphQL\Error\Error;
66
use GraphQL\Error\InvariantViolation;
77
use GraphQL\Language\AST\ArgumentNode;
8+
use GraphQL\Language\AST\DirectiveNode;
9+
use GraphQL\Language\AST\EnumValueDefinitionNode;
10+
use GraphQL\Language\AST\FieldDefinitionNode;
811
use GraphQL\Language\AST\FieldNode;
9-
use GraphQL\Language\AST\NullValueNode;
10-
use GraphQL\Language\AST\ValueNode;
12+
use GraphQL\Language\AST\FragmentSpreadNode;
13+
use GraphQL\Language\AST\InlineFragmentNode;
1114
use GraphQL\Language\AST\VariableNode;
1215
use GraphQL\Language\AST\VariableDefinitionNode;
1316
use GraphQL\Language\Printer;
1417
use GraphQL\Schema;
1518
use GraphQL\Type\Definition\Directive;
16-
use GraphQL\Type\Definition\FieldArgument;
1719
use GraphQL\Type\Definition\FieldDefinition;
1820
use GraphQL\Type\Definition\InputObjectType;
1921
use GraphQL\Type\Definition\InputType;
@@ -161,6 +163,33 @@ public static function getArgumentValues($def, $node, $variableValues = null)
161163
return $coercedValues;
162164
}
163165

166+
/**
167+
* Prepares an object map of argument values given a directive definition
168+
* and a AST node which may contain directives. Optionally also accepts a map
169+
* of variable values.
170+
*
171+
* If the directive does not exist on the node, returns undefined.
172+
*
173+
* @param Directive $directiveDef
174+
* @param FragmentSpreadNode | FieldNode | InlineFragmentNode | EnumValueDefinitionNode | FieldDefinitionNode $node
175+
* @param array|null $variableValues
176+
*
177+
* @return array|null
178+
*/
179+
public static function getDirectiveValues(Directive $directiveDef, $node, $variableValues = null)
180+
{
181+
if (isset($node->directives) && is_array($node->directives)) {
182+
$directiveNode = Utils::find($node->directives, function(DirectiveNode $directive) use ($directiveDef) {
183+
return $directive->name->value === $directiveDef->name;
184+
});
185+
186+
if ($directiveNode) {
187+
return self::getArgumentValues($directiveDef, $directiveNode, $variableValues);
188+
}
189+
}
190+
return null;
191+
}
192+
164193
/**
165194
* @deprecated as of 8.0 (Moved to Utils\AST::valueFromAST)
166195
*

src/Utils/BuildSchema.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use GraphQL\Language\AST\DirectiveDefinitionNode;
77
use GraphQL\Language\AST\DocumentNode;
88
use GraphQL\Language\AST\EnumTypeDefinitionNode;
9+
use GraphQL\Language\AST\EnumValueDefinitionNode;
10+
use GraphQL\Language\AST\FieldDefinitionNode;
911
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
1012
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
1113
use GraphQL\Language\AST\NodeKind;
@@ -359,7 +361,7 @@ function($field) {
359361
'type' => $this->produceOutputType($field->type),
360362
'description' => $this->getDescription($field),
361363
'args' => $this->makeInputValues($field->arguments),
362-
'deprecationReason' => $this->getDeprecationReason($field->directives)
364+
'deprecationReason' => $this->getDeprecationReason($field)
363365
];
364366
}
365367
);
@@ -416,7 +418,7 @@ function($enumValue) {
416418
function($enumValue) {
417419
return [
418420
'description' => $this->getDescription($enumValue),
419-
'deprecationReason' => $this->getDeprecationReason($enumValue->directives)
421+
'deprecationReason' => $this->getDeprecationReason($enumValue)
420422
];
421423
}
422424
)
@@ -461,23 +463,13 @@ private function makeInputObjectDef(InputObjectTypeDefinitionNode $def)
461463
* Given a collection of directives, returns the string value for the
462464
* deprecation reason.
463465
*
464-
* @param $directives
466+
* @param EnumValueDefinitionNode | FieldDefinitionNode $node
467+
* @return string
465468
*/
466-
private function getDeprecationReason($directives)
469+
private function getDeprecationReason($node)
467470
{
468-
$deprecatedAST = $directives ? Utils::find(
469-
$directives,
470-
function($directive) {
471-
return $directive->name->value === Directive::deprecatedDirective()->name;
472-
}
473-
) : null;
474-
if (!$deprecatedAST) {
475-
return;
476-
}
477-
return Values::getArgumentValues(
478-
Directive::deprecatedDirective(),
479-
$deprecatedAST
480-
)['reason'];
471+
$deprecated = Values::getDirectiveValues(Directive::deprecatedDirective(), $node);
472+
return isset($deprecated['reason']) ? $deprecated['reason'] : null;
481473
}
482474

483475
/**

0 commit comments

Comments
 (0)