Skip to content

Commit

Permalink
direct passing of directive arguments & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peldax committed Sep 10, 2020
1 parent f3d889e commit 8aaabd5
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Directive/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function isRepeatable() : bool

public function resolve(\Graphpinator\Resolver\ArgumentValueSet $arguments) : string
{
$result = \call_user_func($this->resolveFn, $arguments);
$result = \call_user_func_array($this->resolveFn, $arguments->getRawArguments());

if (\is_string($result) && \array_key_exists($result, DirectiveResult::ENUM)) {
return $result;
Expand Down
4 changes: 2 additions & 2 deletions src/Directive/IncludeDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function __construct()
new \Graphpinator\Argument\ArgumentSet([
new \Graphpinator\Argument\Argument('if', \Graphpinator\Type\Container\Container::Boolean()->notNull()),
]),
static function (\Graphpinator\Resolver\ArgumentValueSet $arguments) : string {
return $arguments->offsetGet('if')->getRawValue()
static function (bool $if) : string {
return $if
? DirectiveResult::NONE
: DirectiveResult::SKIP;
},
Expand Down
4 changes: 2 additions & 2 deletions src/Directive/SkipDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function __construct()
new \Graphpinator\Argument\ArgumentSet([
new \Graphpinator\Argument\Argument('if', \Graphpinator\Type\Container\Container::Boolean()->notNull()),
]),
static function (\Graphpinator\Resolver\ArgumentValueSet $arguments) : string {
return $arguments->offsetGet('if')->getRawValue()
static function (bool $if) : string {
return $if
? DirectiveResult::SKIP
: DirectiveResult::NONE;
},
Expand Down
8 changes: 4 additions & 4 deletions src/Field/ResolvableField.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@

final class ResolvableField extends \Graphpinator\Field\Field
{
private \Closure $resolveFunction;
private \Closure $resolveFn;

public function __construct(
string $name,
\Graphpinator\Type\Contract\Outputable $type,
callable $resolveFunction,
callable $resolveFn,
?\Graphpinator\Argument\ArgumentSet $arguments = null
)
{
parent::__construct($name, $type, $arguments);
$this->resolveFunction = $resolveFunction;
$this->resolveFn = $resolveFn;
}

public function resolve(FieldResult $parentValue, \Graphpinator\Resolver\ArgumentValueSet $arguments) : FieldResult
{
$args = $arguments->getRawArguments();
\array_unshift($args, $parentValue->getResult()->getRawValue());

$result = \call_user_func_array($this->resolveFunction, $args);
$result = \call_user_func_array($this->resolveFn, $args);

if (!$result instanceof FieldResult) {
return FieldResult::fromRaw($this->type, $result);
Expand Down
12 changes: 6 additions & 6 deletions src/Type/Introspection/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ static function (Definition $definition) : ?string {
new \Graphpinator\Field\ResolvableField(
'fields',
$this->container->introspectionField()->notNull()->list(),
static function (Definition $definition, \Graphpinator\Resolver\ArgumentValueSet $arguments) : ?\Graphpinator\Field\FieldSet {
static function (Definition $definition, bool $includeDeprecated) : ?\Graphpinator\Field\FieldSet {
if (!$definition instanceof \Graphpinator\Type\Contract\InterfaceImplementor) {
return null;
}

if ($arguments['includeDeprecated']->getRawValue() === true) {
if ($includeDeprecated === true) {
return $definition->getFields();
}

Expand All @@ -78,7 +78,7 @@ static function (Definition $definition, \Graphpinator\Resolver\ArgumentValueSet
return new \Graphpinator\Field\FieldSet($filtered);
},
new \Graphpinator\Argument\ArgumentSet([
new \Graphpinator\Argument\Argument('includeDeprecated', \Graphpinator\Type\Container\Container::Boolean(), false),
new \Graphpinator\Argument\Argument('includeDeprecated', \Graphpinator\Type\Container\Container::Boolean()->notNull(), false),
]),
),
new \Graphpinator\Field\ResolvableField(
Expand Down Expand Up @@ -117,12 +117,12 @@ function (Definition $definition) : ?\Graphpinator\Utils\ConcreteSet {
new \Graphpinator\Field\ResolvableField(
'enumValues',
$this->container->introspectionEnumValue()->notNull()->list(),
static function (Definition $definition, \Graphpinator\Resolver\ArgumentValueSet $arguments) : ?\Graphpinator\Type\Enum\EnumItemSet {
static function (Definition $definition, bool $includeDeprecated) : ?\Graphpinator\Type\Enum\EnumItemSet {
if (!$definition instanceof \Graphpinator\Type\EnumType) {
return null;
}

if ($arguments['includeDeprecated']->getRawValue() === true) {
if ($includeDeprecated === true) {
return $definition->getItems();
}

Expand All @@ -139,7 +139,7 @@ static function (Definition $definition, \Graphpinator\Resolver\ArgumentValueSet
return new \Graphpinator\Type\Enum\EnumItemSet($filtered);
},
new \Graphpinator\Argument\ArgumentSet([
new \Graphpinator\Argument\Argument('includeDeprecated', \Graphpinator\Type\Container\Container::Boolean(), false),
new \Graphpinator\Argument\Argument('includeDeprecated', \Graphpinator\Type\Container\Container::Boolean()->notNull(), false),
]),
),
new \Graphpinator\Field\ResolvableField(
Expand Down
4 changes: 2 additions & 2 deletions src/Type/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function() : self {
$this->query->addMetaField(new \Graphpinator\Field\ResolvableField(
'__type',
$this->container->introspectionType(),
function($parent, \Graphpinator\Resolver\ArgumentValueSet $args) : \Graphpinator\Type\Contract\Definition {
return $this->container->getType($args['name']->getRawValue());
function($parent, string $name) : \Graphpinator\Type\Contract\Definition {
return $this->container->getType($name);
},
new \Graphpinator\Argument\ArgumentSet([
new \Graphpinator\Argument\Argument('name', \Graphpinator\Type\Container\Container::String()->notNull()),
Expand Down
2 changes: 1 addition & 1 deletion tests/Spec/PrintSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected function getFieldDefinition() : \Graphpinator\Field\ResolvableFieldSet
(new \Graphpinator\Field\ResolvableField(
'field1',
TestSchema::getInterface(),
static function (int $parent, \Graphpinator\Resolver\ArgumentValueSet $args) {
static function (int $parent, ?int $arg1, ?\stdClass $arg2) {
return 1;
},
new \Graphpinator\Argument\ArgumentSet([
Expand Down
4 changes: 2 additions & 2 deletions tests/Spec/PrintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ enum __TypeKind {
name: String
description: String
fields(
includeDeprecated: Boolean = false
includeDeprecated: Boolean! = false
): [__Field!]
interfaces: [__Type!]
possibleTypes: [__Type!]
enumValues(
includeDeprecated: Boolean = false
includeDeprecated: Boolean! = false
): [__EnumValue!]
inputFields: [__InputValue!]
ofType: __Type
Expand Down
12 changes: 6 additions & 6 deletions tests/Spec/TestSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ protected function getFieldDefinition() : \Graphpinator\Field\ResolvableFieldSet
new \Graphpinator\Field\ResolvableField(
'field1',
TestSchema::getInterface(),
static function (int $parent, \Graphpinator\Resolver\ArgumentValueSet $args) : \Graphpinator\Resolver\FieldResult {
static function (int $parent, ?int $arg1, ?\stdClass $arg2) : \Graphpinator\Resolver\FieldResult {
$object = new \stdClass();

if ($args['arg2']->getRawValue() === null) {
$object->name = 'Test ' . $args['arg1']->getRawValue();
if ($arg2 === null) {
$object->name = 'Test ' . $arg1;
} else {
$concat = static function (\stdClass $objectVal) use (&$concat) : string {
$str = '';
Expand All @@ -121,14 +121,14 @@ static function (int $parent, \Graphpinator\Resolver\ArgumentValueSet $args) : \
return $str;
};

$object->name = $concat($args['arg2']->getRawValue());
$object->name = $concat($arg2);
}

return \Graphpinator\Resolver\FieldResult::fromRaw(TestSchema::getTypeXyz(), $object);
},
new \Graphpinator\Argument\ArgumentSet([
new \Graphpinator\Argument\Argument('arg1', \Graphpinator\Type\Container\Container::Int(), 123),
new \Graphpinator\Argument\Argument('arg2', TestSchema::getInput()),
new \Graphpinator\Argument\Argument('arg1', \Graphpinator\Type\Container\Container::Int(), 123),
new \Graphpinator\Argument\Argument('arg2', TestSchema::getInput()),
]),
),
]);
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Type/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,29 @@ protected function getFieldDefinition() : \Graphpinator\Field\ResolvableFieldSet
new \Graphpinator\Field\ResolvableField(
'field1',
\Graphpinator\Type\Container\Container::String(),
static function ($parentValue, \Graphpinator\Resolver\ArgumentValueSet $arguments) {
static function ($parentValue) {
TypeTest::assertSame(TypeTest::PARENT_VAL, $parentValue);
TypeTest::assertCount(0, $arguments);
TypeTest::assertCount(1, \func_get_args());

return 'fieldValue';
},
),
new \Graphpinator\Field\ResolvableField(
'field2',
\Graphpinator\Type\Container\Container::Boolean(),
static function ($parentValue, \Graphpinator\Resolver\ArgumentValueSet $arguments) {
static function ($parentValue) {
TypeTest::assertSame(TypeTest::PARENT_VAL, $parentValue);
TypeTest::assertCount(0, $arguments);
TypeTest::assertCount(1, \func_get_args());

return false;
},
),
new \Graphpinator\Field\ResolvableField(
'field3',
\Graphpinator\Type\Container\Container::Int(),
static function ($parentValue, \Graphpinator\Resolver\ArgumentValueSet $arguments) {
static function ($parentValue) {
TypeTest::assertSame(TypeTest::PARENT_VAL, $parentValue);
TypeTest::assertCount(0, $arguments);
TypeTest::assertCount(1, \func_get_args());

return null;
},
Expand Down

0 comments on commit 8aaabd5

Please sign in to comment.