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

Can't set a scalar parameter with GraphQL services #410

Open
tlenclos opened this issue Oct 24, 2018 · 14 comments
Open

Can't set a scalar parameter with GraphQL services #410

tlenclos opened this issue Oct 24, 2018 · 14 comments
Labels

Comments

@tlenclos
Copy link

tlenclos commented Oct 24, 2018

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 tlenclos changed the title Can't set a scalar parameter into GraphQL classes Can't set a scalar parameter with GraphQL services Oct 24, 2018
@mcg-web mcg-web added the bug label Oct 24, 2018
@mcg-web
Copy link
Member

mcg-web commented Oct 24, 2018

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

@mcg-web
Copy link
Member

mcg-web commented Oct 24, 2018

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

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

@tlenclos
Copy link
Author

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

@mcg-web
Copy link
Member

mcg-web commented Oct 24, 2018

Can you try to remove MutationInterface

@tlenclos
Copy link
Author

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

@Sephirothus
Copy link

Sephirothus commented Mar 20, 2019

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.

@mcg-web
Copy link
Member

mcg-web commented Mar 20, 2019

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

@Sephirothus
Copy link

Sephirothus commented Mar 20, 2019

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!

@mcg-web
Copy link
Member

mcg-web commented Mar 20, 2019

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 }

@Sephirothus
Copy link

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

@mcg-web
Copy link
Member

mcg-web commented Mar 20, 2019

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

@Sephirothus
Copy link

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

@mcg-web
Copy link
Member

mcg-web commented Mar 20, 2019

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

@Sephirothus
Copy link

Sephirothus commented Mar 21, 2019

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:)

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

No branches or pull requests

3 participants