[GraphQL错误]:消息:类型为“ rootMutation”的字段“ createUser”上的未知参数“电子邮件”。,位置:[对象对象],路径:未定义

时间:2019-09-08 10:58:55

标签: reactjs graphql

我在这里使用express graphql和apollo客户端。我遇到了突变问题

............................................... ................................................... ......................

我的错误:

  

[GraphQL错误]:消息:字段上的未知参数“电子邮件”   类型“ rootMutation”的“ createUser”。,位置:[对象对象],路径:   未定义

当我单击注册按钮时,这给我一个错误。 我的组件:

import { useMutation } from "@apollo/react-hooks";
import { gql } from "apollo-boost";

const CREATE_USER = gql`
  mutation createUser($email: String!, $password: String) {
    createUser(email: $email, password: $password) {
      _id
      email
    }
  }
`;

function Register() {
  const [email, setEmail] = React.useState("");
  const [password, setPassword] = React.useState("");
  const [createUser, { data }] = useMutation(CREATE_USER);

  return (
    <div class="row">
      <div class="input-field col s12">
        <input
          id="email"
          type="text"
          class="validate"
          onChange={e => setEmail(e.target.value)}
        />
        <label for="email"> Email </label>
      </div>
      <div class="input-field col s12">
        <input
          id="password"
          type="password"
          class="validate"
          onChange={e => setPassword(e.target.value)}
        />
        <label for="password"> Password</label>
      </div>
      <button
        className="btn"
        onClick={() => {
          createUser({
            variables: { $email: email, $password: password }
          });

          console.log(data);
        }}
      >
        Register
      </button>
    </div>
  );
}

export default Register;

我的架构:

``` type User {
  email: String!,
  password: String,
  createdEvents:[Event!]
  }

  type rootMutation {
           createUser(userInput: UserInput): User,

}```

1 个答案:

答案 0 :(得分:0)

您的突变收到一个UserInput参数,而不是直接接收电子邮件和密码。

const CREATE_USER = gql`
  mutation createUser($userInput: UserInput) {
    createUser(userInput: $userInput) {
      _id
      email
    }
  }
`;

...

  <button
    className="btn"
    onClick={() => {
      createUser({
        variables: { userInput: { email, password } }
      });

      console.log(data);
    }}
  >
    Register
  </button>