Search code examples
node.jsexpressgraphqlreact-apollopostgraphile

GraphQL "Must provide a query string" graphql-tag react-apollo


I'm using react-apollo and graphql-tag but something seems to fail. The server is running on express & postgraphile.

Am I doing something wrong?


Postman (working):

{
    "query": "{\n  allPosts(first:10) {\n    nodes {\n    \tnodeId,\n      id,\n      content\n    }\n  }\n}"
}

React (failing);

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

Code:

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

The generated request payload looks accordingly:

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

I also tried to run this payload via Postman and it looked all right.

This is my Apollo client config:

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

Solution

  • Thanks to @azium i figured it out. Thanks a lot again. The Content-Type was duplicating itself:

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


    So, removing the headers object fixes the problem.

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