Skip to content

Commit 31cdc0b

Browse files
committed
Use Traits to add flexibility
1 parent 95b46b7 commit 31cdc0b

9 files changed

+264
-34
lines changed

src/Type/Builder/ArgConfig.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace GraphQL\Type\Builder;
4+
5+
class ArgConfig extends Config
6+
{
7+
use NameDescriptionConfigTrait;
8+
use TypeConfigTrait;
9+
10+
/**
11+
* @param mixed $defaultValue
12+
*
13+
* @return $this
14+
*/
15+
public function defaultValue($defaultValue)
16+
{
17+
$this->addConfig('defaultValue', $defaultValue, false);
18+
19+
return $this;
20+
}
21+
}

src/Type/Builder/ArgsConfig.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44

55
class ArgsConfig extends Config
66
{
7+
public function addArgConfig(ArgConfig $argConfig)
8+
{
9+
return $this->addConfig('args', $argConfig->build());
10+
}
11+
712
public function addArg($name, $type, $defaultValue = null, $description = null)
813
{
9-
$this->addConfig('args', [
10-
'name' => $name,
11-
'type' => $type,
12-
'defaultValue' => $defaultValue,
13-
'description' => $description,
14-
]);
14+
$argConfig = ArgConfig::create()
15+
->name($name)
16+
->type($type)
17+
->defaultValue($defaultValue)
18+
->description($description);
1519

16-
return $this;
20+
return $this->addArgConfig($argConfig);
1721
}
1822

1923
public function build()

src/Type/Builder/FieldConfig.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace GraphQL\Type\Builder;
4+
5+
class FieldConfig extends Config
6+
{
7+
use NameDescriptionConfigTrait;
8+
use TypeConfigTrait;
9+
10+
/**
11+
* @param callable|null $resolve
12+
*
13+
* @return $this
14+
*/
15+
public function resolve(callable $resolve = null)
16+
{
17+
return $this->addConfig('resolve', $resolve, false);
18+
}
19+
20+
/**
21+
* @param callable|null $complexity
22+
*
23+
* @return $this
24+
*/
25+
public function complexity(callable $complexity = null)
26+
{
27+
return $this->addConfig('complexity', $complexity, false);
28+
}
29+
30+
/**
31+
* @param string|null $deprecationReason
32+
* @return $this
33+
*/
34+
public function deprecationReason($deprecationReason = null)
35+
{
36+
return $this->addConfig('deprecationReason', $deprecationReason, false);
37+
}
38+
39+
/**
40+
* @param ArgConfig $arg
41+
*
42+
* @return $this
43+
*/
44+
public function addArg(ArgConfig $arg)
45+
{
46+
return $this->addConfig('args', $arg->build());
47+
}
48+
49+
/**
50+
* @param ArgsConfig $args
51+
*
52+
* @return $this
53+
*/
54+
public function addArgs(ArgsConfig $args)
55+
{
56+
foreach ($args->build() as $arg) {
57+
$this->addConfig('args', $arg);
58+
}
59+
return $this;
60+
}
61+
}

src/Type/Builder/FieldsConfig.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace GraphQL\Type\Builder;
4+
5+
class FieldsConfig extends Config
6+
{
7+
use FieldsConfigTrait;
8+
9+
public function build()
10+
{
11+
$fields = parent::build();
12+
13+
return isset($fields['fields']) ? $fields['fields'] : [];
14+
}
15+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace GraphQL\Type\Builder;
4+
5+
/**
6+
* Class FieldsConfigTrait
7+
*
8+
* @method $this addConfig(string $name, mixed $value, boolean $append = true)
9+
*/
10+
trait FieldsConfigTrait
11+
{
12+
public function addFieldConfig(FieldConfig $fieldConfig)
13+
{
14+
return $this->addConfig('fields', $fieldConfig->build());
15+
}
16+
17+
public function addField($name, $type, $description = null, ArgsConfig $args = null, callable $resolve = null, callable $complexity = null)
18+
{
19+
return $this->pushField($name, $type, $description, $args, $resolve, $complexity);
20+
}
21+
22+
public function addDeprecatedField($name, $type, $deprecationReason, $description = null, ArgsConfig $args = null, callable $resolve = null, callable $complexity = null)
23+
{
24+
return $this->pushField($name, $type, $description, $args, $resolve, $complexity, $deprecationReason);
25+
}
26+
27+
protected function pushField($name, $type, $description = null, ArgsConfig $args = null, callable $resolve = null, callable $complexity = null, $deprecationReason = null)
28+
{
29+
$fieldConfig = FieldConfig::create()
30+
->name($name)
31+
->type($type)
32+
->resolve($resolve)
33+
->description($description);
34+
35+
if (null !== $args) {
36+
$fieldConfig->addArgs($args);
37+
}
38+
$fieldConfig->complexity($complexity);
39+
if (null !== $deprecationReason) {
40+
$fieldConfig->deprecationReason($deprecationReason);
41+
}
42+
43+
return $this->addFieldConfig($fieldConfig);
44+
}
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace GraphQL\Type\Builder;
4+
5+
/**
6+
* Class NameDescriptionConfigTrait
7+
*
8+
* @method $this addConfig(string $name, mixed $value, boolean $append = true)
9+
*/
10+
trait NameDescriptionConfigTrait
11+
{
12+
/**
13+
* @param string $name
14+
*
15+
* @return $this
16+
*/
17+
public function name($name)
18+
{
19+
$this->addConfig('name', $name, false);
20+
21+
return $this;
22+
}
23+
24+
/**
25+
* @param string|null $description
26+
*
27+
* @return $this
28+
*/
29+
public function description($description)
30+
{
31+
$this->addConfig('description', $description, false);
32+
33+
return $this;
34+
}
35+
}

src/Type/Builder/ObjectTypeConfig.php

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,73 @@
22

33
namespace GraphQL\Type\Builder;
44

5+
use GraphQL\Type\Definition\InterfaceType;
6+
57
class ObjectTypeConfig extends Config
68
{
7-
public function name($name)
8-
{
9-
$this->addConfig('name', $name, false);
10-
return $this;
11-
}
9+
use NameDescriptionConfigTrait;
10+
use FieldsConfigTrait;
1211

13-
public function description($description)
12+
/**
13+
* @param callable $interfaces
14+
*
15+
* @return $this
16+
*/
17+
public function interfaces(callable $interfaces)
1418
{
15-
$this->addConfig('description', $description, false);
19+
$this->addConfig('interfaces', $interfaces, false);
20+
1621
return $this;
1722
}
1823

19-
public function addField($name, $type, callable $resolve = null, $description = null, ArgsConfig $args = null, callable $complexity = null, $deprecationReason = null)
24+
/**
25+
* @param callable|FieldsConfig $fields
26+
*
27+
* @return $this
28+
*/
29+
public function fields($fields)
2030
{
21-
$field = [
22-
'name' => $name,
23-
'type' => $type,
24-
'description' => $description,
25-
'resolve' => $resolve,
26-
'complexity' => $complexity,
27-
'deprecationReason' => $deprecationReason,
28-
'args' => null === $args ? [] : $args->build(),
29-
];
30-
31-
$this->addConfig('fields', $field);
32-
31+
if ($fields instanceof FieldsConfig) {
32+
$this->addConfig('fields', $fields->build(), false);
33+
} else {
34+
$this->addConfig('fields', $fields, false);
35+
}
3336
return $this;
3437
}
3538

39+
/**
40+
* @param InterfaceType|callable $interface
41+
*
42+
* @return $this
43+
*/
3644
public function addInterface($interface)
3745
{
3846
$this->addConfig('interfaces', $interface);
47+
3948
return $this;
4049
}
4150

51+
/**
52+
* @param callable|null $isTypeOf
53+
*
54+
* @return $this
55+
*/
4256
public function isTypeOf(callable $isTypeOf = null)
4357
{
4458
$this->addConfig('isTypeOf', $isTypeOf, false);
59+
4560
return $this;
4661
}
4762

63+
/**
64+
* @param callable|null $resolveField
65+
*
66+
* @return $this
67+
*/
4868
public function resolveField(callable $resolveField = null)
4969
{
5070
$this->addConfig('resolveField', $resolveField, false);
71+
5172
return $this;
5273
}
5374
}

src/Type/Builder/TypeConfigTrait.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+
use GraphQL\Type\Definition\Type;
6+
7+
/**
8+
* Class TypeConfigTrait
9+
*
10+
* @method $this addConfig(string $name, mixed $value, boolean $append = true)
11+
*/
12+
trait TypeConfigTrait
13+
{
14+
/**
15+
* @param Type|callable $type
16+
*
17+
* @return $this
18+
*/
19+
public function type($type)
20+
{
21+
$this->addConfig('type', $type, false);
22+
23+
return $this;
24+
}
25+
}

tests/Type/Builder/ObjectTypeConfigTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ public function testBuild()
2020
$isTypeOf = function () {
2121
return true;
2222
};
23-
$isTypeOf = function () {
24-
return [];
25-
};
2623

2724
$config = ObjectTypeConfig::create()
2825
->name('TypeName')
29-
->addField('field1', Type::string(), $field1Resolver, 'description field1', $args)
26+
->addField('field1', Type::string(), 'description field1', $args, $field1Resolver)
3027
->addField('field2', Type::nonNull(Type::string()))
28+
->addDeprecatedField('deprecatedField', Type::string(), 'This field is deprecated.')
3129
->description('My new Object')
3230
->isTypeOf($isTypeOf)
3331
->resolveField();
@@ -57,16 +55,21 @@ public function testBuild()
5755
],
5856
],
5957
'complexity' => null,
60-
'deprecationReason' => null,
6158
],
6259
[
6360
'name' => 'field2',
6461
'type' => Type::nonNull(Type::string()),
6562
'description' => null,
6663
'resolve' => null,
6764
'complexity' => null,
68-
'deprecationReason' => null,
69-
'args' => [],
65+
],
66+
[
67+
'name' => 'deprecatedField',
68+
'type' => Type::string(),
69+
'description' => null,
70+
'resolve' => null,
71+
'complexity' => null,
72+
'deprecationReason' => 'This field is deprecated.',
7073
],
7174
],
7275
'isTypeOf' => $isTypeOf,

0 commit comments

Comments
 (0)