Skip to content

Commit 330b752

Browse files
authored
Merge pull request #233 from moufmouf/enum_issue
Fixing enum does not work as a response parameter
2 parents d92271b + e2c342c commit 330b752

File tree

7 files changed

+113
-10
lines changed

7 files changed

+113
-10
lines changed

docs/pagination.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ PHP arrays, Doctrine and [TDBM](https://thecodingmachine.github.io/tdbm/doc/limi
1414
<div class="alert alert-warning">If you are a Laravel user, Eloquent does not come with a Porpaginas
1515
iterator. However, <a href="laravel-package-advanced">the GraphQLite Laravel bundle comes with its own pagination system</a>.</div>
1616

17+
## Installation
18+
19+
You will need to install the [Porpaginas](https://github.com/beberlei/porpaginas) library to benefit from this feature.
20+
21+
```bash
22+
$ composer require beberlei/porpaginas
23+
```
24+
1725
## Usage
1826

1927
In your query, simply return a class that implements `Porpaginas\Result`:

src/Mappers/Root/MyCLabsEnumTypeMapper.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use phpDocumentor\Reflection\Type;
1515
use phpDocumentor\Reflection\Types\Object_;
1616
use ReflectionMethod;
17+
use TheCodingMachine\GraphQLite\Types\MyCLabsEnumType;
1718
use function is_a;
1819
use function str_replace;
1920
use function strpos;
@@ -87,16 +88,7 @@ private function mapByClassName(string $enumClass): ?EnumType
8788
return $this->cache[$enumClass];
8889
}
8990

90-
$consts = $enumClass::toArray();
91-
$constInstances = [];
92-
foreach ($consts as $key => $value) {
93-
$constInstances[$key] = ['value' => $enumClass::$key()];
94-
}
95-
96-
return $this->cache[$enumClass] = new EnumType([
97-
'name' => 'MyCLabsEnum_' . str_replace('\\', '__', $enumClass),
98-
'values' => $constInstances,
99-
]);
91+
return $this->cache[$enumClass] = new MyCLabsEnumType($enumClass);
10092
}
10193

10294
/**

src/Types/MyCLabsEnumType.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\GraphQLite\Types;
6+
7+
use GraphQL\Type\Definition\EnumType;
8+
use InvalidArgumentException;
9+
use MyCLabs\Enum\Enum;
10+
use function str_replace;
11+
12+
/**
13+
* An extension of the EnumType to support Myclabs enum.
14+
*
15+
* This implementation is needed to overwrite the default "serialize" method, that expects to see the exact same object
16+
* (while there can be several instances of the same enum value with MyclabsEnum)
17+
*/
18+
class MyCLabsEnumType extends EnumType
19+
{
20+
public function __construct(string $enumClassName)
21+
{
22+
$consts = $enumClassName::toArray();
23+
$constInstances = [];
24+
foreach ($consts as $key => $value) {
25+
$constInstances[$key] = ['value' => $enumClassName::$key()];
26+
}
27+
28+
parent::__construct([
29+
'name' => 'MyCLabsEnum_' . str_replace('\\', '__', $enumClassName),
30+
'values' => $constInstances,
31+
]);
32+
}
33+
34+
/**
35+
* @param mixed $value
36+
*
37+
* @return mixed
38+
*/
39+
public function serialize($value)
40+
{
41+
if (! $value instanceof Enum) {
42+
throw new InvalidArgumentException('Expected a Myclabs Enum instance');
43+
}
44+
return $value->getKey();
45+
}
46+
}

tests/Fixtures/Integration/Controllers/ProductController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public function echoProductType(ProductTypeEnum $productType): ProductTypeEnum
5151
return $productType;
5252
}
5353

54+
/**
55+
* @Query()
56+
*/
57+
public function echoSomeProductType(): ProductTypeEnum
58+
{
59+
return ProductTypeEnum::FOOD();
60+
}
61+
5462
/**
5563
* @Query()
5664
*/

tests/Integration/EndToEndTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,29 @@ public function testEndToEndEnums(): void
934934
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
935935
}
936936

937+
public function testEndToEndEnums2(): void
938+
{
939+
/**
940+
* @var Schema $schema
941+
*/
942+
$schema = $this->mainContainer->get(Schema::class);
943+
944+
$queryString = '
945+
query {
946+
echoSomeProductType
947+
}
948+
';
949+
950+
$result = GraphQL::executeQuery(
951+
$schema,
952+
$queryString
953+
);
954+
955+
$this->assertSame([
956+
'echoSomeProductType' => 'FOOD'
957+
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
958+
}
959+
937960
public function testEndToEndDateTime(): void
938961
{
939962
/**

tests/Types/MyclabsEnumTypeTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace TheCodingMachine\GraphQLite\Types;
4+
5+
use InvalidArgumentException;
6+
use PHPUnit\Framework\TestCase;
7+
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\ProductTypeEnum;
8+
9+
class MyclabsEnumTypeTest extends TestCase
10+
{
11+
public function testException()
12+
{
13+
$enumType = new MyCLabsEnumType(ProductTypeEnum::class);
14+
$this->expectException(InvalidArgumentException::class);
15+
$this->expectExceptionMessage('Expected a Myclabs Enum instance');
16+
$enumType->serialize('foo');
17+
}
18+
}

website/versioned_docs/version-4.0/pagination.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ PHP arrays, Doctrine and [TDBM](https://thecodingmachine.github.io/tdbm/doc/limi
1515
<div class="alert alert-warning">If you are a Laravel user, Eloquent does not come with a Porpaginas
1616
iterator. However, the GraphQLite Laravel bundle <a href="laravel-package-advanced.md">comes with its own pagination system</a>.</div>
1717

18+
## Installation
19+
20+
You will need to install the [Porpaginas](https://github.com/beberlei/porpaginas) library to benefit from this feature.
21+
22+
```bash
23+
$ composer require beberlei/porpaginas
24+
```
25+
1826
## Usage
1927

2028
In your query, simply return a class that implements `Porpaginas\Result`:

0 commit comments

Comments
 (0)