diff --git a/src/Annotation/Scalar.php b/src/Annotation/Scalar.php index 89d697a13..363861c29 100644 --- a/src/Annotation/Scalar.php +++ b/src/Annotation/Scalar.php @@ -22,4 +22,14 @@ final class Scalar implements Annotation * @var string */ public string $scalarType; + + /** + * @var string + */ + public string $doctrineType; + + /** + * @var string + */ + public string $phpType; } diff --git a/src/Config/Parser/AnnotationParser.php b/src/Config/Parser/AnnotationParser.php index 8ca6a6901..868bd8e6e 100644 --- a/src/Config/Parser/AnnotationParser.php +++ b/src/Config/Parser/AnnotationParser.php @@ -93,6 +93,7 @@ public static function reset(): void self::$classesMap = []; self::$providers = []; self::$graphClassCache = []; + self::$doctrineMapping = []; } /** @@ -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 { @@ -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; diff --git a/tests/Config/Parser/AnnotationParserTest.php b/tests/Config/Parser/AnnotationParserTest.php index 3c391ecbe..5be17b302 100644 --- a/tests/Config/Parser/AnnotationParserTest.php +++ b/tests/Config/Parser/AnnotationParserTest.php @@ -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 { diff --git a/tests/Config/Parser/fixtures/annotations/Scalar/ScalarWithTypeMapping.php b/tests/Config/Parser/fixtures/annotations/Scalar/ScalarWithTypeMapping.php new file mode 100644 index 000000000..0f220051c --- /dev/null +++ b/tests/Config/Parser/fixtures/annotations/Scalar/ScalarWithTypeMapping.php @@ -0,0 +1,14 @@ +