admin管理员组文章数量:1400776
I'm kind new to Apollo Client
. Let's talk less and show the code. I have a server that is running and I'm trying to make a mutation using react.
mutation in the server.js
const RootMutationType = new GraphQLObjectType({
name: "RootMutation",
description: "Root Mutation Type.",
fields: () => ({
addBook: {
type: BookType,
args: {
authorId: { type: GraphQLID },
name: { type: GraphQLString },
},
resolve: async (parent, args) => {
const book = new Book({
name: args.name,
authorId: args.authorId,
});
return await book.save();
},
},
......
// other crazy stuff here
Using graphiql
Im able to add a book which means the server is running fine. But on the react client when i try to add a book i get the error. Here is what i have.
mutations.js
import { gql } from "@apollo/client";
const ADD_BOOK_MUTATION = gql`
mutation addBook($authorId: Integer!, $name: String!) {
addBook(authorId: $authorId, name: $name) {
name
id
}
}
`;
export { ADD_BOOK_MUTATION };
Form.jsx
import React, { useEffect, useRef } from "react";
import { useMutation } from "@apollo/client";
import { ADD_BOOK_MUTATION } from "./mutations";
const Index = () => {
const [addBook, { data, error }] = useMutation(ADD_BOOK_MUTATION);
const idRef = useRef(null);
const nameRef = useRef(null);
useEffect(() => {
if (error) {
console.error(error);
} else {
console.log(data);
}
}, [data, error]);
const addBookHandler = (e) => {
e.preventDefault();
addBook({
variables: {
authorId: idRef.current.value,
name: nameRef.current.value,
},
});
};
return (
<form className="form">
<input ref={idRef} type="number" placeholder="author id" />
<input ref={nameRef} type="text" placeholder="book name" />
<button onClick={addBookHandler}>addBook</button>
</form>
);
};
export default Index;
Can someone tell me where am i wrong here!! An help input will be appreciated folks!!
I'm kind new to Apollo Client
. Let's talk less and show the code. I have a server that is running and I'm trying to make a mutation using react.
mutation in the server.js
const RootMutationType = new GraphQLObjectType({
name: "RootMutation",
description: "Root Mutation Type.",
fields: () => ({
addBook: {
type: BookType,
args: {
authorId: { type: GraphQLID },
name: { type: GraphQLString },
},
resolve: async (parent, args) => {
const book = new Book({
name: args.name,
authorId: args.authorId,
});
return await book.save();
},
},
......
// other crazy stuff here
Using graphiql
Im able to add a book which means the server is running fine. But on the react client when i try to add a book i get the error. Here is what i have.
mutations.js
import { gql } from "@apollo/client";
const ADD_BOOK_MUTATION = gql`
mutation addBook($authorId: Integer!, $name: String!) {
addBook(authorId: $authorId, name: $name) {
name
id
}
}
`;
export { ADD_BOOK_MUTATION };
Form.jsx
import React, { useEffect, useRef } from "react";
import { useMutation } from "@apollo/client";
import { ADD_BOOK_MUTATION } from "./mutations";
const Index = () => {
const [addBook, { data, error }] = useMutation(ADD_BOOK_MUTATION);
const idRef = useRef(null);
const nameRef = useRef(null);
useEffect(() => {
if (error) {
console.error(error);
} else {
console.log(data);
}
}, [data, error]);
const addBookHandler = (e) => {
e.preventDefault();
addBook({
variables: {
authorId: idRef.current.value,
name: nameRef.current.value,
},
});
};
return (
<form className="form">
<input ref={idRef} type="number" placeholder="author id" />
<input ref={nameRef} type="text" placeholder="book name" />
<button onClick={addBookHandler}>addBook</button>
</form>
);
};
export default Index;
Can someone tell me where am i wrong here!! An help input will be appreciated folks!!
Share Improve this question asked Jun 21, 2021 at 11:34 crispengaricrispengari 9,3838 gold badges58 silver badges67 bronze badges3 Answers
Reset to default 4Generally these type of error arise because of mismatch between types of mutation parameters and the user defined GraphQL mutation query parameters.
For example, the mutation query I wrote was missing a !
whereas the type definition for the mutation had !
.
A mon and easy way to check errors is to install the apollo-client plugin to your browser.
I just figure out myself after some time. $authorId: Integer!
should be $authorId:ID
.
New mutation
import { gql } from "@apollo/client";
const ADD_BOOK_MUTATION = gql`
mutation addBook($authorId: ID, $name: String!) {
addBook(authorId: $authorId, name: $name) {
name
id
}
}
`;
export { ADD_BOOK_MUTATION };
本文标签:
版权声明:本文标题:javascript - Unhandled Rejection (Error): Response not successful: Received status code 400 new ApolloError - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744264738a2597894.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论