/** * Determines if a field should be included based on the @include and @skip * directives, where @skip has higher precedence than @include. * * @param ExecutionContext $exeContext * @param $directives * @return bool */ private static function shouldIncludeNode(ExecutionContext $exeContext, $directives) { $skipDirective = Directive::skipDirective(); $includeDirective = Directive::includeDirective(); /** @var \GraphQL\Language\AST\DirectiveNode $skipNode */ $skipNode = $directives ? Utils::find($directives, function (\GraphQL\Language\AST\DirectiveNode $directive) use($skipDirective) { return $directive->name->value === $skipDirective->name; }) : null; if ($skipNode) { $argValues = Values::getArgumentValues($skipDirective, $skipNode, $exeContext->variableValues); if (isset($argValues['if']) && $argValues['if'] === true) { return false; } } /** @var \GraphQL\Language\AST\DirectiveNode $includeNode */ $includeNode = $directives ? Utils::find($directives, function (\GraphQL\Language\AST\DirectiveNode $directive) use($includeDirective) { return $directive->name->value === $includeDirective->name; }) : null; if ($includeNode) { $argValues = Values::getArgumentValues($includeDirective, $includeNode, $exeContext->variableValues); if (isset($argValues['if']) && $argValues['if'] === false) { return false; } } return true; }
/** * Determines if a field should be included based on the @include and @skip * directives, where @skip has higher precedence than @include. */ private static function shouldIncludeNode(ExecutionContext $exeContext, $directives) { $skipDirective = Directive::skipDirective(); $includeDirective = Directive::includeDirective(); /** @var \GraphQL\Language\AST\Directive $skipAST */ $skipAST = $directives ? Utils::find($directives, function (\GraphQL\Language\AST\Directive $directive) use($skipDirective) { return $directive->name->value === $skipDirective->name; }) : null; if ($skipAST) { $argValues = Values::getArgumentValues($skipDirective->args, $skipAST->arguments, $exeContext->variableValues); return empty($argValues['if']); } /** @var \GraphQL\Language\AST\Directive $includeAST */ $includeAST = $directives ? Utils::find($directives, function (\GraphQL\Language\AST\Directive $directive) use($includeDirective) { return $directive->name->value === $includeDirective->name; }) : null; if ($includeAST) { $argValues = Values::getArgumentValues($includeDirective->args, $includeAST->arguments, $exeContext->variableValues); return !empty($argValues['if']); } return true; }
/** * @param Node $node */ function enter(Node $node) { $schema = $this->schema; switch ($node->kind) { case Node::SELECTION_SET: $namedType = Type::getNamedType($this->getType()); $compositeType = null; if (Type::isCompositeType($namedType)) { // isCompositeType is a type refining predicate, so this is safe. $compositeType = $namedType; } $this->parentTypeStack[] = $compositeType; // push break; case Node::FIELD: $parentType = $this->getParentType(); $fieldDef = null; if ($parentType) { $fieldDef = self::getFieldDefinition($schema, $parentType, $node); } $this->fieldDefStack[] = $fieldDef; // push $this->typeStack[] = $fieldDef ? $fieldDef->getType() : null; // push break; case Node::DIRECTIVE: $this->directive = $schema->getDirective($node->name->value); break; case Node::OPERATION_DEFINITION: $type = null; if ($node->operation === 'query') { $type = $schema->getQueryType(); } else { if ($node->operation === 'mutation') { $type = $schema->getMutationType(); } else { if ($node->operation === 'subscription') { $type = $schema->getSubscriptionType(); } } } $this->typeStack[] = $type; // push break; case Node::INLINE_FRAGMENT: case Node::FRAGMENT_DEFINITION: $typeConditionAST = $node->typeCondition; $outputType = $typeConditionAST ? self::typeFromAST($schema, $typeConditionAST) : $this->getType(); $this->typeStack[] = $outputType; // push break; case Node::VARIABLE_DEFINITION: $inputType = self::typeFromAST($schema, $node->type); $this->inputTypeStack[] = $inputType; // push break; case Node::ARGUMENT: $fieldOrDirective = $this->getDirective() ?: $this->getFieldDef(); $argDef = $argType = null; if ($fieldOrDirective) { $argDef = Utils::find($fieldOrDirective->args, function ($arg) use($node) { return $arg->name === $node->name->value; }); if ($argDef) { $argType = $argDef->getType(); } } $this->argument = $argDef; $this->inputTypeStack[] = $argType; // push break; case Node::LST: $listType = Type::getNullableType($this->getInputType()); $this->inputTypeStack[] = $listType instanceof ListOfType ? $listType->getWrappedType() : null; // push break; case Node::OBJECT_FIELD: $objectType = Type::getNamedType($this->getInputType()); $fieldType = null; if ($objectType instanceof InputObjectType) { $tmp = $objectType->getFields(); $inputField = isset($tmp[$node->name->value]) ? $tmp[$node->name->value] : null; $fieldType = $inputField ? $inputField->getType() : null; } $this->inputTypeStack[] = $fieldType; break; } }
/** * @param InterfaceType $iface * @return bool */ public function implementsInterface(InterfaceType $iface) { return !!Utils::find($this->getInterfaces(), function ($implemented) use($iface) { return $iface === $implemented; }); }
function enter(Node $node) { $schema = $this->_schema; switch ($node->kind) { case Node::SELECTION_SET: // var $compositeType: ?GraphQLCompositeType; $rawType = Type::getUnmodifiedType($this->getType()); $compositeType = null; if (Type::isCompositeType($rawType)) { // isCompositeType is a type refining predicate, so this is safe. $compositeType = $rawType; } array_push($this->_parentTypeStack, $compositeType); break; case Node::FIELD: $parentType = $this->getParentType(); $fieldDef = null; if ($parentType) { $fieldDef = self::_getFieldDef($schema, $parentType, $node); } array_push($this->_fieldDefStack, $fieldDef); array_push($this->_typeStack, $fieldDef ? $fieldDef->getType() : null); break; case Node::OPERATION_DEFINITION: $type = null; if ($node->operation === 'query') { $type = $schema->getQueryType(); } else { if ($node->operation === 'mutation') { $type = $schema->getMutationType(); } } array_push($this->_typeStack, $type); break; case Node::INLINE_FRAGMENT: case Node::FRAGMENT_DEFINITION: $type = $schema->getType($node->typeCondition->value); array_push($this->_typeStack, $type); break; case Node::VARIABLE_DEFINITION: array_push($this->_inputTypeStack, self::typeFromAST($schema, $node->type)); break; case Node::ARGUMENT: $field = $this->getFieldDef(); $argType = null; if ($field) { $argDef = Utils::find($field->args, function ($arg) use($node) { return $arg->name === $node->name->value; }); if ($argDef) { $argType = $argDef->getType(); } } array_push($this->_inputTypeStack, $argType); break; case Node::DIRECTIVE: $directive = $schema->getDirective($node->name->value); array_push($this->_inputTypeStack, $directive ? $directive->type : null); break; case Node::ARR: $arrayType = Type::getNullableType($this->getInputType()); array_push($this->_inputTypeStack, $arrayType instanceof ListOfType ? $arrayType->getWrappedType() : null); break; case Node::OBJECT_FIELD: $objectType = Type::getUnmodifiedType($this->getInputType()); $fieldType = null; if ($objectType instanceof InputObjectType) { $tmp = $objectType->getFields(); $inputField = isset($tmp[$node->name->value]) ? $tmp[$node->name->value] : null; $fieldType = $inputField ? $inputField->getType() : null; } array_push($this->_inputTypeStack, $fieldType); break; } }