isValidLiteralValue() public static method

Note that this only validates literal values, variables are assumed to provide values of the correct type.
public static isValidLiteralValue ( Type $type, $valueNode ) : array
$type GraphQL\Type\Definition\Type
return array
Ejemplo n.º 1
0
 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;
     }];
 }
 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]);
         }
     }];
 }
Ejemplo n.º 3
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();
     }];
 }
Ejemplo n.º 4
0
 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();
     }];
 }
Ejemplo n.º 5
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;
 }