overblog / GraphQLBundle

This bundle provides tools to build a complete GraphQL API server in your Symfony App.
MIT License
783 stars 221 forks source link

ScalarType is not processed correctly when defined with Annotations #1132

Open ageurts opened 1 year ago

ageurts commented 1 year ago
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Version/Branch 1.0

I'm using annotations in Symfony 6 on PHP 8.1 and I'm trying to define a custom scalar type for using the file upload feature. Somehow the scalarType class name is not processed correctly and I end up with a faulty Type class.

This is my definition:

<?php

namespace App\GraphQL\CustomTypes;

use Overblog\GraphQLBundle\Annotation\Scalar;

#[Scalar(name: 'UploadImage', scalarType: '@=newObject("Overblog\\GraphQLBundle\\Upload\\Type\\GraphQLUploadType")')]
class UploadImageType
{
}

and this is the (faulty) generated Type class (see the scalarType line):

<?php

namespace Overblog\GraphQLBundle\__DEFINITIONS__;

use Overblog\GraphQLBundle\Definition\ConfigProcessor;
use Overblog\GraphQLBundle\Definition\GraphQLServices;
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface;
use Overblog\GraphQLBundle\Definition\Type\CustomScalarType;
use Overblog\GraphQLBundle\Definition\Type\GeneratedTypeInterface;

/**
 * THIS FILE WAS GENERATED AND SHOULD NOT BE EDITED MANUALLY.
 */
final class UploadImageType extends CustomScalarType implements GeneratedTypeInterface, AliasedInterface
{
    public const NAME = 'UploadImage';

    public function __construct(ConfigProcessor $configProcessor, GraphQLServices $services)
    {
        $config = [
            'name' => self::NAME,
            'scalarType' => (new \ReflectionClass("OverblogGraphQLBundleUploadTypeGraphQLUploadType"))->newInstanceArgs([]),
        ];

        parent::__construct($configProcessor->process($config));
    }

    /**
     * {@inheritdoc}
     */
    public static function getAliases(): array
    {
        return [self::NAME];
    }
}

The scalar class 'OverblogGraphQLBundleUploadTypeGraphQLUploadType' does not exist of course.

The Type class is generated correctly if I use the YAML variant.

UploadImage:
    type: custom-scalar
    config:
        scalarType: '@=newObject("Overblog\\GraphQLBundle\\Upload\\Type\\GraphQLUploadType")'

UploadImageType.php:

    public function __construct(ConfigProcessor $configProcessor, GraphQLServices $services)
    {
        $config = [
            'name' => self::NAME,
            'scalarType' => (new \ReflectionClass("Overblog\\GraphQLBundle\\Upload\\Type\\GraphQLUploadType"))->newInstanceArgs([]),
        ];

        parent::__construct($configProcessor->process($config));
    }

Is this a bug in the type class generation or am I using Annotations wrong?

ageurts commented 1 year ago

So I found that escaping the class in the Annotation with 4 slashes fixes the generated class.

<?php

namespace App\GraphQL\CustomTypes;

use Overblog\GraphQLBundle\Annotation\Scalar;

#[Scalar(name: 'UploadImage', scalarType: '@=newObject("Overblog\\\\GraphQLBundle\\\\Upload\\\\Type\\\\GraphQLUploadType")')]
class UploadImageType
{
}

But I'm guessing this is not supposed to work like this.