Mateusz
Mateusz

Reputation: 97

Apollo client not connecting to server when Redis cookie is set

When I try to login which sets a cookie and then I refresh the page I don't get any response from apollo server and every requests made by graphql client are kept (pending) status. After I remove a cookie, everything seems to work fine. I'm not even sure how can I debug this and have a little experience with backend so any advice would be helpful.

Here is how I setup connection from client:

const link = createHttpLink({
  uri: 'http://localhost:5000/graphql',
  credentials: 'include',
});

const apolloLink = ApolloLink.from([
  errorLink,
  link
]);

const apolloClient = new ApolloClient({
  cache: new InMemoryCache(),
  link: apolloLink,
});

And server:


useContainer(Container);

const establishDatabaseConnection = async (): Promise<void> => {
  try {
    await createDatabaseConnection();
  } catch (error) {
    console.error(error);
  }
};

const initExpressGraphql = async () => {
  const app = express();
  const redis = new Redis();
  const RedisStore = connectRedis(session);

  const corsOptions = {
    origin: 'http://localhost:3000',
    credentials: true,
  };

  const schema = await buildSchema({
    resolvers: RESOLVERS,
    container: Container,
  });

  const apolloServer = new ApolloServer({
    schema: schema as GraphQLSchema,
    context: ({ req, res }: any) => ({ req, res }),
    introspection: true,
    plugins: [
      ApolloServerLoaderPlugin({
        typeormGetConnection: getConnection, // for use with TypeORM
      }),
    ],
  });

  app.use(
    session({
      store: new RedisStore({
        client: redis as any,
      }),
      name: 'rds',
      secret: 'verysecretdata',
      resave: false,
      saveUninitialized: false,
      cookie: {
        httpOnly: true,
        secure: process.env.NODE_ENV === 'production',
        maxAge: 1000 * 60 * 60 * 24 * 7 * 365, // 7 years
        sameSite: 'lax',
      },
    })
  );

  apolloServer.applyMiddleware({
    app,
    cors: corsOptions
  }) 

  const PORT = process.env.PORT || 5000;
  app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
};

const startServer = async (): Promise<void> => {
  await establishDatabaseConnection();
  initExpressGraphql();
};

startServer();

Upvotes: 0

Views: 242

Answers (1)

Mateusz
Mateusz

Reputation: 97

My issue got solved after I restarted redis server

Upvotes: 0

Related Questions