【问题标题】:[GraphQL error]: Message: Unknown argument "email" on field "createUser" of type "rootMutation"., Location: [object Object], Path: undefined[GraphQL 错误]:消息:“rootMutation”类型的字段“createUser”上的未知参数“email”。位置:[object Object],路径:未定义
【发布时间】:2020-01-10 11:17:59
【问题描述】:

我在这里使用 express graphql 和 apollo 客户端。我面临一个突变问题

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

我的错误:

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

当我点击注册按钮时,它给了我一个错误。 我的组件:

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,

}```

【问题讨论】:

  • 你能在你的架构中显示你的 UserInput 类型吗?
  • 为什么是负面的?...

标签: reactjs graphql


【解决方案1】:

您的变异接收一个 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>

【讨论】:

    猜你喜欢
    • 2019-05-29
    • 2019-06-29
    • 2020-09-15
    • 2019-01-15
    • 2020-09-08
    • 2020-01-16
    • 2018-11-27
    • 2017-02-03
    • 2020-05-18
    相关资源
    最近更新 更多