GraphQLBundle icon indicating copy to clipboard operation
GraphQLBundle copied to clipboard

Can't set a scalar parameter with GraphQL services

Open tlenclos opened this issue 5 years ago • 14 comments

Q A
Bug report? yes
Version 0.11.9
Symfony version 4.1

Hi and thanks for your work !

I think I have encountered a bug in the compiler pass of the bundle. I can't manage to use named parameter with autowiring for my GraphQL services, they are reset somehow.

Reproduction

in services.yaml

    App\GraphQL\Mutation\UploadMutation:
        arguments:
            $cardUploadDir: '%card_upload_dir%'

in App\GraphQL\Mutation\UploadMutation

class UploadMutation implements MutationInterface, AliasedInterface
{
    private $cardUploadDir;

    public function __construct(string $cardUploadDir)

I always end up with the exception

Cannot autowire service "App\GraphQL\Mutation\UploadMutation": argument "$cardUploadDir" of method "__construct()" is type-hinted "string", you should configure its value explicitly.

Temporary fix for me

Quick and dirty fix for me was to override the process function in my kernel.

    public function process(ContainerBuilder $container)
    {
        $container->getDefinition('App\GraphQL\Mutation\UploadMutation')->setArgument('$cardUploadDir', '%card_upload_dir%');
    }

tlenclos avatar Oct 24 '18 10:10 tlenclos

Hi! thanks for reporting this issue and the reproduction. We'll try to give this a fix quickly.

mcg-web avatar Oct 24 '18 12:10 mcg-web

Ok I get it, your service is missing the mutation tag

    App\GraphQL\Mutation\UploadMutation:
        arguments:
            $cardUploadDir: '%card_upload_dir%'
        tags: ['overblog_graphql.mutation']

mcg-web avatar Oct 24 '18 14:10 mcg-web

I'm having the same issue with the tag sadly.

tlenclos avatar Oct 24 '18 14:10 tlenclos

Can you try to remove MutationInterface

mcg-web avatar Oct 24 '18 14:10 mcg-web

I'm having a weird exception now with

class UploadMutation implements AliasedInterface
{
    App\GraphQL\Mutation\UploadMutation:
        tags: ['overblog_graphql.mutation']
        arguments:
            $cardUploadDir: '%card_upload_dir%'

image

tlenclos avatar Oct 24 '18 14:10 tlenclos

Hi, what's the status on this bug? I'm using your package and I need to pass entity manager with alternative connection. I'm doing this from services.yaml

        AppBundle\GraphQL\Mutation\UserMutation:
              tags: ['@overblog_graphql.mutation']
              arguments:
                  $em: '@doctrine.orm.user_entity_manager'

And when I autowire argument in class __constuct, I get default entity manager.

Sephirothus avatar Mar 20 '19 09:03 Sephirothus

This bug was not fixed since this could introduce BC, this come from AliasedInterface usage, removed it and defined your aliases using tags.

mcg-web avatar Mar 20 '19 10:03 mcg-web

Thanx for quick answer, but it did not help:( I'll post my whole code, please look at it, maybe I've made a mistake somewhere.

mutation class:

Class UserMutation implements MutationInterface
{
    /**
     * @var EntityManagerInterface
     */
    protected $em;

    public function __construct(EntityManagerInterface $em)
    {
        \Doctrine\Common\Util\Debug::dump($em->getConnection());die; // here I get default connection
        $this->em = $em;
    }

    public function createUser(array $args): User
    {...}
}

services.yaml

AppBundle\GraphQL\Mutation\UserMutation:
        arguments:
            $em: '@doctrine.orm.user_entity_manager'
        tags: 
            - { name: overblog_graphql.mutation, method: createUser, alias: createUser }

Mutation.yaml

Mutation:
    type: object
    config:
        fields:
            createUser:
                type: User!
                resolve: '@=mutation("AppBundle\\GraphQL\\Mutation\\UserMutation::createUser", [args["input"]])'
                args:
                    input:
                        type: UserInput!

Sephirothus avatar Mar 20 '19 11:03 Sephirothus

if you want to use fqcn you must remove alias:

AppBundle\GraphQL\Mutation\UserMutation:
        arguments:
            $em: '@doctrine.orm.user_entity_manager'
        tags: 
            - { name: overblog_graphql.mutation, method: createUser }

mcg-web avatar Mar 20 '19 13:03 mcg-web

Did not helped, still default connection:( Any thoughts?

Sephirothus avatar Mar 20 '19 13:03 Sephirothus

Do you have some wildcard rules in your dependency injection configuration?

mcg-web avatar Mar 20 '19 14:03 mcg-web

I get a quick look at project and not found any wildcards

Sephirothus avatar Mar 20 '19 14:03 Sephirothus

Can you name your service

app.user_mutation:
        class: AppBundle\GraphQL\Mutation\UserMutation:
        arguments:
            $em: '@doctrine.orm.user_entity_manager'
        tags: 
            - { name: overblog_graphql.mutation, method: createUser }

what is the output of this CLI?

bin/console graphql:debug --category=mutation

mcg-web avatar Mar 20 '19 15:03 mcg-web

solution id: AppBundle\GraphQL\Mutation\UserMutation::createUser
app.user_mutation::createUser aliases :

It's shown in table, but I don't know how to visualise table, sorry:)

Sephirothus avatar Mar 21 '19 11:03 Sephirothus