【问题标题】:GraphQL "Must provide a query string" graphql-tag react-apolloGraphQL“必须提供查询字符串” graphql-tag react-apollo
【发布时间】:2019-06-09 01:32:45
【问题描述】:

我正在使用 react-apollo 和 graphql-tag 但似乎有些失败。服务器在 express 和 postgraphile 上运行。

我做错了吗?


邮递员(工作):
{
    "query": "{\n  allPosts(first:10) {\n    nodes {\n    \tnodeId,\n      id,\n      content\n    }\n  }\n}"
}

反应(失败);

Status Code: 400 Bad Request Response: {"errors":[{"message":"Must provide a query string."}]}

代码:

export const allPostsQuery = gql`
  {
    allPosts(first: 10) {
      nodes {
        nodeId,
        id,
        content
      }
    }
  }
`;
 . . . 
<Query query={ allPostsQuery }> . . . </Query>

生成的请求负载看起来是这样的:

{"operationName":null,"variables":{},"query":"{\n  allPosts(first: 10) {\n    nodes {\n      nodeId\n      id\n      content\n      __typename\n    }\n    __typename\n  }\n}\n"}

我也尝试通过 Postman 运行这个有效载荷,它看起来还不错。

这是我的 Apollo 客户端配置:

return new ApolloClient({
    connectToDevTools: process.browser,
    ssrMode: !process.browser,
    link: new HttpLink({
      uri: 'http://127.0.0.1:8181/graphql',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json'
      }
    }),
    cache: new InMemoryCache().restore(initialState || {})
});

【问题讨论】:

  • 对我来说似乎是 mime 类型/标题不匹配。您可以记录标题,特别是邮递员和反应请求的content-type 吗? (从你的graphql端点内的服务器)
  • 谢谢,帮了大忙

标签: node.js express graphql react-apollo postgraphile


【解决方案1】:

感谢@azium 我想通了。再次非常感谢。 Content-Type 正在自我复制:

Request Headers -> content-type: application/json, application/json


因此,删除 headers 对象可以解决问题。
function create (initialState) {
  return new ApolloClient({
    connectToDevTools: process.browser,
    ssrMode: !process.browser,
    link: new HttpLink({
      uri: 'http://127.0.0.1:8181/graphql',
      credentials: 'include'
      // headers:
      //  'Content-Type': 'application/json' <-- remove the duplicate
      //}
    }),
    cache: new InMemoryCache().restore(initialState || {})
  });
}

【讨论】:

    猜你喜欢
    • 2019-06-02
    • 2018-01-13
    • 2018-12-30
    • 2016-08-10
    • 2020-04-23
    • 2020-01-26
    • 2022-01-18
    • 2020-07-29
    • 2021-12-15
    相关资源
    最近更新 更多