1 | <?php |
||
24 | class ResolveValidator implements ResolveValidatorInterface |
||
25 | { |
||
26 | |||
27 | /** @var ExecutionContext */ |
||
28 | private $executionContext; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * ResolveValidator constructor. |
||
33 | * |
||
34 | * @param ExecutionContext $executionContext |
||
35 | */ |
||
36 | 70 | public function __construct(ExecutionContext $executionContext) |
|
37 | { |
||
38 | 70 | $this->executionContext = $executionContext; |
|
39 | 70 | } |
|
40 | |||
41 | 67 | public function assetTypeHasField(AbstractType $objectType, AstFieldInterface $ast) |
|
42 | { |
||
43 | /** @var AbstractObjectType $objectType */ |
||
44 | 67 | if ($this->executionContext->getField($objectType, $ast->getName()) !== null) { |
|
45 | 67 | return; |
|
46 | } |
||
47 | |||
48 | 3 | if (!(TypeService::isObjectType($objectType) || TypeService::isInputObjectType($objectType)) || !$objectType->hasField($ast->getName())) { |
|
49 | $availableFieldNames = implode(', ', array_map(function (FieldInterface $field) { |
||
50 | 3 | return sprintf('"%s"', $field->getName()); |
|
51 | 3 | }, $objectType->getFields())); |
|
52 | 3 | throw new ResolveException(sprintf('Field "%s" not found in type "%s". Available fields are: %s', $ast->getName(), $objectType->getNamedType()->getName(), $availableFieldNames), $ast->getLocation()); |
|
53 | } |
||
54 | } |
||
55 | |||
56 | public function assertValidArguments(FieldInterface $field, AstFieldInterface $query, Request $request) |
||
57 | { |
||
58 | 66 | $requiredArguments = array_filter($field->getArguments(), function (InputField $argument) { |
|
59 | 49 | return $argument->getType()->getKind() === TypeMap::KIND_NON_NULL; |
|
60 | 66 | }); |
|
61 | |||
62 | 66 | foreach ($query->getArguments() as $astArgument) { |
|
63 | 37 | if (!$field->hasArgument($astArgument->getName())) { |
|
64 | 1 | throw new ResolveException(sprintf('Unknown argument "%s" on field "%s"', $astArgument->getName(), $field->getName()), $astArgument->getLocation()); |
|
65 | } |
||
66 | |||
67 | 37 | $argument = $field->getArgument($astArgument->getName()); |
|
68 | 37 | $argumentType = $argument->getType()->getNullableType(); |
|
69 | |||
70 | 37 | switch ($argumentType->getKind()) { |
|
71 | 37 | case TypeMap::KIND_ENUM: |
|
72 | 37 | case TypeMap::KIND_SCALAR: |
|
73 | 37 | case TypeMap::KIND_INPUT_OBJECT: |
|
74 | 37 | case TypeMap::KIND_LIST: |
|
75 | 37 | if (!$argument->getType()->isValidValue($astArgument->getValue())) { |
|
76 | 6 | $error = $argument->getType()->getValidationError($astArgument->getValue()) ?: '(no details available)'; |
|
77 | 6 | throw new ResolveException(sprintf('Not valid type for argument "%s" in query "%s": %s', $astArgument->getName(), $field->getName(), $error), $astArgument->getLocation()); |
|
78 | } |
||
79 | |||
80 | 34 | break; |
|
81 | |||
82 | default: |
||
83 | throw new ResolveException(sprintf('Invalid argument type "%s"', $argumentType->getName())); |
||
84 | 34 | } |
|
85 | |||
86 | 34 | if (array_key_exists($astArgument->getName(), $requiredArguments) || $argument->getConfig()->get('defaultValue') !== null) { |
|
87 | 11 | unset($requiredArguments[$astArgument->getName()]); |
|
88 | 11 | } |
|
89 | 63 | } |
|
90 | |||
91 | 63 | if (count($requiredArguments)) { |
|
92 | 3 | throw new ResolveException(sprintf('Require "%s" arguments to query "%s"', implode(', ', array_keys($requiredArguments)), $query->getName())); |
|
93 | } |
||
94 | 61 | } |
|
95 | |||
96 | 61 | public function assertValidResolvedValueForField(FieldInterface $field, $resolvedValue) |
|
109 | |||
110 | 6 | public function assertTypeImplementsInterface(AbstractType $type, AbstractInterfaceType $interface) |
|
111 | { |
||
112 | 6 | if ($type instanceof AbstractObjectType) { |
|
122 | |||
123 | 2 | public function assertTypeInUnionTypes(AbstractType $type, AbstractUnionType $unionType) |
|
133 | } |
||
134 |