the-road-to-graphql / fullstack-apollo-express-postgresql-boilerplate

💥 A sophisticated GraphQL with Apollo, Express and PostgreSQL boilerplate project.
https://roadtoreact.com
MIT License
1.2k stars 261 forks source link

client can't reach graphql server #16

Closed richardpatsch closed 5 years ago

richardpatsch commented 5 years ago

hi, after following the instructions in the readme I'm getting this:

screen shot 2018-09-16 at 15 20 25

The graphQL server is running and I can execute the queries as well, but somehow the client can not reach it. I tried it on 2 PCs already. Any ideas? thx in advance!

rwieruch commented 5 years ago

It should be fixed now. It was because there was no sample data and I never caught the case when there are no messages available. Can you try again to clone it?

richardpatsch commented 5 years ago

now I'm getting this:

screen shot 2018-09-20 at 17 49 26
Azrealy commented 5 years ago

@richardpatsch I met the same problem that 400 Bad Request from the Graphql server. As the GrapQL error message said 'Your session expired. Sign in again.', that should happened at the following code snippet in the server/src/index.js.

  if (token) {
    try {
      return await jwt.verify(token, process.env.SECRET);
    } catch (e) {
      throw new AuthenticationError(
        'Your session expired. Sign in again.',
      );
    }
  }

and the question is how the null token expected value from token = req.headers['x-token']; can involve the if statement. I try to console.log(req.headers) it and see the result is like { ..., 'x-token': 'null', ...}. You 'token' value is equal to String 'null' not null. I change the code in client/src/index.js and the fixed this issue.

const authLink = new ApolloLink((operation, forward) => {
  operation.setContext(({ headers = {} }) => {
    const token = localStorage.getItem('token')
    return {
    headers: {
      ...headers,
      'x-token': token ? token :  '',
    },
  }
});

Hope that can be help.

rwieruch commented 5 years ago

Oh, might be my bad implementation here. Thanks @Azrealy for figuring it out. Can you open a Pull Request for this project which fixes this? :)

Azrealy commented 5 years ago

@rwieruch Of course, and also I notice one more issue that happened at the following code snippet.

const errorLink = onError(({ graphQLErrors, networkError }) => {
  if (graphQLErrors) {
    graphQLErrors.forEach(({ message, locations, path }) => {
      console.log('GraphQL error', message);

      if (message === 'NOT_AUTHENTICATED') {
        signOut(client);
      }
    });
  }

here will cause a problem when the token expired. Because graphql server will throw the 'Your session expired. Sign in again.' error message, and this won't involve the if (message === 'NOT_AUTHENTICATED') statement, and I try to fix it through the following modification.

if (message) {
  const token = localStorage.getItem('token')
  if (token) {
     signOut(client);
  }
}

BTW, I create a new branch and fixed those issues but don't have permission to push it.

rwieruch commented 5 years ago

I think you have to go the official way of making a fork of the project and then opening up a Pull Request with it. Thanks for your help on this! 🚀

ACPK commented 5 years ago

@Azrealy - Did you make a PR? The authLink creates a syntax error.