【问题标题】:Laravel GraphQL Access an Arg inside args FunctionLaravel GraphQL 在 args 函数中访问一个 Arg
【发布时间】:2023-03-19 13:50:01
【问题描述】:

我正在使用 Laravel 8 构建一个项目并安装了https://github.com/rebing/graphql-laravel,以便在我的项目中使用 GraphQL。我有一个名为“UpdateUserMutation”的突变,它从突变扩展而来,它有一个 args 函数来检索用户发送的数据。基本上是这样的;

    public function args(): array
    {
        return [
            'id' => [
                'name' => 'id',
                'type' => Type::nonNull(Type::int()),
                'rules' => [
                    'required',
                    'numeric',
                    'exists:users'
                ]
            ],

            'name' => [
                'name' => 'name',
                'type' => Type::string(),
                'rules' => [
                    'string',
                    'min:3',
                    'max:191'
                ]
            ],

            'email' => [
                'name' => 'email',
                'type' => Type::string(),
                'rules' => [
                    'string',
                    'min:3',
                    'max:191',
                    'email:rfc,dns',
                    'unique:users,email,' . 'id'
                ]
            ],

            'department_id' => [
                'name' => 'department_id',
                'type' => Type::int(),
                'rules' => [
                    'numeric',
                    'exists:departments,id'
                ]
            ]
        ];
    }
```

I define Laravel validation rules inside of each element. I need to define the 'unique' rule for the email field but of course, I need to exclude that user's email address if the user doesn't change his/her email address. So, I need to access the id element which is inside args function. How can I do that?

【问题讨论】:

    标签: laravel graphql laravel-8


    【解决方案1】:

    您可以将匿名函数传递给规则。请检查下面的代码,我已修复。

    [
        'name' => 'email',
        'type' => Type::string(),
        'rules' => function($args) {
            return [
            'string',
            'min:3',
            'max:191',
            'email:rfc,dns',
            'unique:users,email,' . $args['id'] . ',id'
            ]; 
        }
    ]
    

    【讨论】:

      猜你喜欢
      • 2020-06-06
      • 2020-12-14
      • 2017-11-04
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多