Apollo Client 400错误-bundle.esm.js:60未捕获(承诺)错误:网络错误:响应失败:收到状态码400

时间:2019-05-09 18:51:48

标签: reactjs graphql apollo apollo-client

我试图在我的react应用程序中连接到apollo客户端,并且在运行查询时出现错误400。如果我在邮递员中使用相同的URL进行相同的查询,则效果很好。谢谢你们

我已经尝试使用apollo链接,但无法正常工作。

const client = new ApolloClient({

    uri: 'http://localhost:3000/graphql',
    onError: (e) => { console.log(e) },
})

client.query({

    query: gql`
    {
        contracts{
            id
        }
    } `
}).then(data => console.log(`Query Result ${data}`))

浏览器错误!

{operation: {…}, networkError: ServerError: Response not successful: Received status code 400
    at throwServerError (http://loca…, graphQLErrors: undefined, forward: ƒ}forward: ƒ (op)graphQLErrors: undefinednetworkError: ServerError: Response not successful: Received status code 400
    at throwServerError (http://localhost:3000/static/js/bundle.js:37934:17)
    at http://localhost:3000/static/js/bundle.js:37959:13operation: {variables: {…}, extensions: {…}, operationName: null, query: {…}, setContext: ƒ, …}__proto__: Object
bundle.esm.js:60 Uncaught (in promise) Error: Network error: Response not successful: Received status code 400
    at new ApolloError (bundle.esm.js:60)
    at QueryManager.<anonymous> (bundle.esm.js:1184)
    at step (tslib.es6.js:97)
    at Object.next (tslib.es6.js:78)
    at tslib.es6.js:71
    at new Promise (<anonymous>)
    at __awaiter (tslib.es6.js:67)
    at bundle.esm.js:1147
    at bundle.esm.js:1627
    at Array.forEach (<anonymous>)
    at bundle.esm.js:1626
    at Map.forEach (<anonymous>)
    at QueryManager../node_modules/apollo-client/bundle.esm.js.QueryManager.broadcastQueries (bundle.esm.js:1621)
    at bundle.esm.js:1127

我只是试图以控制台方式记录查询结果以进行测试

2 个答案:

答案 0 :(得分:0)

在uri的末尾添加一个正斜杠,就像http://localhost:3000/graphql/ 可能无关紧要,但有所作为。

答案 1 :(得分:0)

您必须提及提取策略。如果您使用的是当前的React钩子,请使用类似

import { useQuery, useMutation } from '@apollo/react-hooks';

const Login = () => {
    const [signInResponse] = useMutation(SIGNIN_USER);

    const onSubmit = async (e) => {
        const response = await signInResponse({
            variables:{email:'email',password:'password'},
            errorPolicy:"all"
        });
        console.log(response)
        // dispatch({type:"LOGIN",payload:response.data})
    };
}