Graphql错误,POSThttp://localhost:3000/graphql400(错误请求)



我正试图在代码中查找错误,以了解为什么createHTTP链接会出现此错误。这在graphql中工作得很好,但在用户保存选择时在应用程序上不起作用。有人能告诉我哪里出了问题吗?我提前为所有的代码道歉,我想确保我得到了所有可能相关的东西。我已经查了一段时间了。对于用户信息是保存到数据库中的,书籍则不是。谢谢

import {
ApolloClient,
InMemoryCache,
ApolloProvider,
createHttpLink,
} from "@apollo/client";
import { setContext } from "@apollo/client/link/context";
import SearchBooks from "./pages/SearchBooks";
import SavedBooks from "./pages/SavedBooks";
import Navbar from "./components/Navbar";
const httpLink = createHttpLink({
uri: "/graphql",
});
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem("id_token");
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
},
};
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
});

突变

import { gql } from "@apollo/client";
export const LOGIN_USER = gql`
mutation login($email: String!, $password: String!) {
login(email: $email, password: $password) {
token
user {
_id
username
}
}
}
`;
export const ADD_USER = gql`
mutation addUser($username: String!, $email: String!, $password: String!) {
addUser(username: $username, email: $email, password: $password) {
token
user {
_id
username
email
}
}
}
`;
export const SAVE_BOOK = gql`
mutation saveBook($input: SaveBookInput) {
saveBook(input: $input) {
username
_id
bookCount
savedBooks {
bookId
authors
title
image
link
description
}
}
}
`;

查询

import { gql } from "@apollo/client";
export const GET_ME = gql`
{
me {
_id
username
email
bookCount
savedBooks {
title
bookId
authors
description
image
link
}
}
}
`;

400错误通常是由gql中的语法错误引起的。不幸的是,这是一条不透明的错误消息,但我已经遇到过很多次了。

在这种情况下,您有:

export const GET_ME = gql`
{
me {
_id
username
etc...
}
}
`

其不是可执行查询。

我希望有更像的东西

export const GET_ME = gql`
query getMe {
me {
_id
username
etc...
}
}
`

假设在你的typeDef中有这样的东西:

type Query {
me: User
…other queries
}

最新更新