0

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 || {})
});
Dawid
  • 585
  • 5
  • 26
  • 1
    seems like a mime type / headers mismatch to me. can you log the headers, particularly the `content-type` of the request of both postman and react? (from your server inside the graphql endpoint) – azium Jan 15 '19 at 03:18
  • 1
    possibly helpful https://github.com/Yelp/yelp-fusion/issues/278 – azium Jan 15 '19 at 03:21
  • Thanks, it helped a lot – Dawid Jan 15 '19 at 12:04

1 Answers1

0

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 || {})
  });
}
Dawid
  • 585
  • 5
  • 26