public function assertValidArguments(FieldInterface $field, AstFieldInterface $query, Request $request) { $requiredArguments = array_filter($field->getArguments(), function (InputField $argument) { return $argument->getType()->getKind() == TypeMap::KIND_NON_NULL; }); foreach ($query->getArguments() as $astArgument) { if (!$field->hasArgument($astArgument->getName())) { throw new ResolveException(sprintf('Unknown argument "%s" on field "%s"', $astArgument->getName(), $field->getName()), $astArgument->getLocation()); } $argument = $field->getArgument($astArgument->getName()); $argumentType = $argument->getType()->getNullableType(); switch ($argumentType->getKind()) { case TypeMap::KIND_ENUM: case TypeMap::KIND_SCALAR: case TypeMap::KIND_INPUT_OBJECT: case TypeMap::KIND_LIST: if (!$argument->getType()->isValidValue($argumentType->parseValue($astArgument->getValue()))) { throw new ResolveException(sprintf('Not valid type for argument "%s" in query "%s"', $astArgument->getName(), $field->getName()), $astArgument->getLocation()); } break; default: throw new ResolveException(sprintf('Invalid argument type "%s"', $argumentType->getName())); } if (array_key_exists($astArgument->getName(), $requiredArguments) || $argument->getConfig()->get('default') !== null) { unset($requiredArguments[$astArgument->getName()]); } } if (count($requiredArguments)) { throw new ResolveException(sprintf('Require "%s" arguments to query "%s"', implode(', ', array_keys($requiredArguments)), $query->getName())); } }
protected function parseArgumentsValues(FieldInterface $field, AstFieldInterface $ast) { $values = []; $defaults = []; foreach ($field->getArguments() as $argument) { /** @var $argument InputField */ if ($argument->getConfig()->has('default')) { $defaults[$argument->getName()] = $argument->getConfig()->getDefaultValue(); } } foreach ($ast->getArguments() as $astArgument) { $argument = $field->getArgument($astArgument->getName()); $argumentType = $argument->getType()->getNullableType(); $values[$argument->getName()] = $argumentType->parseValue($astArgument->getValue()); if (isset($defaults[$argument->getName()])) { unset($defaults[$argument->getName()]); } } return array_merge($values, $defaults); }