public function __invoke(ValidationContext $context)
 {
     return [Node::FIELD => function (Field $fieldAST) use($context) {
         $fieldDef = $context->getFieldDef();
         if (!$fieldDef) {
             return Visitor::skipNode();
         }
         $errors = [];
         $argASTs = $fieldAST->arguments ?: [];
         $argASTMap = Utils::keyMap($argASTs, function (Argument $arg) {
             return $arg->name->value;
         });
         foreach ($fieldDef->args as $argDef) {
             $argAST = isset($argASTMap[$argDef->name]) ? $argASTMap[$argDef->name] : null;
             if (!$argAST && $argDef->getType() instanceof NonNull) {
                 $errors[] = new Error(Messages::missingArgMessage($fieldAST->name->value, $argDef->name, $argDef->getType()), [$fieldAST]);
             }
         }
         $argDefMap = Utils::keyMap($fieldDef->args, function ($def) {
             return $def->name;
         });
         foreach ($argASTs as $argAST) {
             $argDef = $argDefMap[$argAST->name->value];
             if ($argDef && !DocumentValidator::isValidLiteralValue($argAST->value, $argDef->getType())) {
                 $errors[] = new Error(Messages::badValueMessage($argAST->name->value, $argDef->getType(), Printer::doPrint($argAST->value)), [$argAST->value]);
             }
         }
         return !empty($errors) ? $errors : null;
     }];
 }
示例#2
0
 function expectInvalid($schema, $rules, $queryString, $expectedErrors)
 {
     $errors = DocumentValidator::validate($schema, Parser::parse($queryString), $rules);
     $this->assertNotEmpty($errors, 'GraphQL should not validate');
     $this->assertEquals($expectedErrors, array_map(['GraphQL\\Error', 'formatError'], $errors));
     return $errors;
 }
示例#3
0
 function expectInvalid($schema, $rules, $queryString, $errors)
 {
     $result = DocumentValidator::validate($schema, Parser::parse($queryString), $rules);
     $this->assertEquals(false, $result['isValid'], 'GraphQL should not validate');
     $this->assertEquals($errors, $result['errors']);
     return $result;
 }
 public function __invoke(ValidationContext $context)
 {
     return [Node::ARGUMENT => function (Argument $argAST) use($context) {
         $argDef = $context->getArgument();
         if ($argDef && !DocumentValidator::isValidLiteralValue($argAST->value, $argDef->getType())) {
             return new Error(self::badValueMessage($argAST->name->value, $argDef->getType(), Printer::doPrint($argAST->value)), [$argAST->value]);
         }
     }];
 }
 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();
     }];
 }
示例#6
0
 /**
  * @param Schema $schema
  * @param $requestString
  * @param mixed $rootValue
  * @param array <string, string>|null $variableValues
  * @param string|null $operationName
  * @return array
  */
 public static function execute(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null)
 {
     try {
         $source = new Source($requestString ?: '', 'GraphQL request');
         $documentAST = Parser::parse($source);
         $validationErrors = DocumentValidator::validate($schema, $documentAST);
         if (!empty($validationErrors)) {
             return ['errors' => array_map(['GraphQL\\Error', 'formatError'], $validationErrors)];
         } else {
             return Executor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName)->toArray();
         }
     } catch (Error $e) {
         return ['errors' => [Error::formatError($e)]];
     }
 }
示例#7
0
 /**
  * @param Schema $schema
  * @param $requestString
  * @param null $rootValue
  * @param null $variableValues
  * @param null $operationName
  * @return array|ExecutionResult
  */
 public static function executeAndReturnResult(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null)
 {
     try {
         $source = new Source($requestString ?: '', 'GraphQL request');
         $documentAST = Parser::parse($source);
         $validationErrors = DocumentValidator::validate($schema, $documentAST);
         if (!empty($validationErrors)) {
             return new ExecutionResult(null, $validationErrors);
         } else {
             return Executor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName);
         }
     } catch (Error $e) {
         return new ExecutionResult(null, [$e]);
     }
 }
示例#8
0
 /**
  * @param Schema $schema
  * @param $requestString
  * @param mixed $rootObject
  * @param array <string, string>|null $variableValues
  * @param string|null $operationName
  * @return array
  */
 public static function execute(Schema $schema, $requestString, $rootObject = null, $variableValues = null, $operationName = null)
 {
     try {
         $source = new Source($requestString ?: '', 'GraphQL request');
         $ast = Parser::parse($source);
         $validationResult = DocumentValidator::validate($schema, $ast);
         if (empty($validationResult['isValid'])) {
             return ['errors' => $validationResult['errors']];
         } else {
             return Executor::execute($schema, $rootObject, $ast, $operationName, $variableValues);
         }
     } catch (\Exception $e) {
         return ['errors' => Error::formatError($e)];
     }
 }
示例#9
0
 /**
  * @param Schema $schema
  * @param $requestString
  * @param null $rootValue
  * @param null $variableValues
  * @param null $operationName
  * @return array|ExecutionResult
  */
 public static function executeAndReturnResult(Schema $schema, $requestString, $rootValue = null, $contextValue = null, $variableValues = null, $operationName = null)
 {
     try {
         if ($requestString instanceof Document) {
             $documentAST = $requestString;
         } else {
             $source = new Source($requestString ?: '', 'GraphQL request');
             $documentAST = Parser::parse($source);
         }
         /** @var QueryComplexity $queryComplexity */
         $queryComplexity = DocumentValidator::getRule('QueryComplexity');
         $queryComplexity->setRawVariableValues($variableValues);
         $validationErrors = DocumentValidator::validate($schema, $documentAST);
         if (!empty($validationErrors)) {
             return new ExecutionResult(null, $validationErrors);
         } else {
             return Executor::execute($schema, $documentAST, $rootValue, $contextValue, $variableValues, $operationName);
         }
     } catch (Error $e) {
         return new ExecutionResult(null, [$e]);
     }
 }
 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();
     }];
 }
 /**
  * Helper function to test a query and the expected response.
  */
 private function validationErrors($query)
 {
     $ast = Parser::parse($query);
     return DocumentValidator::validate(StarWarsSchema::build(), $ast);
 }
示例#12
0
 function expectFailsCompleteValidation($queryString, $errors)
 {
     $this->expectInvalid($this->getDefaultSchema(), DocumentValidator::allRules(), $queryString, $errors);
 }
示例#13
0
 /**
  * Prepares an object map of argument values given a list of argument
  * definitions and list of argument AST nodes.
  *
  * @param FieldDefinition|Directive $def
  * @param FieldNode|\GraphQL\Language\AST\DirectiveNode $node
  * @param $variableValues
  * @return array
  * @throws Error
  */
 public static function getArgumentValues($def, $node, $variableValues)
 {
     $argDefs = $def->args;
     $argNodes = $node->arguments;
     if (!$argDefs || null === $argNodes) {
         return [];
     }
     $coercedValues = [];
     $undefined = Utils::undefined();
     /** @var ArgumentNode[] $argNodeMap */
     $argNodeMap = $argNodes ? Utils::keyMap($argNodes, function (ArgumentNode $arg) {
         return $arg->name->value;
     }) : [];
     foreach ($argDefs as $argDef) {
         $name = $argDef->name;
         $argType = $argDef->getType();
         $argumentNode = isset($argNodeMap[$name]) ? $argNodeMap[$name] : null;
         if (!$argumentNode) {
             if ($argDef->defaultValueExists()) {
                 $coercedValues[$name] = $argDef->defaultValue;
             } else {
                 if ($argType instanceof NonNull) {
                     throw new Error('Argument "' . $name . '" of required type ' . '"' . Utils::printSafe($argType) . '" was not provided.', [$node]);
                 }
             }
         } else {
             if ($argumentNode->value instanceof VariableNode) {
                 $variableName = $argumentNode->value->name->value;
                 if ($variableValues && array_key_exists($variableName, $variableValues)) {
                     // Note: this does not check that this variable value is correct.
                     // This assumes that this query has been validated and the variable
                     // usage here is of the correct type.
                     $coercedValues[$name] = $variableValues[$variableName];
                 } else {
                     if ($argDef->defaultValueExists()) {
                         $coercedValues[$name] = $argDef->defaultValue;
                     } else {
                         if ($argType instanceof NonNull) {
                             throw new Error('Argument "' . $name . '" of required type "' . Utils::printSafe($argType) . '" was ' . 'provided the variable "$' . $variableName . '" which was not provided ' . 'a runtime value.', [$argumentNode->value]);
                         }
                     }
                 }
             } else {
                 $valueNode = $argumentNode->value;
                 $coercedValue = Utils\AST::valueFromAST($valueNode, $argType, $variableValues);
                 if ($coercedValue === $undefined) {
                     $errors = DocumentValidator::isValidLiteralValue($argType, $valueNode);
                     $message = !empty($errors) ? "\n" . implode("\n", $errors) : '';
                     throw new Error('Argument "' . $name . '" got invalid value ' . Printer::doPrint($valueNode) . '.' . $message, [$argumentNode->value]);
                 }
                 $coercedValues[$name] = $coercedValue;
             }
         }
     }
     return $coercedValues;
 }
 protected function assertDocumentValidator($queryString, $max, array $expectedErrors = [])
 {
     $errors = DocumentValidator::validate(QuerySecuritySchema::buildSchema(), Parser::parse($queryString), [$this->getRule($max)]);
     $this->assertEquals($expectedErrors, array_map(['GraphQL\\Error\\Error', 'formatError'], $errors), $queryString);
     return $errors;
 }