0

The same query in GraphiQL returns data, but returns null in React-Apollo.

Problem (so far) is only with this query. I've used a "Signup" mutation that returns data when tried from both React-Apollo and GraphiQL. This leads me to think the problem is with the query, but I can't find anything wrong.

I did notice that request from Apollo has slightly different Header info. I'm not sure if that's the issue and, if so, how to change it.

GraphiQL vs Apollo request/response

Snippet in class component:

render() {
    return (
        <Query query={currentUser} >
            {({ loading, error, data }) => {
            if (loading) return "Loading...";
            if (error) return `Error! ${error.message}`;


            return (
                <UsersContext.Provider
                    value={{ ...this.state }}
                    >
                {console.log('data', data)}
                    {this.props.children}
                </UsersContext.Provider >
            )
        }}
        </Query>
    )
}

I also tried calling query by export default graphql(currentUser)(UserProvider) but it failed the same.

Query code:

import gql from 'graphql-tag';

const currentUser =  gql`
  {
    user {
        id
        email
        username
      }
  }
`;

export default currentUser;

Query is exact same as in GraphiQL: Query and response in GraphiQL

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • 1
    If the data is coming back as null in the server response, then it means your request was successful, but the field itself resolved to null. How is the server determining what user to return? If you're using cookie-based sessions, is your HttpLink configured correctly as outlined [here](https://www.apollographql.com/docs/react/recipes/authentication.html#Cookie) and is your server configured to receive credentials? – Daniel Rearden Jan 24 '19 at 15:57
  • @DanielRearden, thank you! Looks like my server was not configured to receive the credentials. Although, I'm still not sure why GraphiQL would be successful. – Alex Miller Jan 25 '19 at 12:09

0 Answers0