1

我的突变方案:

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

查询变量如下

{
  "id": 1
}

我将它与 laravel-graphql 一起使用。这是我的定义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;
    }
}

我使用我的查询字符串来请求 graphql 服务器,然后服务器返回我的错误

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

我尝试了很多东西并阅读了 github 上的文档 [ https://github.com/Folkloreatelier/laravel-graphql#creating-a-mutation][1]

并阅读了graphql网站的文档,并以多种方式更改了我的args定义样式,但都失败了,奇怪的是我可以获取args但使用静态变量,如follow

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

然后它起作用了!我试图胡思乱想,但对此一无所知。我想我真的需要帮助

4

2 回答 2

0

比较文档与您的代码,似乎在您的args方法中,您需要指定 arg 名称,如下所示:

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

参考:https ://github.com/Folkloreatelier/laravel-graphql#creating-a-query

希望这有帮助!

问候

于 2019-01-11T14:52:15.580 回答
0

原因是因为版本较低,如果你安装 laravel-graphql 的版本是 '~1.0.0',去修改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'
于 2019-01-11T17:24:59.123 回答