Skip to content

Commit b9456e2

Browse files
committed
first commit
0 parents  commit b9456e2

File tree

6 files changed

+884
-0
lines changed

6 files changed

+884
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
4+
.DS_Store
5+
*/.DS_Store
6+
*/*/.DS_Store
7+
*/*/*/.DS_Store
8+
.phplint-cache

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "dmitryrechkin/php-graphql-typeregistry",
3+
"type": "php-graphql-typeregistry",
4+
"description": "PHP GraphQL Type Registry which allows to add implementations of the Type to it and get a list of all the registered types",
5+
"keywords": [
6+
],
7+
"license": "AGPL-3.0-only",
8+
"authors": [
9+
{
10+
"name": "Dmitry Rechkin",
11+
"email": "[email protected]",
12+
"homepage": "https://github.com/dmitryrechkin/"
13+
}
14+
],
15+
"minimum-stability": "dev",
16+
"require": {
17+
"php": "^7.3 || ^8.0",
18+
"webonyx/graphql-php": "^14.11"
19+
},
20+
"require-dev": {
21+
"dmitryrechkin/php-standard": "^1.0",
22+
"dmitryrechkin/php-phpunit-bootstrap": "^1.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"DmitryRechkin\\" : "src/"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"DmitryRechkin\\Tests\\" : "tests/"
32+
}
33+
},
34+
"config": {
35+
"allow-plugins": {
36+
"dealerdirect/phpcodesniffer-composer-installer": true
37+
}
38+
}
39+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DmitryRechkin\GraphQL\TypeRegistry;
6+
7+
use GraphQL\Type\Definition\LeafType;
8+
use GraphQL\Type\Definition\Type;
9+
10+
class TypeRegistry implements TypeRegistryInterface
11+
{
12+
/**
13+
* @var array<Type>
14+
*/
15+
private $types;
16+
17+
/**
18+
* constructor
19+
*/
20+
public function __construct()
21+
{
22+
$this->types = [];
23+
}
24+
25+
/**
26+
* adds a given type to the registry
27+
*
28+
* @param Type $type
29+
* @return self
30+
*/
31+
public function addType(Type $type): self
32+
{
33+
$this->types[$type->name] = $this->prepareType($type);
34+
35+
return $this;
36+
}
37+
38+
/**
39+
* returns a list of all registered types
40+
*
41+
* @return array<Type>
42+
*/
43+
public function getTypes(): array
44+
{
45+
return $this->types;
46+
}
47+
48+
/**
49+
* prepares a given type and returns it
50+
*
51+
* @param Type $type
52+
* @return Type
53+
*/
54+
private function prepareType(Type $type): Type
55+
{
56+
if (false === ($type instanceof LeafType)) {
57+
return $type;
58+
}
59+
60+
if (empty($type->config['serialize'])) {
61+
$type->config['serialize'] = function ($value) use ($type) {
62+
return $type->serialize($value);
63+
};
64+
}
65+
66+
if (empty($type->config['parseLiteral'])) {
67+
$type->config['parseLiteral'] = function ($value) use ($type) {
68+
return $type->parseLiteral($value);
69+
};
70+
}
71+
72+
if (empty($type->config['parseValue'])) {
73+
$type->config['parseValue'] = function ($value) use ($type) {
74+
return $type->parseValue($value);
75+
};
76+
}
77+
78+
return $type;
79+
}
80+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DmitryRechkin\GraphQL\TypeRegistry;
6+
7+
use GraphQL\Type\Definition\Type;
8+
9+
interface TypeRegistryInterface
10+
{
11+
/**
12+
* adds a given type to the registry
13+
*
14+
* @param Type $type
15+
* @return self
16+
*/
17+
public function addType(Type $type): self;
18+
19+
/**
20+
* returns a list of all registered types
21+
*
22+
* @return array<Type>
23+
*/
24+
public function getTypes(): array;
25+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DmitryRechkin\Tests\Unit\GraphQL\TypeRegistry;
6+
7+
use DmitryRechkin\GraphQL\TypeRegistry\TypeRegistry;
8+
use GraphQL\Type\Definition\BooleanType;
9+
use GraphQL\Type\Definition\EnumType;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class TypeRegistryTest extends TestCase
13+
{
14+
/**
15+
* @return void
16+
*/
17+
public function testAddTypeReturnsSelf(): void
18+
{
19+
$typeRegistry = new TypeRegistry();
20+
$this->assertSame($typeRegistry, $typeRegistry->addType(new BooleanType()));
21+
}
22+
23+
/**
24+
* @return void
25+
*/
26+
public function testAddTypeAddsSameTypeOnlyOnce(): void
27+
{
28+
$typeRegistry = new TypeRegistry();
29+
$typeRegistry->addType(new BooleanType());
30+
$typeRegistry->addType(new BooleanType());
31+
32+
$this->assertSame(1, count($typeRegistry->getTypes()));
33+
}
34+
35+
/**
36+
* @return void
37+
*/
38+
public function testGetTypesReturnsAllPreviouslyAddedTypes(): void
39+
{
40+
$booleanType = new BooleanType();
41+
$enumType = new EnumType(['name' => 'blah']);
42+
43+
$types = [
44+
$booleanType->name => $booleanType,
45+
$enumType->name => $enumType,
46+
];
47+
48+
$typeRegistry = new TypeRegistry();
49+
foreach ($types as $type) {
50+
$typeRegistry->addType($type);
51+
}
52+
53+
$outputTypes = $typeRegistry->getTypes();
54+
foreach ($outputTypes as $name => $outputType) {
55+
$this->assertInstanceOf(get_class($types[$name]), $outputType);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)