Apollo Server 3 Express中间件无法与REECT SSR路由一起使用

发布于 2025-02-09 08:21:01 字数 769 浏览 1 评论 0原文

我最近从apollo-server-express升级:2.25.4 :3.9.0

我不需要很多迁移,但是最初的任何请求都可以/graphql不是由Apollo服务器中间件回答,而是由React SSR中间件回答(@apollo/client/react/react/ssr:3.6.8)。

我必须手动将以下几行添加到我的SSR中间件中,以便要通过的请求:

+ if (req.url == "/graphql") {
+   return next();
+ }

const client = new ApolloClient(...);

renderToStringWithData(...)
    .then((content) => {
        const state = client.extract();
        const html = <Html content={content} state={JSON.stringify(state)} />;
        res.send(`<!doctype html>\n${ReactDOM.renderToStaticMarkup(html)}`);
    })
    .catch(...);

现在,这有效;但是我想知道为什么有必要?我梳理了ChangElogs,但无法在Apollo-Server-express中找到任何更改,以了解为什么我现在需要添加它?

I recently upgrade from apollo-server-express:2.25.4 to :3.9.0

I didn't need many migrations, however initially any request to /graphql were answered not by the apollo server middleware, but instead by the React SSR middleware I also used (@apollo/client/react/ssr:3.6.8).

I had to manually add the following lines to my SSR middleware in order for the requests to go through:

+ if (req.url == "/graphql") {
+   return next();
+ }

const client = new ApolloClient(...);

renderToStringWithData(...)
    .then((content) => {
        const state = client.extract();
        const html = <Html content={content} state={JSON.stringify(state)} />;
        res.send(`<!doctype html>\n${ReactDOM.renderToStaticMarkup(html)}`);
    })
    .catch(...);

Now, this works; however I want to know why it was necessary? I combed through the changelogs but wasn't able to find any change in apollo-server-express as to why I need to add this now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紧拥背影 2025-02-16 08:21:01

事实证明,要归咎于启动阿波罗服务器的异步方法的更改。

在Apollo 3中,您需要首先调用start()然后应用实际中间件:

// app: ExpressApplication
const apolloServer = new ApolloServer(...);
apolloServer.start().then(() => server.applyMiddleware({ app }));

因为这是一种异步方法,所以我应用的SSR中间件实际上是在Apollo之前订购的中间件

createApolloServer(app); // see above
app.use(renderDom); // see question

以及这样的SSR是在Apollo服务器之前执行的,要求我添加IF-CARE,以防止SSR呈现/graphQl路由。

当然,另一个解决方案是正确等待异步函数,然后随后应用SSR中间件:

createApolloServer(app).then(() => {
    app.use(renderDom);
    app.listen(...);
});

Turns out, the change to an async method for starting the apollo server was to blame.

In Apollo 3 you need to first call start() and then apply the actual middleware:

// app: ExpressApplication
const apolloServer = new ApolloServer(...);
apolloServer.start().then(() => server.applyMiddleware({ app }));

As this is an asynchronous method, the SSR middleware I applied was actually ordered before the Apollo middleware

createApolloServer(app); // see above
app.use(renderDom); // see question

And as such the SSR was executed before the Apollo Server, requiring me to add the if-clause to prevent the /graphql route being rendered by SSR.

Another fix of course is to properly wait for the async function and then apply the SSR middleware afterwards:

createApolloServer(app).then(() => {
    app.use(renderDom);
    app.listen(...);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文