Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible for scalar annotation to register Doctrine and PHP types to map #751

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Annotation/Scalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ final class Scalar implements Annotation
* @var string
*/
public string $scalarType;

/**
* @var string
*/
public string $doctrineType;

/**
* @var string
*/
public string $phpType;
}
13 changes: 12 additions & 1 deletion src/Config/Parser/AnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public static function reset(): void
self::$classesMap = [];
self::$providers = [];
self::$graphClassCache = [];
self::$doctrineMapping = [];
}

/**
Expand All @@ -102,7 +103,7 @@ public static function reset(): void
*/
private static function processFile(SplFileInfo $file, ContainerBuilder $container, array $configs, bool $preProcess): array
{
self::$doctrineMapping = $configs['doctrine']['types_mapping'];
self::$doctrineMapping += $configs['doctrine']['types_mapping'];
$container->addResource(new FileResource($file->getRealPath()));

try {
Expand Down Expand Up @@ -189,6 +190,16 @@ private static function classAnnotationsToGQLConfiguration(
$gqlType = self::GQL_SCALAR;
if (!$preProcess) {
$gqlConfiguration = self::scalarAnnotationToGQLConfiguration($graphClass, $classAnnotation);
} else {
if (isset($classAnnotation->doctrineType)) {
$gqlName = $classAnnotation->name ?? $graphClass->getShortName();
self::$doctrineMapping[$classAnnotation->doctrineType] = $gqlName;
}
if (isset($classAnnotation->phpType)) {
self::$classesMap[$gqlName] = ['type' => $gqlType, 'class' => $classAnnotation->phpType];

return $gqlTypes;
}
}
break;

Expand Down
10 changes: 10 additions & 0 deletions tests/Config/Parser/AnnotationParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,16 @@ public function testRelayConnectionEdge(): void
]);
}

public function testScalarRegisterType(): void
{
$this->expect('CustomScalarType', 'object', [
'fields' => [
'doctrineDatetime' => ['type' => 'ScalarWithTypeMapping'],
'phpDatetime' => ['type' => 'ScalarWithTypeMapping!'],
],
]);
}

public function testInvalidParamGuessing(): void
{
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Overblog\GraphQLBundle\Tests\Config\Parser\fixtures\annotations\Scalar;

use Overblog\GraphQLBundle\Annotation as GQL;

/**
* @GQL\Scalar(doctrineType="datetime", phpType=\DateTime::class)
*/
class ScalarWithTypeMapping
{
}
27 changes: 27 additions & 0 deletions tests/Config/Parser/fixtures/annotations/Type/CustomScalarType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Overblog\GraphQLBundle\Tests\Config\Parser\fixtures\annotations\Type;

use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Overblog\GraphQLBundle\Annotation as GQL;

/**
* @GQL\Type
*/
class CustomScalarType
{
/**
* @ORM\Column(type="datetime", nullable=true)
* @GQL\Field
*/
// @phpstan-ignore-next-line
bartv2 marked this conversation as resolved.
Show resolved Hide resolved
protected $doctrineDatetime;

/**
* @GQL\Field
*/
protected DateTime $phpDatetime;
}