Skip to content

Commit 0fc1759

Browse files
committed
Add ObjectTypeConfig and ArgConfig
Introduce optional ConfigBuilder (see #70)
1 parent 986eff9 commit 0fc1759

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

src/Type/Builder/ArgsConfig.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace GraphQL\Type\Builder;
4+
5+
class ArgsConfig extends Config
6+
{
7+
public function addArg($name, $type, $defaultValue = null, $description = null)
8+
{
9+
$this->addConfig('args', [
10+
'name' => $name,
11+
'type' => $type,
12+
'defaultValue' => $defaultValue,
13+
'description' => $description,
14+
]);
15+
16+
return $this;
17+
}
18+
19+
public function build()
20+
{
21+
$args = parent::build();
22+
23+
return isset($args['args']) ? $args['args'] : [];
24+
}
25+
}

src/Type/Builder/Config.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace GraphQL\Type\Builder;
4+
5+
abstract class Config
6+
{
7+
/**
8+
* @var \ArrayObject
9+
*/
10+
private $config;
11+
12+
protected function __construct()
13+
{
14+
$this->config = [];
15+
}
16+
17+
/**
18+
* @return self
19+
*/
20+
public static function create()
21+
{
22+
return new static();
23+
}
24+
25+
protected function addConfig($name, $value, $append = true)
26+
{
27+
if (!$append) {
28+
$this->config[$name] = $value;
29+
} else {
30+
if (!isset($this->config[$name])) {
31+
$this->config[$name] = [];
32+
}
33+
$this->config[$name][] = $value;
34+
}
35+
36+
return $this;
37+
}
38+
39+
public function build()
40+
{
41+
return $this->config;
42+
}
43+
}

src/Type/Builder/ObjectTypeConfig.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace GraphQL\Type\Builder;
4+
5+
class ObjectTypeConfig extends Config
6+
{
7+
public function name($name)
8+
{
9+
$this->addConfig('name', $name, false);
10+
return $this;
11+
}
12+
13+
public function description($description)
14+
{
15+
$this->addConfig('description', $description, false);
16+
return $this;
17+
}
18+
19+
public function addField($name, $type, callable $resolve = null, $description = null, ArgsConfig $args = null)
20+
{
21+
$field = [
22+
'name' => $name,
23+
'type' => $type,
24+
];
25+
26+
if (null !== $description) {
27+
$field['description'] = $description;
28+
}
29+
30+
if (null !== $resolve) {
31+
$field['resolve'] = $resolve;
32+
}
33+
34+
if (null !== $args) {
35+
$field['args'] = $args->build();
36+
}
37+
38+
$this->addConfig('fields', $field);
39+
40+
return $this;
41+
}
42+
43+
public function addInterface($interface)
44+
{
45+
$this->addConfig('interfaces', $interface);
46+
return $this;
47+
}
48+
49+
public function isTypeOf(callable $isTypeOf = null)
50+
{
51+
$this->addConfig('isTypeOf', $isTypeOf, false);
52+
return $this;
53+
}
54+
55+
public function resolveField(callable $resolveField = null)
56+
{
57+
$this->addConfig('resolveField', $resolveField, false);
58+
return $this;
59+
}
60+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace GraphQL\Tests\Type\Builder;
4+
5+
use GraphQL\Type\Builder\ArgsConfig;
6+
use GraphQL\Type\Builder\ObjectTypeConfig;
7+
use GraphQL\Type\Definition\Type;
8+
9+
class ObjectTypeConfigTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testBuild()
12+
{
13+
$args = ArgsConfig::create()
14+
->addArg('arg1', Type::boolean(), true, 'description arg1')
15+
->addArg('arg2', Type::string(), 'defaultVal', 'description arg2');
16+
17+
$config = ObjectTypeConfig::create()
18+
->name('TypeName')
19+
->addField('field1', Type::string(), null, 'description field1', $args)
20+
->addField('field2', Type::nonNull(Type::string()))
21+
;
22+
23+
$this->assertEquals(
24+
[
25+
'name' => 'TypeName',
26+
'fields' => [
27+
[
28+
'name' => 'field1',
29+
'type' => Type::string(),
30+
'description' => 'description field1',
31+
'args' => [
32+
[
33+
'name' => 'arg1',
34+
'type' => Type::boolean(),
35+
'description' => 'description arg1',
36+
'defaultValue' => true,
37+
],
38+
[
39+
'name' => 'arg2',
40+
'type' => Type::string(),
41+
'description' => 'description arg2',
42+
'defaultValue' => 'defaultVal',
43+
],
44+
],
45+
],
46+
[
47+
'name' => 'field2',
48+
'type' => Type::nonNull(Type::string()),
49+
],
50+
],
51+
],
52+
$config->build()
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)