简体   繁体   English

为什么我在 Apollo Client 中出现 400 错误

[英]Why am I getting a 400 error in mutation in Apollo Client

I'm getting error 400 when mutation signUp process.我在突变注册过程中收到错误 400。 I'm using React.JS for frontend and Apollo Client for consuming API. let's talk less and here's the code.我在前端使用 React.JS,在消费 API 时使用 Apollo Client。让我们少说话,这是代码。 SignUp.js注册.js

import React, { useEffect, useState } from 'react';
import { useMutation, useApolloClient, gql } from '@apollo/client';
import styled from 'styled-components';

import Button from '../components/Button';

const Wrapper = styled.div `
    border: 1px solid #f5f4f0;
    max-width: 500px;
    padding: 1em;
    margin: 0 auto;
`;

const Form = styled.form `
label,
input {
    display: block;
    line-height: 2em;
}

input {
    width: 100%;
    margin-bottom: 1em;
}

const SIGNUP_USER = gql `
  mutation signUp($email: String!, $username: String!, $password: String!) {
    signUp(email: $email, username: $username, password: $password)
  }
`;

const SignUp = (props) => {
const [values, setValues] = useState();

const onChange = (event) => {
    setValues({
        ...values,
        [event.target.name]: event.target.value
    })
}

useEffect(() => {
    document.title = 'Sign Up - Notedly';
});

const [signUp, { loading, error }] = useMutation(SIGNUP_USER, {
    onCompleted: data => {
        // console.log the JSON Web Token when the mutation is complete
        console.log(data.signUp);
    }
});

return (
    <Wrapper>
        <h2>Sign Up</h2>
        <Form
            onSubmit={event => {
            event.preventDefault();
            signUp({
                    variables: {
                      ...values
                    }
                  });
                }}
              >
            <label htmlFor="username">Username : </label>
            <input type="text" name="username" id="Username" placeholder="username" required/>
            <label htmlFor="email">Email : </label>
            <input type="email" name="email" id="email" placeholder="Email" required/>
            <label htmlFor="username">Password : </label>
            <input type="password" name="password" id="password" placeholder="Password" required/>
            <button type="submit">Submit</button>
        </Form>
    </Wrapper>
)
};

export default SignUp;

i got this error.我收到这个错误。

在此处输入图像描述

I don't have any idea, i've tried and read documentation still got no answer.我不知道,我试过并阅读文档仍然没有答案。 any help will be apreciated.任何帮助将不胜感激。 Thank you.谢谢你。

A 400 error is almost always a bad GraphQL request. 400 错误几乎总是错误的 GraphQL 请求。 In this case you're not returning anything from the mutation.在这种情况下,您不会从突变中返回任何东西。 Look at the definition of the signUp mutation to see what it returns and include at least one field from that.查看signUp突变的定义,看看它返回什么,并至少包含其中的一个字段。 Other possible errors include:其他可能的错误包括:

  • using the wrong types in the inputs在输入中使用错误的类型
  • missing some required inputs缺少一些必需的输入
  • including unexpected inputs or mis-spelling some of the input variables包括意外输入或拼写错误的一些输入变量

Use the playground to test your mutation before embedding it in code.在将突变嵌入代码之前,使用 playground 测试它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM