【问题标题】:Apollo client not connecting to server when Redis cookie is set设置 Redis cookie 时,Apollo 客户端未连接到服务器
【发布时间】:2021-12-20 05:51:03
【问题描述】:

当我尝试登录设置 cookie 然后刷新页面时,我没有从 apollo 服务器得到任何响应,并且 graphql 客户端发出的每个请求都保持(待处理)状态。 删除 cookie 后,一切似乎都正常。我什至不确定如何调试它并且对后端有一点经验,所以任何建议都会有所帮助。

这是我从客户端设置连接的方式:

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

和服务器:


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();

【问题讨论】:

    标签: node.js redis graphql apollo apollo-server


    【解决方案1】:

    重启redis服务器后问题解决了

    【讨论】:

    猜你喜欢
    • 2018-07-29
    • 2020-06-12
    • 2021-02-05
    • 2013-12-26
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多