Skip to content

Commit c8332f6

Browse files
committed
Merge remote-tracking branch 'origin/relay'
2 parents 45ed2ba + d3ecfe7 commit c8332f6

File tree

6 files changed

+40
-45
lines changed

6 files changed

+40
-45
lines changed

Tests/Library/Field/FieldTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public function testInlineFieldCreation()
4141
}
4242
]);
4343

44-
$this->assertEquals('true', $fieldWithResolve->resolve(true, [], $resolveInfo), 'Resolve bool to string');
45-
$this->assertEquals('CTO', $fieldWithResolve->resolve('CTO', [], $resolveInfo));
44+
$this->assertEquals(null, $fieldWithResolve->resolve(true, [], $resolveInfo), 'Resolve bool to string');
4645

4746
$fieldWithResolve->setType(new IntType());
4847
$this->assertEquals(new IntType(), $fieldWithResolve->getType());

Tests/Library/Validator/TypeValidationRuleTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TypeValidationRuleTest extends \PHPUnit_Framework_TestCase
2929

3030
protected function setUp()
3131
{
32-
$this->rule = new TypeValidationRule(new ConfigValidator());
32+
$this->rule = new TypeValidationRule(ConfigValidator::getInstance());
3333
}
3434

3535

src/Config/AbstractConfig.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator;
13-
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidatorInterface;
1413
use Youshido\GraphQL\Validator\Exception\ConfigurationException;
1514
use Youshido\GraphQL\Validator\Exception\ValidationException;
1615

@@ -32,9 +31,6 @@ abstract class AbstractConfig
3231

3332
protected $extraFieldsAllowed = null;
3433

35-
/** @var ConfigValidatorInterface */
36-
protected $validator;
37-
3834
/**
3935
* TypeConfig constructor.
4036
* @param array $configData
@@ -53,10 +49,11 @@ public function __construct(array $configData, $contextObject = null, $finalClas
5349
$this->contextObject = $contextObject;
5450
$this->data = $configData;
5551
$this->finalClass = $finalClass;
56-
$this->validator = new ConfigValidator($contextObject);
5752

58-
if (!$this->validator->validate($this->data, $this->getContextRules(), $this->extraFieldsAllowed)) {
59-
throw new ConfigurationException('Config is not valid for ' . ($contextObject ? get_class($contextObject) : null) . "\n" . implode("\n", $this->validator->getErrorsArray(false)));
53+
$validator = ConfigValidator::getInstance();
54+
55+
if (!$validator->validate($this->data, $this->getContextRules(), $this->extraFieldsAllowed)) {
56+
throw new ConfigurationException('Config is not valid for ' . ($contextObject ? get_class($contextObject) : null) . "\n" . implode("\n", $validator->getErrorsArray(false)));
6057
}
6158

6259
$this->build();

src/Execution/Processor.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,13 @@ protected function resolveFieldValue(AbstractField $field, $contextValue, Query
222222
{
223223
$resolveInfo = new ResolveInfo($field, $query->getFields(), $field->getType(), $this->executionContext);
224224

225-
return $field->resolve($contextValue, $this->parseArgumentsValues($field, $query), $resolveInfo);
225+
if ($resolveFunc = $field->getConfig()->getResolveFunction()) {
226+
return $resolveFunc($contextValue, $this->parseArgumentsValues($field, $query), $resolveInfo);
227+
} elseif ($propertyValue = TypeService::getPropertyValue($contextValue, $field->getName())) {
228+
return $propertyValue;
229+
} else {
230+
return $field->resolve($contextValue, $this->parseArgumentsValues($field, $query), $resolveInfo);
231+
}
226232
}
227233

228234
/**
@@ -251,9 +257,10 @@ protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, Abstra
251257
$resolved = true;
252258
}
253259

254-
if ($field->getConfig()->getResolveFunction()) {
260+
if ($resolveFunction = $field->getConfig()->getResolveFunction()) {
255261
$resolveInfo = new ResolveInfo($field, [$fieldAst], $field->getType(), $this->executionContext);
256-
$resolverValue = $field->resolve($resolved ? $resolverValue : $contextValue, $fieldAst->getKeyValueArguments(), $resolveInfo);
262+
263+
$resolverValue = $resolveFunction($resolved ? $resolverValue : $contextValue, $fieldAst->getKeyValueArguments(), $resolveInfo);
257264
}
258265

259266
if (!$resolverValue && !$resolved) {

src/Field/AbstractField.php

-8
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ public function setType($type)
5959
*/
6060
public function resolve($value, array $args, ResolveInfo $info)
6161
{
62-
if ($resolveFunc = $this->getConfig()->getResolveFunction()) {
63-
$info->setReturnType($this->getType());
64-
65-
return $resolveFunc($value, $args, $info);
66-
} elseif ($propertyValue = TypeService::getPropertyValue($value, $this->getName())) {
67-
return $propertyValue;
68-
}
69-
7062
return null;
7163
}
7264

src/Validator/ConfigValidator/ConfigValidator.php

+24-24
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,33 @@ class ConfigValidator implements ConfigValidatorInterface
2121

2222
protected $rules = [];
2323

24-
protected $contextObject;
25-
2624
protected $extraFieldsAllowed = false;
2725

2826
/** @var ValidationRuleInterface[] */
2927
protected $validationRules = [];
3028

31-
public function __construct($contextObject = null)
29+
/** @var ConfigValidator */
30+
protected static $instance;
31+
32+
private function __construct()
3233
{
33-
$this->contextObject = $contextObject;
3434
$this->initializeRules();
3535
}
3636

37+
/**
38+
* @return ConfigValidator
39+
*/
40+
public static function getInstance()
41+
{
42+
if (empty(self::$instance)) {
43+
self::$instance = new self();
44+
}
45+
46+
self::$instance->clearErrors();
47+
48+
return self::$instance;
49+
}
50+
3751
public function validate($data, $rules = [], $extraFieldsAllowed = null)
3852
{
3953
if ($extraFieldsAllowed !== null) $this->setExtraFieldsAllowed($extraFieldsAllowed);
@@ -47,7 +61,7 @@ public function validate($data, $rules = [], $extraFieldsAllowed = null)
4761
unset($fieldRules['required']);
4862

4963
if (!array_key_exists($fieldName, $data)) {
50-
$this->addError(new ValidationException('Field \'' . $fieldName . '\' of ' . $this->getContextName() . ' is required'));
64+
$this->addError(new ValidationException(sprintf('Field "%s" is required', $fieldName)));
5165

5266
continue;
5367
}
@@ -59,25 +73,21 @@ public function validate($data, $rules = [], $extraFieldsAllowed = null)
5973
/** Validation of all other rules*/
6074
foreach ($fieldRules as $ruleName => $ruleInfo) {
6175
if (!array_key_exists($ruleName, $this->validationRules)) {
62-
$this->addError(new ValidationException('Field \'' . $fieldName . '\' has invalid rule \'' . $ruleInfo . '\''));
76+
$this->addError(new ValidationException(sprintf('Field "%s" has invalid rule "%s"', $fieldName, $ruleInfo)));
6377

6478
continue;
6579
}
6680

6781
if (!$this->validationRules[$ruleName]->validate($data[$fieldName], $ruleInfo)) {
68-
$this->addError(
69-
new ValidationException('Field \'' . $fieldName . '\' of ' . $this->getContextName()
70-
. ' expected to be ' . $ruleName . ': \'' . (string)$ruleInfo . '\', but got: ' . gettype($data[$fieldName])));
82+
$this->addError(new ValidationException(sprintf('Field "%s" expected to be "%s" but got "%s"', $fieldName, $ruleName, gettype($data[$fieldName]))));
7183
}
7284
}
7385
}
7486

7587
if (!$this->isExtraFieldsAllowed()) {
7688
foreach (array_keys($data) as $fieldName) {
7789
if (!in_array($fieldName, $processedFields)) {
78-
$this->addError(
79-
new ValidationException('Field \'' . $fieldName . '\' is not expected in ' . $this->getContextName()));
80-
90+
$this->addError(new ValidationException(sprintf('Field "%s" is not expected', $fieldName)));
8191
}
8292
}
8393
}
@@ -90,19 +100,9 @@ protected function initializeRules()
90100
$this->validationRules['type'] = new TypeValidationRule($this);
91101
}
92102

93-
/**
94-
* @return string
95-
*/
96-
protected function getContextName()
103+
public function addRule($name, ValidationRuleInterface $rule)
97104
{
98-
if (is_object($this->contextObject)) {
99-
$class = get_class($this->contextObject);
100-
$class = substr($class, strrpos($class, '\\') + 1);
101-
102-
return $class;
103-
} else {
104-
return $this->contextObject ? $this->contextObject : '(context)';
105-
}
105+
$this->validationRules[$name] = $rule;
106106
}
107107

108108
public function isValid()

0 commit comments

Comments
 (0)