首页 > 解决方案 > Strapi graphql 突变

问题描述

我正在使用 strapi 作为无头 CMS

我创建了一个客户内容类型并安装了 graphql 插件。

我正在尝试在 graphql 游乐场中创建一个突变以创建一个新客户

mutation createCustomer($username: String!, $email: String, $password: String){
  createCustomer(username: $username, email: $email, password: $password){
    _id username email password
  }
}

我在查询变量中传递用户名等

{ "username": "user1" }
{ "email": "User1@test.com" }
{"password":"123456"}   

我收到错误

"message": "Unknown argument \"username\" on field \"createCustomer\" of type \"Mutation\".",

如果我查看文档,它看起来像。

在此处输入图像描述

如何编写突变以创建新客户

标签: graphqlstrapimutation

解决方案


该错误表明在突变的查询组件中缺少对客户类型的引用。

另外,我相信您缺少“输入:”和“数据:”。我知道使用 createUser 时需要这些。

举例:

mutation createCustomer($username: String!, $email: String, $password: String) {
  createCustomer(
    input: { data: { username: $username, email: $email, password: $password } }
  ) {
    customer {
      id
      username
      email
      password
    }
  }
}

推荐阅读