From 306879381860c89a5fcda8e7279a1436c54502f0 Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Sat, 12 Nov 2016 11:16:07 +0100 Subject: [PATCH 1/4] Add ObjectTypeConfig and ArgConfig Introduce optional ConfigBuilder (see #70) --- src/Type/Builder/ArgsConfig.php | 25 +++++++++ src/Type/Builder/Config.php | 43 +++++++++++++++ src/Type/Builder/ObjectTypeConfig.php | 60 +++++++++++++++++++++ tests/Type/Builder/ObjectTypeConfigTest.php | 55 +++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 src/Type/Builder/ArgsConfig.php create mode 100644 src/Type/Builder/Config.php create mode 100644 src/Type/Builder/ObjectTypeConfig.php create mode 100644 tests/Type/Builder/ObjectTypeConfigTest.php diff --git a/src/Type/Builder/ArgsConfig.php b/src/Type/Builder/ArgsConfig.php new file mode 100644 index 000000000..b483d017b --- /dev/null +++ b/src/Type/Builder/ArgsConfig.php @@ -0,0 +1,25 @@ +addConfig('args', [ + 'name' => $name, + 'type' => $type, + 'defaultValue' => $defaultValue, + 'description' => $description, + ]); + + return $this; + } + + public function build() + { + $args = parent::build(); + + return isset($args['args']) ? $args['args'] : []; + } +} diff --git a/src/Type/Builder/Config.php b/src/Type/Builder/Config.php new file mode 100644 index 000000000..ee163b2b4 --- /dev/null +++ b/src/Type/Builder/Config.php @@ -0,0 +1,43 @@ +config = []; + } + + /** + * @return self + */ + public static function create() + { + return new static(); + } + + protected function addConfig($name, $value, $append = true) + { + if (!$append) { + $this->config[$name] = $value; + } else { + if (!isset($this->config[$name])) { + $this->config[$name] = []; + } + $this->config[$name][] = $value; + } + + return $this; + } + + public function build() + { + return $this->config; + } +} diff --git a/src/Type/Builder/ObjectTypeConfig.php b/src/Type/Builder/ObjectTypeConfig.php new file mode 100644 index 000000000..ebcf0a97f --- /dev/null +++ b/src/Type/Builder/ObjectTypeConfig.php @@ -0,0 +1,60 @@ +addConfig('name', $name, false); + return $this; + } + + public function description($description) + { + $this->addConfig('description', $description, false); + return $this; + } + + public function addField($name, $type, callable $resolve = null, $description = null, ArgsConfig $args = null) + { + $field = [ + 'name' => $name, + 'type' => $type, + ]; + + if (null !== $description) { + $field['description'] = $description; + } + + if (null !== $resolve) { + $field['resolve'] = $resolve; + } + + if (null !== $args) { + $field['args'] = $args->build(); + } + + $this->addConfig('fields', $field); + + return $this; + } + + public function addInterface($interface) + { + $this->addConfig('interfaces', $interface); + return $this; + } + + public function isTypeOf(callable $isTypeOf = null) + { + $this->addConfig('isTypeOf', $isTypeOf, false); + return $this; + } + + public function resolveField(callable $resolveField = null) + { + $this->addConfig('resolveField', $resolveField, false); + return $this; + } +} diff --git a/tests/Type/Builder/ObjectTypeConfigTest.php b/tests/Type/Builder/ObjectTypeConfigTest.php new file mode 100644 index 000000000..867a240aa --- /dev/null +++ b/tests/Type/Builder/ObjectTypeConfigTest.php @@ -0,0 +1,55 @@ +addArg('arg1', Type::boolean(), true, 'description arg1') + ->addArg('arg2', Type::string(), 'defaultVal', 'description arg2'); + + $config = ObjectTypeConfig::create() + ->name('TypeName') + ->addField('field1', Type::string(), null, 'description field1', $args) + ->addField('field2', Type::nonNull(Type::string())) + ; + + $this->assertEquals( + [ + 'name' => 'TypeName', + 'fields' => [ + [ + 'name' => 'field1', + 'type' => Type::string(), + 'description' => 'description field1', + 'args' => [ + [ + 'name' => 'arg1', + 'type' => Type::boolean(), + 'description' => 'description arg1', + 'defaultValue' => true, + ], + [ + 'name' => 'arg2', + 'type' => Type::string(), + 'description' => 'description arg2', + 'defaultValue' => 'defaultVal', + ], + ], + ], + [ + 'name' => 'field2', + 'type' => Type::nonNull(Type::string()), + ], + ], + ], + $config->build() + ); + } +} From 4362b8b25694ed346f9f512b0eb3b2ac58bab879 Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Sat, 12 Nov 2016 12:23:33 +0100 Subject: [PATCH 2/4] Add missing AddField arguments --- src/Type/Builder/Config.php | 3 ++- src/Type/Builder/ObjectTypeConfig.php | 19 +++++--------- tests/Type/Builder/ObjectTypeConfigTest.php | 29 ++++++++++++++++++--- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/Type/Builder/Config.php b/src/Type/Builder/Config.php index ee163b2b4..39ece2115 100644 --- a/src/Type/Builder/Config.php +++ b/src/Type/Builder/Config.php @@ -4,8 +4,9 @@ abstract class Config { + /** - * @var \ArrayObject + * @var array */ private $config; diff --git a/src/Type/Builder/ObjectTypeConfig.php b/src/Type/Builder/ObjectTypeConfig.php index ebcf0a97f..f936bfbec 100644 --- a/src/Type/Builder/ObjectTypeConfig.php +++ b/src/Type/Builder/ObjectTypeConfig.php @@ -16,25 +16,18 @@ public function description($description) return $this; } - public function addField($name, $type, callable $resolve = null, $description = null, ArgsConfig $args = null) + public function addField($name, $type, callable $resolve = null, $description = null, ArgsConfig $args = null, callable $complexity = null, $deprecationReason = null) { $field = [ 'name' => $name, 'type' => $type, + 'description' => $description, + 'resolve' => $resolve, + 'complexity' => $complexity, + 'deprecationReason' => $deprecationReason, + 'args' => null === $args ? [] : $args->build(), ]; - if (null !== $description) { - $field['description'] = $description; - } - - if (null !== $resolve) { - $field['resolve'] = $resolve; - } - - if (null !== $args) { - $field['args'] = $args->build(); - } - $this->addConfig('fields', $field); return $this; diff --git a/tests/Type/Builder/ObjectTypeConfigTest.php b/tests/Type/Builder/ObjectTypeConfigTest.php index 867a240aa..a3b877a99 100644 --- a/tests/Type/Builder/ObjectTypeConfigTest.php +++ b/tests/Type/Builder/ObjectTypeConfigTest.php @@ -14,20 +14,34 @@ public function testBuild() ->addArg('arg1', Type::boolean(), true, 'description arg1') ->addArg('arg2', Type::string(), 'defaultVal', 'description arg2'); + $field1Resolver = function () { + return 'resolve it!'; + }; + $isTypeOf = function () { + return true; + }; + $isTypeOf = function () { + return []; + }; + $config = ObjectTypeConfig::create() ->name('TypeName') - ->addField('field1', Type::string(), null, 'description field1', $args) + ->addField('field1', Type::string(), $field1Resolver, 'description field1', $args) ->addField('field2', Type::nonNull(Type::string())) - ; + ->description('My new Object') + ->isTypeOf($isTypeOf) + ->resolveField(); $this->assertEquals( [ 'name' => 'TypeName', - 'fields' => [ + 'description' => 'My new Object', + 'fields' => [ [ 'name' => 'field1', 'type' => Type::string(), 'description' => 'description field1', + 'resolve' => $field1Resolver, 'args' => [ [ 'name' => 'arg1', @@ -42,12 +56,21 @@ public function testBuild() 'defaultValue' => 'defaultVal', ], ], + 'complexity' => null, + 'deprecationReason' => null, ], [ 'name' => 'field2', 'type' => Type::nonNull(Type::string()), + 'description' => null, + 'resolve' => null, + 'complexity' => null, + 'deprecationReason' => null, + 'args' => [], ], ], + 'isTypeOf' => $isTypeOf, + 'resolveField' => null, ], $config->build() ); From 758294abe50233b4385b814cecc3bece0f974c10 Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Sun, 13 Nov 2016 14:07:14 +0100 Subject: [PATCH 3/4] Use Traits to add flexibility --- src/Type/Builder/ArgConfig.php | 21 +++++++ src/Type/Builder/ArgsConfig.php | 18 +++--- src/Type/Builder/FieldConfig.php | 61 +++++++++++++++++++ src/Type/Builder/FieldsConfig.php | 15 +++++ src/Type/Builder/FieldsConfigTrait.php | 45 ++++++++++++++ .../Builder/NameDescriptionConfigTrait.php | 35 +++++++++++ src/Type/Builder/ObjectTypeConfig.php | 61 +++++++++++++------ src/Type/Builder/TypeConfigTrait.php | 25 ++++++++ tests/Type/Builder/ObjectTypeConfigTest.php | 17 +++--- 9 files changed, 264 insertions(+), 34 deletions(-) create mode 100644 src/Type/Builder/ArgConfig.php create mode 100644 src/Type/Builder/FieldConfig.php create mode 100644 src/Type/Builder/FieldsConfig.php create mode 100644 src/Type/Builder/FieldsConfigTrait.php create mode 100644 src/Type/Builder/NameDescriptionConfigTrait.php create mode 100644 src/Type/Builder/TypeConfigTrait.php diff --git a/src/Type/Builder/ArgConfig.php b/src/Type/Builder/ArgConfig.php new file mode 100644 index 000000000..fe14fbbfb --- /dev/null +++ b/src/Type/Builder/ArgConfig.php @@ -0,0 +1,21 @@ +addConfig('defaultValue', $defaultValue, false); + + return $this; + } +} diff --git a/src/Type/Builder/ArgsConfig.php b/src/Type/Builder/ArgsConfig.php index b483d017b..7f2623bf9 100644 --- a/src/Type/Builder/ArgsConfig.php +++ b/src/Type/Builder/ArgsConfig.php @@ -4,16 +4,20 @@ class ArgsConfig extends Config { + public function addArgConfig(ArgConfig $argConfig) + { + return $this->addConfig('args', $argConfig->build()); + } + public function addArg($name, $type, $defaultValue = null, $description = null) { - $this->addConfig('args', [ - 'name' => $name, - 'type' => $type, - 'defaultValue' => $defaultValue, - 'description' => $description, - ]); + $argConfig = ArgConfig::create() + ->name($name) + ->type($type) + ->defaultValue($defaultValue) + ->description($description); - return $this; + return $this->addArgConfig($argConfig); } public function build() diff --git a/src/Type/Builder/FieldConfig.php b/src/Type/Builder/FieldConfig.php new file mode 100644 index 000000000..ccda138d0 --- /dev/null +++ b/src/Type/Builder/FieldConfig.php @@ -0,0 +1,61 @@ +addConfig('resolve', $resolve, false); + } + + /** + * @param callable|null $complexity + * + * @return $this + */ + public function complexity(callable $complexity = null) + { + return $this->addConfig('complexity', $complexity, false); + } + + /** + * @param string|null $deprecationReason + * @return $this + */ + public function deprecationReason($deprecationReason = null) + { + return $this->addConfig('deprecationReason', $deprecationReason, false); + } + + /** + * @param ArgConfig $arg + * + * @return $this + */ + public function addArg(ArgConfig $arg) + { + return $this->addConfig('args', $arg->build()); + } + + /** + * @param ArgsConfig $args + * + * @return $this + */ + public function addArgs(ArgsConfig $args) + { + foreach ($args->build() as $arg) { + $this->addConfig('args', $arg); + } + return $this; + } +} diff --git a/src/Type/Builder/FieldsConfig.php b/src/Type/Builder/FieldsConfig.php new file mode 100644 index 000000000..516ed7342 --- /dev/null +++ b/src/Type/Builder/FieldsConfig.php @@ -0,0 +1,15 @@ +addConfig('fields', $fieldConfig->build()); + } + + public function addField($name, $type, $description = null, ArgsConfig $args = null, callable $resolve = null, callable $complexity = null) + { + return $this->pushField($name, $type, $description, $args, $resolve, $complexity); + } + + public function addDeprecatedField($name, $type, $deprecationReason, $description = null, ArgsConfig $args = null, callable $resolve = null, callable $complexity = null) + { + return $this->pushField($name, $type, $description, $args, $resolve, $complexity, $deprecationReason); + } + + protected function pushField($name, $type, $description = null, ArgsConfig $args = null, callable $resolve = null, callable $complexity = null, $deprecationReason = null) + { + $fieldConfig = FieldConfig::create() + ->name($name) + ->type($type) + ->resolve($resolve) + ->description($description); + + if (null !== $args) { + $fieldConfig->addArgs($args); + } + $fieldConfig->complexity($complexity); + if (null !== $deprecationReason) { + $fieldConfig->deprecationReason($deprecationReason); + } + + return $this->addFieldConfig($fieldConfig); + } +} diff --git a/src/Type/Builder/NameDescriptionConfigTrait.php b/src/Type/Builder/NameDescriptionConfigTrait.php new file mode 100644 index 000000000..134b6619d --- /dev/null +++ b/src/Type/Builder/NameDescriptionConfigTrait.php @@ -0,0 +1,35 @@ +addConfig('name', $name, false); + + return $this; + } + + /** + * @param string|null $description + * + * @return $this + */ + public function description($description) + { + $this->addConfig('description', $description, false); + + return $this; + } +} diff --git a/src/Type/Builder/ObjectTypeConfig.php b/src/Type/Builder/ObjectTypeConfig.php index f936bfbec..5efa65468 100644 --- a/src/Type/Builder/ObjectTypeConfig.php +++ b/src/Type/Builder/ObjectTypeConfig.php @@ -2,52 +2,73 @@ namespace GraphQL\Type\Builder; +use GraphQL\Type\Definition\InterfaceType; + class ObjectTypeConfig extends Config { - public function name($name) - { - $this->addConfig('name', $name, false); - return $this; - } + use NameDescriptionConfigTrait; + use FieldsConfigTrait; - public function description($description) + /** + * @param callable $interfaces + * + * @return $this + */ + public function interfaces(callable $interfaces) { - $this->addConfig('description', $description, false); + $this->addConfig('interfaces', $interfaces, false); + return $this; } - public function addField($name, $type, callable $resolve = null, $description = null, ArgsConfig $args = null, callable $complexity = null, $deprecationReason = null) + /** + * @param callable|FieldsConfig $fields + * + * @return $this + */ + public function fields($fields) { - $field = [ - 'name' => $name, - 'type' => $type, - 'description' => $description, - 'resolve' => $resolve, - 'complexity' => $complexity, - 'deprecationReason' => $deprecationReason, - 'args' => null === $args ? [] : $args->build(), - ]; - - $this->addConfig('fields', $field); - + if ($fields instanceof FieldsConfig) { + $this->addConfig('fields', $fields->build(), false); + } else { + $this->addConfig('fields', $fields, false); + } return $this; } + /** + * @param InterfaceType|callable $interface + * + * @return $this + */ public function addInterface($interface) { $this->addConfig('interfaces', $interface); + return $this; } + /** + * @param callable|null $isTypeOf + * + * @return $this + */ public function isTypeOf(callable $isTypeOf = null) { $this->addConfig('isTypeOf', $isTypeOf, false); + return $this; } + /** + * @param callable|null $resolveField + * + * @return $this + */ public function resolveField(callable $resolveField = null) { $this->addConfig('resolveField', $resolveField, false); + return $this; } } diff --git a/src/Type/Builder/TypeConfigTrait.php b/src/Type/Builder/TypeConfigTrait.php new file mode 100644 index 000000000..79e55042f --- /dev/null +++ b/src/Type/Builder/TypeConfigTrait.php @@ -0,0 +1,25 @@ +addConfig('type', $type, false); + + return $this; + } +} diff --git a/tests/Type/Builder/ObjectTypeConfigTest.php b/tests/Type/Builder/ObjectTypeConfigTest.php index a3b877a99..2d2f2625e 100644 --- a/tests/Type/Builder/ObjectTypeConfigTest.php +++ b/tests/Type/Builder/ObjectTypeConfigTest.php @@ -20,14 +20,12 @@ public function testBuild() $isTypeOf = function () { return true; }; - $isTypeOf = function () { - return []; - }; $config = ObjectTypeConfig::create() ->name('TypeName') - ->addField('field1', Type::string(), $field1Resolver, 'description field1', $args) + ->addField('field1', Type::string(), 'description field1', $args, $field1Resolver) ->addField('field2', Type::nonNull(Type::string())) + ->addDeprecatedField('deprecatedField', Type::string(), 'This field is deprecated.') ->description('My new Object') ->isTypeOf($isTypeOf) ->resolveField(); @@ -57,7 +55,6 @@ public function testBuild() ], ], 'complexity' => null, - 'deprecationReason' => null, ], [ 'name' => 'field2', @@ -65,8 +62,14 @@ public function testBuild() 'description' => null, 'resolve' => null, 'complexity' => null, - 'deprecationReason' => null, - 'args' => [], + ], + [ + 'name' => 'deprecatedField', + 'type' => Type::string(), + 'description' => null, + 'resolve' => null, + 'complexity' => null, + 'deprecationReason' => 'This field is deprecated.', ], ], 'isTypeOf' => $isTypeOf, From 691f558101731ff7aaf39f8326c300070ebce70a Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Mon, 18 Dec 2017 10:07:44 +0100 Subject: [PATCH 4/4] Improve tests coverage --- src/Type/Builder/Config.php | 3 +- src/Type/Builder/FieldsConfigTrait.php | 18 +- src/Type/Builder/ObjectTypeConfig.php | 16 +- tests/Type/Builder/ObjectTypeConfigTest.php | 179 ++++++++++++++++++-- 4 files changed, 186 insertions(+), 30 deletions(-) diff --git a/src/Type/Builder/Config.php b/src/Type/Builder/Config.php index 39ece2115..051329cf9 100644 --- a/src/Type/Builder/Config.php +++ b/src/Type/Builder/Config.php @@ -4,7 +4,6 @@ abstract class Config { - /** * @var array */ @@ -16,7 +15,7 @@ protected function __construct() } /** - * @return self + * @return static */ public static function create() { diff --git a/src/Type/Builder/FieldsConfigTrait.php b/src/Type/Builder/FieldsConfigTrait.php index b44975a1c..4c547f37c 100644 --- a/src/Type/Builder/FieldsConfigTrait.php +++ b/src/Type/Builder/FieldsConfigTrait.php @@ -28,14 +28,24 @@ protected function pushField($name, $type, $description = null, ArgsConfig $args { $fieldConfig = FieldConfig::create() ->name($name) - ->type($type) - ->resolve($resolve) - ->description($description); + ->type($type); + + if (null !== $resolve) { + $fieldConfig->resolve($resolve); + } + + if (null !== $description) { + $fieldConfig->description($description); + } if (null !== $args) { $fieldConfig->addArgs($args); } - $fieldConfig->complexity($complexity); + + if (null !== $complexity) { + $fieldConfig->complexity($complexity); + } + if (null !== $deprecationReason) { $fieldConfig->deprecationReason($deprecationReason); } diff --git a/src/Type/Builder/ObjectTypeConfig.php b/src/Type/Builder/ObjectTypeConfig.php index 5efa65468..fb2ac4496 100644 --- a/src/Type/Builder/ObjectTypeConfig.php +++ b/src/Type/Builder/ObjectTypeConfig.php @@ -10,13 +10,16 @@ class ObjectTypeConfig extends Config use FieldsConfigTrait; /** - * @param callable $interfaces + * @param InterfaceType[] $interfaces * * @return $this */ - public function interfaces(callable $interfaces) + public function interfaces(array $interfaces) { - $this->addConfig('interfaces', $interfaces, false); + $this->addConfig('interfaces', [], false); + foreach ($interfaces as $interface) { + $this->addInterface($interface); + } return $this; } @@ -30,18 +33,19 @@ public function fields($fields) { if ($fields instanceof FieldsConfig) { $this->addConfig('fields', $fields->build(), false); - } else { + } elseif ($fields) { $this->addConfig('fields', $fields, false); } + return $this; } /** - * @param InterfaceType|callable $interface + * @param InterfaceType $interface * * @return $this */ - public function addInterface($interface) + public function addInterface(InterfaceType $interface) { $this->addConfig('interfaces', $interface); diff --git a/tests/Type/Builder/ObjectTypeConfigTest.php b/tests/Type/Builder/ObjectTypeConfigTest.php index 2d2f2625e..f7c8be8b2 100644 --- a/tests/Type/Builder/ObjectTypeConfigTest.php +++ b/tests/Type/Builder/ObjectTypeConfigTest.php @@ -2,44 +2,194 @@ namespace GraphQL\Tests\Type\Builder; +use GraphQL\Type\Builder\ArgConfig; use GraphQL\Type\Builder\ArgsConfig; +use GraphQL\Type\Builder\Config; +use GraphQL\Type\Builder\FieldConfig; +use GraphQL\Type\Builder\FieldsConfig; use GraphQL\Type\Builder\ObjectTypeConfig; +use GraphQL\Type\Definition\InterfaceType; use GraphQL\Type\Definition\Type; class ObjectTypeConfigTest extends \PHPUnit_Framework_TestCase { - public function testBuild() - { - $args = ArgsConfig::create() - ->addArg('arg1', Type::boolean(), true, 'description arg1') - ->addArg('arg2', Type::string(), 'defaultVal', 'description arg2'); + private $field1Resolver; + + private $isTypeOf; + + private $interface; - $field1Resolver = function () { + public function setUp() + { + $this->field1Resolver = function () { return 'resolve it!'; }; - $isTypeOf = function () { + $this->isTypeOf = function () { return true; }; + $this->interface = new InterfaceType(['name' => 'Foo', 'fields' => ['field2' => ['type' => Type::nonNull(Type::string())]]]); + } + + public function testBuildUsingAddField() + { + $args = ArgsConfig::create() + ->addArg('arg1', Type::boolean(), true, 'description arg1') + ->addArg('arg2', Type::string(), 'defaultVal', 'description arg2'); $config = ObjectTypeConfig::create() ->name('TypeName') - ->addField('field1', Type::string(), 'description field1', $args, $field1Resolver) + ->description('My new Object') + ->interfaces([$this->interface]) + ->addField('field1', Type::string(), 'description field1', $args, $this->field1Resolver) ->addField('field2', Type::nonNull(Type::string())) ->addDeprecatedField('deprecatedField', Type::string(), 'This field is deprecated.') + ->isTypeOf($this->isTypeOf) + ->resolveField(); + + $this->assertConfig($config); + } + + public function testBuildUsingFields() + { + $args = ArgsConfig::create() + ->addArg('arg1', Type::boolean(), true, 'description arg1') + ->addArg('arg2', Type::string(), 'defaultVal', 'description arg2'); + + $fields = FieldsConfig::create() + ->addField('field1', Type::string(), 'description field1', $args, $this->field1Resolver) + ->addField('field2', Type::nonNull(Type::string())) + ; + $config = ObjectTypeConfig::create() + ->name('TypeName') ->description('My new Object') - ->isTypeOf($isTypeOf) + ->interfaces([$this->interface]) + ->fields($fields) + ->addDeprecatedField('deprecatedField', Type::string(), 'This field is deprecated.') + ->isTypeOf($this->isTypeOf) ->resolveField(); + $this->assertConfig($config); + } + + public function testBuildUsingFieldConfig() + { + $args = ArgsConfig::create() + ->addArg('arg1', Type::boolean(), true, 'description arg1') + ->addArg('arg2', Type::string(), 'defaultVal', 'description arg2'); + + $fields = FieldsConfig::create(); + + $fields->addFieldConfig( + FieldConfig::create() + ->name('field1')->type(Type::string())->description('description field1') + ->addArgs($args)->resolve($this->field1Resolver) + ) + ->addFieldConfig(FieldConfig::create()->name('field2')->type(Type::nonNull(Type::string()))); + ; + $config = ObjectTypeConfig::create() + ->name('TypeName') + ->description('My new Object') + ->interfaces([$this->interface]) + ->fields($fields) + ->addDeprecatedField('deprecatedField', Type::string(), 'This field is deprecated.') + ->isTypeOf($this->isTypeOf) + ->resolveField(); + + $this->assertConfig($config); + } + + public function testFieldsAsCallable() + { + $fields = function () { + return []; + }; + + $config = ObjectTypeConfig::create() + ->name('TypeName') + ->fields($fields); + + $this->assertEquals( + [ + 'name' => 'TypeName', + 'fields' => $fields, + ], + $config->build() + ); + } + + public function testFieldWithComplexity() + { + $complexity = function () { + return 2000; + }; + + $config = ObjectTypeConfig::create() + ->name('TypeName') + ->addFieldConfig( + FieldConfig::create() + ->name('field1') + ->addArg(ArgConfig::create()->name('arg1')->type(Type::string())) + ->type(Type::string()) + ->complexity($complexity) + ) + ; + + $this->assertEquals( + [ + 'name' => 'TypeName', + 'fields' => [ + [ + 'name' => 'field1', + 'type' => Type::string(), + 'args' => [ + [ + 'name' => 'arg1', + 'type' => Type::string(), + ], + ], + 'complexity' => $complexity + ] + ], + ], + $config->build() + ); + } + + public function testFieldsWithComplexity() + { + $complexity = function () { + return 2000; + }; + + $fields = FieldsConfig::create() + ->addField('field1', Type::string(), null, null, null, $complexity) + ; + + $this->assertEquals( + [ + [ + 'name' => 'field1', + 'type' => Type::string(), + 'complexity' => $complexity + ] + ], + $fields->build() + ); + } + + private function assertConfig(Config $config) + { $this->assertEquals( [ 'name' => 'TypeName', 'description' => 'My new Object', + 'interfaces' => [$this->interface], 'fields' => [ [ 'name' => 'field1', 'type' => Type::string(), 'description' => 'description field1', - 'resolve' => $field1Resolver, + 'resolve' => $this->field1Resolver, 'args' => [ [ 'name' => 'arg1', @@ -54,25 +204,18 @@ public function testBuild() 'defaultValue' => 'defaultVal', ], ], - 'complexity' => null, ], [ 'name' => 'field2', 'type' => Type::nonNull(Type::string()), - 'description' => null, - 'resolve' => null, - 'complexity' => null, ], [ 'name' => 'deprecatedField', 'type' => Type::string(), - 'description' => null, - 'resolve' => null, - 'complexity' => null, 'deprecationReason' => 'This field is deprecated.', ], ], - 'isTypeOf' => $isTypeOf, + 'isTypeOf' => $this->isTypeOf, 'resolveField' => null, ], $config->build()