reportError() public method

public reportError ( Error $error )
$error GraphQL\Error\Error
示例#1
0
 public function __invoke(ValidationContext $context)
 {
     return [Node::DIRECTIVE => function (Directive $node, $key, $parent, $path, $ancestors) use($context) {
         $directiveDef = null;
         foreach ($context->getSchema()->getDirectives() as $def) {
             if ($def->name === $node->name->value) {
                 $directiveDef = $def;
                 break;
             }
         }
         if (!$directiveDef) {
             $context->reportError(new Error(self::unknownDirectiveMessage($node->name->value), [$node]));
             return;
         }
         $appliedTo = $ancestors[count($ancestors) - 1];
         $candidateLocation = $this->getLocationForAppliedNode($appliedTo);
         if (!$candidateLocation) {
             $context->reportError(new Error(self::misplacedDirectiveMessage($node->name->value, $node->type), [$node]));
         } else {
             if (!in_array($candidateLocation, $directiveDef->locations)) {
                 $context->reportError(new Error(self::misplacedDirectiveMessage($node->name->value, $candidateLocation), [$node]));
             }
         }
     }];
 }
 public function __invoke(ValidationContext $context)
 {
     return [Node::INLINE_FRAGMENT => function (InlineFragment $node) use($context) {
         $type = $context->getType();
         if ($node->typeCondition && $type && !Type::isCompositeType($type)) {
             $context->reportError(new Error(static::inlineFragmentOnNonCompositeErrorMessage($type), [$node->typeCondition]));
         }
     }, Node::FRAGMENT_DEFINITION => function (FragmentDefinition $node) use($context) {
         $type = $context->getType();
         if ($type && !Type::isCompositeType($type)) {
             $context->reportError(new Error(static::fragmentOnNonCompositeErrorMessage($node->name->value, Printer::doPrint($node->typeCondition)), [$node->typeCondition]));
         }
     }];
 }
示例#3
0
 public function __invoke(ValidationContext $context)
 {
     $this->operationDefs = [];
     $this->fragmentDefs = [];
     return [NodeKind::OPERATION_DEFINITION => function ($node) {
         $this->operationDefs[] = $node;
         return Visitor::skipNode();
     }, NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $def) {
         $this->fragmentDefs[] = $def;
         return Visitor::skipNode();
     }, NodeKind::DOCUMENT => ['leave' => function () use($context) {
         $fragmentNameUsed = [];
         foreach ($this->operationDefs as $operation) {
             foreach ($context->getRecursivelyReferencedFragments($operation) as $fragment) {
                 $fragmentNameUsed[$fragment->name->value] = true;
             }
         }
         foreach ($this->fragmentDefs as $fragmentDef) {
             $fragName = $fragmentDef->name->value;
             if (empty($fragmentNameUsed[$fragName])) {
                 $context->reportError(new Error(self::unusedFragMessage($fragName), [$fragmentDef]));
             }
         }
     }]];
 }
 public function __invoke(ValidationContext $context)
 {
     return [NodeKind::OPERATION_DEFINITION => ['enter' => function () {
         $this->varDefMap = [];
     }, 'leave' => function (OperationDefinitionNode $operation) use($context) {
         $usages = $context->getRecursiveVariableUsages($operation);
         foreach ($usages as $usage) {
             $node = $usage['node'];
             $type = $usage['type'];
             $varName = $node->name->value;
             $varDef = isset($this->varDefMap[$varName]) ? $this->varDefMap[$varName] : null;
             if ($varDef && $type) {
                 // A var type is allowed if it is the same or more strict (e.g. is
                 // a subtype of) than the expected type. It can be more strict if
                 // the variable type is non-null when the expected type is nullable.
                 // If both are list types, the variable item type can be more strict
                 // than the expected item type (contravariant).
                 $schema = $context->getSchema();
                 $varType = TypeInfo::typeFromAST($schema, $varDef->type);
                 if ($varType && !TypeInfo::isTypeSubTypeOf($schema, $this->effectiveType($varType, $varDef), $type)) {
                     $context->reportError(new Error(self::badVarPosMessage($varName, $varType, $type), [$varDef, $node]));
                 }
             }
         }
     }], NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $varDefNode) {
         $this->varDefMap[$varDefNode->variable->name->value] = $varDefNode;
     }];
 }
示例#5
0
 public function __invoke(ValidationContext $context)
 {
     return [NodeKind::FIELD => function (FieldNode $node) use($context) {
         $type = $context->getType();
         if ($type) {
             if (Type::isLeafType($type)) {
                 if ($node->selectionSet) {
                     $context->reportError(new Error(self::noSubselectionAllowedMessage($node->name->value, $type), [$node->selectionSet]));
                 }
             } else {
                 if (!$node->selectionSet) {
                     $context->reportError(new Error(self::requiredSubselectionMessage($node->name->value, $type), [$node]));
                 }
             }
         }
     }];
 }
示例#6
0
 public function __invoke(ValidationContext $context)
 {
     return [Node::FRAGMENT_SPREAD => function (FragmentSpread $node) use($context) {
         $fragmentName = $node->name->value;
         $fragment = $context->getFragment($fragmentName);
         if (!$fragment) {
             $context->reportError(new Error(self::unknownFragmentMessage($fragmentName), [$node->name]));
         }
     }];
 }
 public function __invoke(ValidationContext $context)
 {
     return [NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) use($context) {
         $type = Utils\TypeInfo::typeFromAST($context->getSchema(), $node->type);
         // If the variable type is not an input type, return an error.
         if ($type && !Type::isInputType($type)) {
             $variableName = $node->variable->name->value;
             $context->reportError(new Error(self::nonInputTypeOnVarMessage($variableName, Printer::doPrint($node->type)), [$node->type]));
         }
     }];
 }
示例#8
0
 public function __invoke(ValidationContext $context)
 {
     $skip = function () {
         return Visitor::skipNode();
     };
     return [Node::OBJECT_TYPE_DEFINITION => $skip, Node::INTERFACE_TYPE_DEFINITION => $skip, Node::UNION_TYPE_DEFINITION => $skip, Node::INPUT_OBJECT_TYPE_DEFINITION => $skip, Node::NAMED_TYPE => function (NamedType $node, $key) use($context) {
         $typeName = $node->name->value;
         $type = $context->getSchema()->getType($typeName);
         if (!$type) {
             $context->reportError(new Error(self::unknownTypeMessage($typeName), [$node]));
         }
     }];
 }
 public function __invoke(ValidationContext $context)
 {
     return [NodeKind::ARGUMENT => function (ArgumentNode $node, $key, $parent, $path, $ancestors) use($context) {
         $argumentOf = $ancestors[count($ancestors) - 1];
         if ($argumentOf->kind === NodeKind::FIELD) {
             $fieldDef = $context->getFieldDef();
             if ($fieldDef) {
                 $fieldArgDef = null;
                 foreach ($fieldDef->args as $arg) {
                     if ($arg->name === $node->name->value) {
                         $fieldArgDef = $arg;
                         break;
                     }
                 }
                 if (!$fieldArgDef) {
                     $parentType = $context->getParentType();
                     Utils::invariant($parentType);
                     $context->reportError(new Error(self::unknownArgMessage($node->name->value, $fieldDef->name, $parentType->name), [$node]));
                 }
             }
         } else {
             if ($argumentOf->kind === NodeKind::DIRECTIVE) {
                 $directive = $context->getDirective();
                 if ($directive) {
                     $directiveArgDef = null;
                     foreach ($directive->args as $arg) {
                         if ($arg->name === $node->name->value) {
                             $directiveArgDef = $arg;
                             break;
                         }
                     }
                     if (!$directiveArgDef) {
                         $context->reportError(new Error(self::unknownDirectiveArgMessage($node->name->value, $directive->name), [$node]));
                     }
                 }
             }
         }
     }];
 }
 public function __invoke(ValidationContext $context)
 {
     return [Node::VARIABLE_DEFINITION => function (VariableDefinition $varDefAST) use($context) {
         $name = $varDefAST->variable->name->value;
         $defaultValue = $varDefAST->defaultValue;
         $type = $context->getInputType();
         if ($type instanceof NonNull && $defaultValue) {
             $context->reportError(new Error(static::defaultForNonNullArgMessage($name, $type, $type->getWrappedType()), [$defaultValue]));
         }
         if ($type && $defaultValue) {
             $errors = DocumentValidator::isValidLiteralValue($type, $defaultValue);
             if (!empty($errors)) {
                 $context->reportError(new Error(static::badValueForDefaultArgMessage($name, $type, Printer::doPrint($defaultValue), $errors), [$defaultValue]));
             }
         }
         return Visitor::skipNode();
     }, Node::SELECTION_SET => function () {
         return Visitor::skipNode();
     }, Node::FRAGMENT_DEFINITION => function () {
         return Visitor::skipNode();
     }];
 }
示例#11
0
 public function __invoke(ValidationContext $context)
 {
     return [Node::ARGUMENT => function (Argument $argAST) use($context) {
         $argDef = $context->getArgument();
         if ($argDef) {
             $errors = DocumentValidator::isValidLiteralValue($argDef->getType(), $argAST->value);
             if (!empty($errors)) {
                 $context->reportError(new Error(self::badValueMessage($argAST->name->value, $argDef->getType(), Printer::doPrint($argAST->value), $errors), [$argAST->value]));
             }
         }
         return Visitor::skipNode();
     }];
 }
示例#12
0
 public function __invoke(ValidationContext $context)
 {
     $this->knownVariableNames = [];
     return [NodeKind::OPERATION_DEFINITION => function () {
         $this->knownVariableNames = [];
     }, NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $node) use($context) {
         $variableName = $node->variable->name->value;
         if (!empty($this->knownVariableNames[$variableName])) {
             $context->reportError(new Error(self::duplicateVariableMessage($variableName), [$this->knownVariableNames[$variableName], $node->variable->name]));
         } else {
             $this->knownVariableNames[$variableName] = $node->variable->name;
         }
     }];
 }
 public function __invoke(ValidationContext $context)
 {
     $operationCount = 0;
     return [NodeKind::DOCUMENT => function (DocumentNode $node) use(&$operationCount) {
         $tmp = Utils::filter($node->definitions, function ($definition) {
             return $definition->kind === NodeKind::OPERATION_DEFINITION;
         });
         $operationCount = count($tmp);
     }, NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) use(&$operationCount, $context) {
         if (!$node->name && $operationCount > 1) {
             $context->reportError(new Error(self::anonOperationNotAloneMessage(), [$node]));
         }
     }];
 }
示例#14
0
 public function __invoke(ValidationContext $context)
 {
     $this->knownFragmentNames = [];
     return [Node::OPERATION_DEFINITION => function () {
         return Visitor::skipNode();
     }, Node::FRAGMENT_DEFINITION => function (FragmentDefinition $node) use($context) {
         $fragmentName = $node->name->value;
         if (!empty($this->knownFragmentNames[$fragmentName])) {
             $context->reportError(new Error(self::duplicateFragmentNameMessage($fragmentName), [$this->knownFragmentNames[$fragmentName], $node->name]));
         } else {
             $this->knownFragmentNames[$fragmentName] = $node->name;
         }
         return Visitor::skipNode();
     }];
 }
 public function __invoke(ValidationContext $context)
 {
     return [NodeKind::FIELD => ['leave' => function (FieldNode $fieldNode) use($context) {
         $fieldDef = $context->getFieldDef();
         if (!$fieldDef) {
             return Visitor::skipNode();
         }
         $argNodes = $fieldNode->arguments ?: [];
         $argNodeMap = [];
         foreach ($argNodes as $argNode) {
             $argNodeMap[$argNode->name->value] = $argNodes;
         }
         foreach ($fieldDef->args as $argDef) {
             $argNode = isset($argNodeMap[$argDef->name]) ? $argNodeMap[$argDef->name] : null;
             if (!$argNode && $argDef->getType() instanceof NonNull) {
                 $context->reportError(new Error(self::missingFieldArgMessage($fieldNode->name->value, $argDef->name, $argDef->getType()), [$fieldNode]));
             }
         }
     }], NodeKind::DIRECTIVE => ['leave' => function (DirectiveNode $directiveNode) use($context) {
         $directiveDef = $context->getDirective();
         if (!$directiveDef) {
             return Visitor::skipNode();
         }
         $argNodes = $directiveNode->arguments ?: [];
         $argNodeMap = [];
         foreach ($argNodes as $argNode) {
             $argNodeMap[$argNode->name->value] = $argNodes;
         }
         foreach ($directiveDef->args as $argDef) {
             $argNode = isset($argNodeMap[$argDef->name]) ? $argNodeMap[$argDef->name] : null;
             if (!$argNode && $argDef->getType() instanceof NonNull) {
                 $context->reportError(new Error(self::missingDirectiveArgMessage($directiveNode->name->value, $argDef->name, $argDef->getType()), [$directiveNode]));
             }
         }
     }]];
 }
 public function __invoke(ValidationContext $context)
 {
     return ['enter' => function (Node $node) use($context) {
         if (isset($node->directives)) {
             $knownDirectives = [];
             foreach ($node->directives as $directive) {
                 /** @var DirectiveNode $directive */
                 $directiveName = $directive->name->value;
                 if (isset($knownDirectives[$directiveName])) {
                     $context->reportError(new Error(self::duplicateDirectiveMessage($directiveName), [$knownDirectives[$directiveName], $directive]));
                 } else {
                     $knownDirectives[$directiveName] = $directive;
                 }
             }
         }
     }];
 }
示例#17
0
 public function __invoke(ValidationContext $context)
 {
     $this->knownArgNames = [];
     return [Node::FIELD => function () {
         $this->knownArgNames = [];
     }, Node::DIRECTIVE => function () {
         $this->knownArgNames = [];
     }, Node::ARGUMENT => function (Argument $node) use($context) {
         $argName = $node->name->value;
         if (!empty($this->knownArgNames[$argName])) {
             $context->reportError(new Error(self::duplicateArgMessage($argName), [$this->knownArgNames[$argName], $node->name]));
         } else {
             $this->knownArgNames[$argName] = $node->name;
         }
         return Visitor::skipNode();
     }];
 }
 public function __invoke(ValidationContext $context)
 {
     $this->knownOperationNames = [];
     return [NodeKind::OPERATION_DEFINITION => function (OperationDefinitionNode $node) use($context) {
         $operationName = $node->name;
         if ($operationName) {
             if (!empty($this->knownOperationNames[$operationName->value])) {
                 $context->reportError(new Error(self::duplicateOperationNameMessage($operationName->value), [$this->knownOperationNames[$operationName->value], $operationName]));
             } else {
                 $this->knownOperationNames[$operationName->value] = $operationName;
             }
         }
         return Visitor::skipNode();
     }, NodeKind::FRAGMENT_DEFINITION => function () {
         return Visitor::skipNode();
     }];
 }
 public function __invoke(ValidationContext $context)
 {
     return [NodeKind::INLINE_FRAGMENT => function (InlineFragmentNode $node) use($context) {
         $fragType = $context->getType();
         $parentType = $context->getParentType();
         if ($fragType && $parentType && !TypeInfo::doTypesOverlap($context->getSchema(), $fragType, $parentType)) {
             $context->reportError(new Error(self::typeIncompatibleAnonSpreadMessage($parentType, $fragType), [$node]));
         }
     }, NodeKind::FRAGMENT_SPREAD => function (FragmentSpreadNode $node) use($context) {
         $fragName = $node->name->value;
         $fragType = $this->getFragmentType($context, $fragName);
         $parentType = $context->getParentType();
         if ($fragType && $parentType && !TypeInfo::doTypesOverlap($context->getSchema(), $fragType, $parentType)) {
             $context->reportError(new Error(self::typeIncompatibleSpreadMessage($fragName, $parentType, $fragType), [$node]));
         }
     }];
 }
 public function __invoke(ValidationContext $context)
 {
     $variableNameDefined = [];
     return [NodeKind::OPERATION_DEFINITION => ['enter' => function () use(&$variableNameDefined) {
         $variableNameDefined = [];
     }, 'leave' => function (OperationDefinitionNode $operation) use(&$variableNameDefined, $context) {
         $usages = $context->getRecursiveVariableUsages($operation);
         foreach ($usages as $usage) {
             $node = $usage['node'];
             $varName = $node->name->value;
             if (empty($variableNameDefined[$varName])) {
                 $context->reportError(new Error(self::undefinedVarMessage($varName, $operation->name ? $operation->name->value : null), [$node, $operation]));
             }
         }
     }], NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $def) use(&$variableNameDefined) {
         $variableNameDefined[$def->variable->name->value] = true;
     }];
 }
 public function __invoke(ValidationContext $context)
 {
     $this->knownNames = [];
     $this->knownNameStack = [];
     return [NodeKind::OBJECT => ['enter' => function () {
         $this->knownNameStack[] = $this->knownNames;
         $this->knownNames = [];
     }, 'leave' => function () {
         $this->knownNames = array_pop($this->knownNameStack);
     }], NodeKind::OBJECT_FIELD => function (ObjectFieldNode $node) use($context) {
         $fieldName = $node->name->value;
         if (!empty($this->knownNames[$fieldName])) {
             $context->reportError(new Error(self::duplicateInputFieldMessage($fieldName), [$this->knownNames[$fieldName], $node->name]));
         } else {
             $this->knownNames[$fieldName] = $node->name;
         }
         return Visitor::skipNode();
     }];
 }
示例#22
0
 public function __invoke(ValidationContext $context)
 {
     return [Node::FIELD => function (Field $node) use($context) {
         $type = $context->getParentType();
         if ($type) {
             $fieldDef = $context->getFieldDef();
             if (!$fieldDef) {
                 // This isn't valid. Let's find suggestions, if any.
                 $suggestedTypes = [];
                 if ($type instanceof AbstractType) {
                     $schema = $context->getSchema();
                     $suggestedTypes = self::getSiblingInterfacesIncludingField($schema, $type, $node->name->value);
                     $suggestedTypes = array_merge($suggestedTypes, self::getImplementationsIncludingField($schema, $type, $node->name->value));
                 }
                 $context->reportError(new Error(static::undefinedFieldMessage($node->name->value, $type->name, $suggestedTypes), [$node]));
             }
         }
     }];
 }
示例#23
0
 public function __invoke(ValidationContext $context)
 {
     $this->variableDefs = [];
     return [Node::OPERATION_DEFINITION => ['enter' => function () {
         $this->variableDefs = [];
     }, 'leave' => function (OperationDefinition $operation) use($context) {
         $variableNameUsed = [];
         $usages = $context->getRecursiveVariableUsages($operation);
         $opName = $operation->name ? $operation->name->value : null;
         foreach ($usages as $usage) {
             $node = $usage['node'];
             $variableNameUsed[$node->name->value] = true;
         }
         foreach ($this->variableDefs as $variableDef) {
             $variableName = $variableDef->variable->name->value;
             if (empty($variableNameUsed[$variableName])) {
                 $context->reportError(new Error(self::unusedVariableMessage($variableName, $opName), [$variableDef]));
             }
         }
     }], Node::VARIABLE_DEFINITION => function ($def) {
         $this->variableDefs[] = $def;
     }];
 }