简体   繁体   English

为什么我的 graphQL 突变创建用户出现错误 400

[英]Why do i get error 400 on my graphQL mutation create user

I'm working with Reactjs and GraphQL integration.我正在使用 Reactjs 和 GraphQL 集成。 i got a problem when i'm doing mutation for new user.我在为新用户做突变时遇到了问题。

Scenario: Creating user using Modals bootstrap.场景:使用 Modals bootstrap 创建用户。 when successful create new user, it shows alert or information success.当成功创建新用户时,它显示警报或信息成功。

Code: Here's my ModalCreate component code.代码:这是我的 ModalCreate 组件代码。

import React, { useState, useEffect } from 'react';
import { Button, Modal, Form } from "react-bootstrap";

const ModalCreate = (props) => {
    // state for check input component
    const [value, setValue] = useState({ 
                                    username: props.username || '', 
                                    email: props.email || '', 
                                    fullname: props.full_name || '',
                                    password: props.password || '', 
                                    phone: props.phone || '', 
                                    address: props.address || '', 
                                    groupid: props.group_id,
                                });
    
    const onChange = event => {
        setValue({
            ...value,
            [event.target.name]: event.target.value
        })
    }

    useEffect(() => {
        if (props.show) {
            document.body.classList.add("modal-open");
        }

        return () => {
            if (document.body.classList.contains("modal-open")) {
                document.body.classList.remove("modal-open");
            }
        };
  }, [props.show]);

  return (
    <Modal show={props.show}>
      <Modal.Header>
        <Modal.Title> <span>FORMULIR AKUN PENGGUNA</span> </Modal.Title>
      </Modal.Header>
      <Modal.Body>
        <Form onSubmit={e => { 
            e.preventDefault();
            props.action({
                variables: {
                    ...value
                }
            })
         }}>
            <Form.Group className="mb-3">
                <Form.Label>Role Akun</Form.Label>
                <Form.Select aria-label="pilih user role" value={value.groupid} onChange={onChange}>
                    <option value="superadmin">Super Admin</option>
                    <option value="admin">Admin</option>
                    <option value="admin_rj ">Admin RJ</option>
                </Form.Select>
            </Form.Group>
            <Form.Group className="mb-3">
                <Form.Label>Username</Form.Label>
                <Form.Control name="username" value={value.username} onChange={onChange}/>
            </Form.Group>
            <Form.Group className="mb-3">
                <Form.Label>Nama Lengkap</Form.Label>
                <Form.Control name="fullname" value={value.fullname} onChange={onChange}/>
            </Form.Group>
            <Form.Group className="mb-3">
                <Form.Label>Email</Form.Label>
                <Form.Control type="email" name="email" value={value.email} onChange={onChange}/>
            </Form.Group>
            <Form.Group className="mb-3">
                <Form.Label>Password</Form.Label>
                <Form.Control type="password" name="password" value={value.password} onChange={onChange}/>
            </Form.Group>
            <Form.Group className="mb-3">
                <Form.Label>Phone</Form.Label>
                <Form.Control type="text" name="phone" value={value.phone} onChange={onChange}/>
            </Form.Group>        
            <Button variant="secondary" type='submit'>
                Simpan
            </Button>
        </Form>
      </Modal.Body>
      <Modal.Footer>
        <Button variant="secondary" onClick={props.onClose}>
          Keluar
        </Button>
      </Modal.Footer>
    </Modal>
  );
};

export default ModalCreate;

and action/performing mutation in page call index.js:和页面调用 index.js 中的操作/执行突变:

import React, { useState } from 'react';
import { useQuery, useMutation } from '@apollo/client';
import { Container, Card, Button, InputGroup, FormControl, Form, Spinner } from 'react-bootstrap';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faSearch } from '@fortawesome/fontawesome-free-solid';

import CardInfo from '../../../component/PengaturanPengguna/CardInfo';
import TableUserInfo from '../../../component/PengaturanPengguna/Table';

import { Loading } from '../../../component/Common';
import ModalCreate from '../../../component/PengaturanPengguna/Modals/ModalCreate';

import { GET_ALL_USERS, GET_USER_BY_ID } from '../../../gql/query';
import { REGISTER_USER } from '../../../gql/mutation';

const SearchInput = () => {
    return (
        <InputGroup className="mb-3">
            <InputGroup.Text>
                <FontAwesomeIcon icon={faSearch} />
            </InputGroup.Text>
            <FormControl
                type="text"
                placeholder="Search..."
            />
      </InputGroup>
    )
}

const PengaturanPengguna = (props) => {
    // refetch and query data
    const { data: usersdata, loading: usersloading, error: userserror } = useQuery(GET_ALL_USERS);
    const { refetch, loading } = useQuery(GET_ALL_USERS);

     // show modals
     const [showModal, setShowModal] = useState(false);

    // mutation new register user
    const [registerUser, { loading: registerloading, error: registererror }] = useMutation(REGISTER_USER, {
        refetchQueries: [{ query: GET_USER_BY_ID }, { query: GET_ALL_USERS }],
        onCompleted: data => {
            console.log(data)
        },
        onError: err => {
            console.error(err);
        }
    }) ;

    const handleRefreshClick = () => {
        refetch();
    }

    const handleShowModal = () => setShowModal(true);
    const handleCloseModal = () => setShowModal(false);

    if (usersloading || registerloading) return <Loading/>
    if (userserror || registererror) return <p>Error!</p>

    return (
        <Container>
            <CardInfo/>
            <Card>
                <Card.Title>
                    <span className='base-md text-regular mt-2 std-txt-primary-200'>Data Pengguna Dashboard</span>
                </Card.Title>
                <Card.Body>
                    <div className='d-flex justify-content-between'>
                        <Form inline>
                            <SearchInput/>
                            <Button variant='secondary' onClick={handleRefreshClick} disabled={loading}>{loading ? ( <Spinner
                                as="span"
                                animation="border"
                                size="sm"
                                role="status"
                                aria-hidden="true"/> ) : 'Muat Ulang'}</Button>
                        </Form>
                        <div>
                            <Button variant='success' onClick={() => { setShowModal(true) }}>Daftar Akun</Button>
                        </div>
                    </div>
                    <TableUserInfo users={usersdata}/>
                </Card.Body>
            </Card>
            {
                showModal ? <ModalCreate show={handleShowModal} onClose={handleCloseModal} action={registerUser} /> : null
            }
        </Container>
    )
}

export default PengaturanPengguna;

and here's my mutation:这是我的突变:

const REGISTER_USER = gql`
    mutation($input: RegisterInput!) {
        register(input: $input) {
            username
            email
            full_name
            phone
            address
            group_id
        }
    }
`;

Error:错误:

I got this error我收到这个错误在此处输入图像描述

Also, Network Status Tabs:此外,网络状态选项卡:

在此处输入图像描述

I've been try any solution but it still not working, any help will be appreciated, thank you我一直在尝试任何解决方案,但它仍然无法正常工作,任何帮助将不胜感激,谢谢

If you are getting validation error from apollo for required fields then check the form fields may be name attribute is missing and value is not storing inside your state.如果您从 apollo 收到必填字段的验证错误,请检查表单字段是否缺少name属性,并且值未存储在您的 state 中。

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

相关问题 我从 apollo 客户端的变异怎么会得到 400 错误,而我可以在操场上做到这一点? - How could my mutation from apollo client get 400 error, while I can do that from playground? 为什么我在 Apollo Client 中出现 400 错误 - Why am I getting a 400 error in mutation in Apollo Client 为什么我的 GraphQL 查询会收到类型错误的 400 响应? - Why am I getting a 400 response with a type error for my GraphQL query? 执行突变时出现错误 400 graphql 和 apollo 客户端 - error 400 graphql and apollo client when performing mutation Graphql 突变返回 400 后错误,不确定如何解决 - Graphql mutation returning a Post 400 Error, not sure how to resolve 表达 http:500 错误在 graphql 突变上从阿波罗客户端在反应中创建用户 - express http: 500 error on graphql mutation to create user from apollo client in react 为什么我不能在GraphQL中提交查询中的数据并以突变形式获取数据? - Why can't I submit data in a query, and get data in a mutation in GraphQL? GraphQL验证错误突变 - GraphQL Validation Error on mutation 为什么我会收到 Redux 突变错误,我该如何避免它? - Why I get the Redux mutation Error and how can I avoid it? 如何在 Apollo / GraphQL 发生突变后更新缓存? - How do I update the cache after a mutation in Apollo / GraphQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM