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

Default enum value for argument when using annotations #1149

Open
mathroc opened this issue Jan 12, 2024 · 3 comments
Open

Default enum value for argument when using annotations #1149

mathroc opened this issue Jan 12, 2024 · 3 comments

Comments

@mathroc
Copy link
Contributor

mathroc commented Jan 12, 2024

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Version/Branch 1.0.0

With something liek this:

#[GQL\Provider]
final class Messages
{
    #[GQL\Query]
    public function messages(
        int $page = 0,
        int $pageSize = 100,
        ResolveStrategy $strategy = ResolveStrategy::InMemory,
    ): ?Message { ... }
}

It gives me this error:

Cannot stringify object of class: 'Proton\Apps\MailExperimental\Application\GraphQL\Query\ResolveStrategy'.

I suppose the enum value is not serialized to its name somewhere when generating the types

Specifying the arguments in the [GQL\Arg] attribute bypass the issue

#[GQL\Provider]
#[GQL\Arg(name: 'page', default: 0, type: 'Int!')]
#[GQL\Arg(name: 'pageSize', default: 100, type: 'Int!')]
#[GQL\Arg(name: 'strategy', default: 'InMemory', type: 'ResolveStrategy!')]
final class Messages
{
    #[GQL\Query]
    public function messages(
        int $page = 0,
        int $pageSize = 100,
        ResolveStrategy $strategy = ResolveStrategy::InMemory,
    ): ?Message { ... }
}
@Vincz
Copy link
Collaborator

Vincz commented Jan 12, 2024

Hi @mathroc! Would you have a complete stack trace of the error plz?

@mathroc
Copy link
Contributor Author

mathroc commented Jan 12, 2024

sure:

In Utils.php line 109:

[Exception]
Cannot stringify object of class: 'Proton\Apps\MailExperimental\Application\GraphQL\Query\ResolveStrategy'.

Exception trace:
at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:109
Murtukov\PHPCodeGenerator\Utils::stringifyValue() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:133
Murtukov\PHPCodeGenerator\Utils::stringifyAssocArray() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:93
Murtukov\PHPCodeGenerator\Utils::stringifyValue() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:56
Murtukov\PHPCodeGenerator\Utils::stringify() at /var/www/vendor/murtukov/php-code-generator/src/Collection.php:202
Murtukov\PHPCodeGenerator\Collection->generate() at /var/www/vendor/murtukov/php-code-generator/src/AbstractGenerator.php:13
Murtukov\PHPCodeGenerator\AbstractGenerator->__toString() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:113
Murtukov\PHPCodeGenerator\Utils::stringifyValue() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:163
Murtukov\PHPCodeGenerator\Utils::stringifyNumericArray() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:94
Murtukov\PHPCodeGenerator\Utils::stringifyValue() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:56
Murtukov\PHPCodeGenerator\Utils::stringify() at /var/www/vendor/murtukov/php-code-generator/src/Collection.php:202
Murtukov\PHPCodeGenerator\Collection->generate() at /var/www/vendor/murtukov/php-code-generator/src/AbstractGenerator.php:13
Murtukov\PHPCodeGenerator\AbstractGenerator->__toString() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:113
Murtukov\PHPCodeGenerator\Utils::stringifyValue() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:133
Murtukov\PHPCodeGenerator\Utils::stringifyAssocArray() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:93
Murtukov\PHPCodeGenerator\Utils::stringifyValue() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:56
Murtukov\PHPCodeGenerator\Utils::stringify() at /var/www/vendor/murtukov/php-code-generator/src/Collection.php:202
Murtukov\PHPCodeGenerator\Collection->generate() at /var/www/vendor/murtukov/php-code-generator/src/AbstractGenerator.php:13
Murtukov\PHPCodeGenerator\AbstractGenerator->__toString() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:113
Murtukov\PHPCodeGenerator\Utils::stringifyValue() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:133
Murtukov\PHPCodeGenerator\Utils::stringifyAssocArray() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:93
Murtukov\PHPCodeGenerator\Utils::stringifyValue() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:56
Murtukov\PHPCodeGenerator\Utils::stringify() at /var/www/vendor/murtukov/php-code-generator/src/Collection.php:202
Murtukov\PHPCodeGenerator\Collection->generate() at /var/www/vendor/murtukov/php-code-generator/src/AbstractGenerator.php:13
Murtukov\PHPCodeGenerator\AbstractGenerator->__toString() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:113
Murtukov\PHPCodeGenerator\Utils::stringifyValue() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:56
Murtukov\PHPCodeGenerator\Utils::stringify() at /var/www/vendor/murtukov/php-code-generator/src/ArrowFunction.php:35
Murtukov\PHPCodeGenerator\ArrowFunction->generate() at /var/www/vendor/murtukov/php-code-generator/src/AbstractGenerator.php:13
Murtukov\PHPCodeGenerator\AbstractGenerator->__toString() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:113
Murtukov\PHPCodeGenerator\Utils::stringifyValue() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:133
Murtukov\PHPCodeGenerator\Utils::stringifyAssocArray() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:93
Murtukov\PHPCodeGenerator\Utils::stringifyValue() at /var/www/vendor/murtukov/php-code-generator/src/Utils.php:56
Murtukov\PHPCodeGenerator\Utils::stringify() at /var/www/vendor/murtukov/php-code-generator/src/Collection.php:202
Murtukov\PHPCodeGenerator\Collection->generate() at /var/www/vendor/murtukov/php-code-generator/src/AbstractGenerator.php:13
Murtukov\PHPCodeGenerator\AbstractGenerator->__toString() at n/a:n/a
join() at /var/www/vendor/murtukov/php-code-generator/src/ScopedContentTrait.php:103
Murtukov\PHPCodeGenerator\Method->Murtukov\PHPCodeGenerator{closure}() at n/a:n/a
array_map() at /var/www/vendor/murtukov/php-code-generator/src/ScopedContentTrait.php:103
Murtukov\PHPCodeGenerator\Method->generateContent() at /var/www/vendor/murtukov/php-code-generator/src/ScopedContentTrait.php:115
Murtukov\PHPCodeGenerator\Method->generateWrappedContent() at /var/www/vendor/murtukov/php-code-generator/src/Method.php:34
Murtukov\PHPCodeGenerator\Method->generate() at /var/www/vendor/murtukov/php-code-generator/src/AbstractGenerator.php:13
Murtukov\PHPCodeGenerator\AbstractGenerator->__toString() at n/a:n/a
join() at /var/www/vendor/murtukov/php-code-generator/src/ScopedContentTrait.php:103
Murtukov\PHPCodeGenerator\OOPStructure->Murtukov\PHPCodeGenerator{closure}() at n/a:n/a
array_map() at /var/www/vendor/murtukov/php-code-generator/src/ScopedContentTrait.php:103
Murtukov\PHPCodeGenerator\OOPStructure->generateContent() at /var/www/vendor/murtukov/php-code-generator/src/ScopedContentTrait.php:115
Murtukov\PHPCodeGenerator\OOPStructure->generateWrappedContent() at /var/www/vendor/murtukov/php-code-generator/src/PhpClass.php:108
Murtukov\PHPCodeGenerator\PhpClass->generate() at /var/www/vendor/murtukov/php-code-generator/src/AbstractGenerator.php:13
Murtukov\PHPCodeGenerator\AbstractGenerator->__toString() at n/a:n/a
implode() at /var/www/vendor/murtukov/php-code-generator/src/PhpFile.php:39
Murtukov\PHPCodeGenerator\PhpFile->generate() at /var/www/vendor/murtukov/php-code-generator/src/PhpFile.php:50
Murtukov\PHPCodeGenerator\PhpFile->__toString() at n/a:n/a
file_put_contents() at /var/www/vendor/murtukov/php-code-generator/src/PhpFile.php:161
Murtukov\PHPCodeGenerator\PhpFile->save() at /var/www/vendor/overblog/graphql-bundle/src/Generator/TypeGenerator.php:93
Overblog\GraphQLBundle\Generator\TypeGenerator->generateClass() at /var/www/vendor/overblog/graphql-bundle/src/Generator/TypeGenerator.php:61
Overblog\GraphQLBundle\Generator\TypeGenerator->compile() at /var/www/vendor/overblog/graphql-bundle/src/CacheWarmer/CompileCacheWarmer.php:44
Overblog\GraphQLBundle\CacheWarmer\CompileCacheWarmer->warmUp() at /var/www/vendor/symfony/http-kernel/CacheWarmer/CacheWarmerAggregate.php:108
Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp() at /var/www/vendor/symfony/http-kernel/Kernel.php:545
Symfony\Component\HttpKernel\Kernel->initializeContainer() at /var/www/vendor/symfony/http-kernel/Kernel.php:763
Symfony\Component\HttpKernel\Kernel->preBoot() at /var/www/vendor/symfony/http-kernel/Kernel.php:126
Symfony\Component\HttpKernel\Kernel->boot() at /var/www/vendor/symfony/framework-bundle/Console/Application.php:190
Symfony\Bundle\FrameworkBundle\Console\Application->registerCommands() at /var/www/vendor/symfony/framework-bundle/Console/Application.php:72
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/vendor/symfony/console/Application.php:175
Symfony\Component\Console\Application->run() at /var/www/vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:49
Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run() at /var/www/vendor/autoload_runtime.php:31

@mathroc
Copy link
Contributor Author

mathroc commented Jan 12, 2024

the workaround I mentioned isn't working in fact. It allows to build the types but at runtime it fails when the argument is omitted because the default is a string, so the reverse transformation isn't happening either for the default

stack trace:

[GraphQL] TypeError: Proton\Apps\MailExperimental\Application\GraphQL\Query\Messages::messages(): Argument #3 ($strategy) must be of type Proton\Apps\MailExperimental\Application\GraphQL\Query\ResolveStrategy, string given, called in /var/www/var/cache/mail-experimental/dev/graphql/types/QueryType.php on line 52[0] (caught throwable) at /var/www/apps/MailExperimental/src/Application/GraphQL/Query/Messages.php line 23.

/var/www/apps/MailExperimental/src/Application/GraphQL/Query/Messages.php:23 {▶}
/var/www/var/cache/mail-experimental/dev/graphql/types/QueryType.php:52 {▶}
/var/www/vendor/overblog/graphql-bundle/src/Definition/ArgumentFactory.php:38 {▶}
/var/www/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php:714 {▶}
/var/www/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php:631 {▶}
/var/www/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php:1317 {▶}
/var/www/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php:298 {▶}
/var/www/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php:237 {▶}
/var/www/vendor/webonyx/graphql-php/src/Executor/Executor.php:159 {▶}
/var/www/vendor/webonyx/graphql-php/src/GraphQL.php:162 {▶}
/var/www/vendor/overblog/graphql-bundle/src/Executor/Executor.php:44 {▶}
/var/www/vendor/overblog/graphql-bundle/src/Request/Executor.php:149 {▶}
/var/www/vendor/overblog/graphql-bundle/src/Controller/GraphController.php:117 {▶}
/var/www/vendor/overblog/graphql-bundle/src/Controller/GraphController.php:89 {▶}
/var/www/vendor/overblog/graphql-bundle/src/Controller/GraphController.php:65 {▶}
/var/www/vendor/overblog/graphql-bundle/src/Controller/GraphController.php:43 {▶}
/var/www/vendor/symfony/http-kernel/HttpKernel.php:181 {▶}
/var/www/vendor/symfony/http-kernel/HttpKernel.php:76 {▶}
/var/www/vendor/symfony/http-kernel/Kernel.php:197 {▶}
/var/www/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php:35 {▶}

so the only workaround I could find is by making the argument nullable and having a null default:

    #[GQL\Query]
    public function messages(
        int $page = 0,
        int $pageSize = 100,
        ?ResolveStrategy $strategy = null,
    ): ?Message {
        $strategy ??= ResolveStrategy::InMemory;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants