GraphQL“必须提供查询字符串” graphql-tag react-apollo

时间:2019-01-15 02:47:49

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

我正在使用react-apollo和graphql-tag,但似乎失败了。服务器正在快递和邮递上运行。

我做错什么了吗?


邮递员(工作中):

{
    "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 || {})
});

1 个答案:

答案 0 :(得分:0)

感谢@azium,我知道了。再次非常感谢。 Content-Type正在复制自身:

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


因此,删除标头对象可以解决此问题。

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 || {})
  });
}