为什么我的注册突变不适用于角色字段?

问题描述 投票:0回答:2

为什么我的注册突变不适用于角色字段?

我编写了以下代码,但无法弄清楚为什么我的角色枚举不适用于我的突变。

我将代码上传到github Github code

注册突变起作用

mutation{
      registerUser(input:{email:"[email protected]", password:"[email protected]", role:"user",passwordConfirmation:"[email protected]"}){
        email

      }
    }

但是当我尝试登录突变时,它会失败

mutation{
  loginUser(input:{email:"[email protected]", password:"[email protected]", role:"user"}){
    token
    user{
      email
    }
  }
}

我得到这个错误:

"# KeyError at POST /api/graphiql\n\nException:\n\n    
** (KeyError) key :role not found in: %{email: \"[email protected]\", password: \"[email protected]\"}\n 

非常感谢您提供有关此问题的帮助或指导。

phoenix-framework ecto absinthe
2个回答
0
投票

所以我在做错事时,@ benwilson512在长生不老药上为我回答了。

以下关于我可以使用field(:role, :user_input)从user_input获取输入类型的假设是不正确的。

如果我想将角色传递给我的突变,最好的方法是使用field(:role, non_null(:string))并当作字符串。

但是尽管这样可以解决我遇到的问题,但是我仍然不确定解决该问题的最佳方法是什么。

因此,感谢所有关注我的问题的人。


0
投票

此外,如果您希望将角色限制为枚举值,那么以下方法应该起作用:

field(:role, :type :role),然后是带有值的角色的枚举。

此答案由@kokolegorille在elixirforum上提供。

解决了问题

© www.soinside.com 2019 - 2024. All rights reserved.