Search code examples
laravel-5graphqllaravel-5.7graphql-php

Why Graphql cannot get the args by using variable but worked when using static var


My mutation scheme:

mutation edit($id: Int!) {
  user_edit(id:$id) {
    user_name
  }
}

query variable is follow

{
  "id": 1
}

I use this with laravel-graphql. Here is my definition of user_edit

class UserEdit extends Mutation
{
    protected $attributes = [
        'name' => 'UserEdit',
        'description' => 'A mutation'
    ];

    public function type()
    {
        return GraphQL::type('UserInfo');
    }

    public function args()
    {
        return [
            'id' => [
                'type' => Type::int(),
            ],
        ];
    }

    public function resolve($root, $args, $context, ResolveInfo $info)
    {
        var_dump($args);exit;
        $data = User::find($args['id']);

        return $data;
    }
}

I use my query string to request the graphql server, then server return my error

{
  "data": null,
  "errors": [
    {
      "message": "Variable \"$edit1\" of required type \"Int!\" was not provided.",
      "locations": [
        {
          "line": 1,
          "column": 11
        }
      ]
    }
  ]
}

l tried a lot things and read the document on github [https://github.com/Folkloreatelier/laravel-graphql#creating-a-mutation][1]

and read the document of graphql's website ,and change my args definition style in many ways, but all failed, and Strange is l can get args but use static variable like follow

mutation edit{
      user_edit(id:1) {
        user_name
      }
    }

and then it worked! l tried to goole but get nothing about this. l think l really need some help


Solution

  • Reason is because of lower version, if you install laravel-graphql version with '~1.0.0', go to modify config/graphql.php

    // The name of the input that contain variables when you query the endpoint.
    // Some library use "variables", you can change it here. "params" will stay
    // the default for now but will be changed to "variables" in the next major
    // release.
    
    'variables_input_name' => 'variables', // modify here from 'params' to 'variables'