Apollo Server 3 Express中间件无法与REECT SSR路由一起使用
我最近从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,要归咎于启动阿波罗服务器的异步方法的更改。
在Apollo 3中,您需要首先调用
start()
然后应用实际中间件:因为这是一种异步方法,所以我应用的SSR中间件实际上是在Apollo之前订购的中间件
以及这样的SSR是在Apollo服务器之前执行的,要求我添加IF-CARE,以防止SSR呈现
/graphQl
路由。当然,另一个解决方案是正确等待异步函数,然后随后应用SSR中间件:
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:As this is an asynchronous method, the SSR middleware I applied was actually ordered before the Apollo middleware
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: