为什么 Apollo Server 不是 Express 的中间件,而是接受 Express 作为中间件的服务器? [英] Why isn't Apollo Server a middleware for Express, rather than being a server that accepts Express as a middleware?

查看:34
本文介绍了为什么 Apollo Server 不是 Express 的中间件,而是接受 Express 作为中间件的服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道这种看似尴尬的配置的原因是什么(来自 开始使用 Apollo 服务器),

I'm just wondering what's the reason for this seemingly awkward configuration (from Getting Started w/ Apollo Server),

const server = new ApolloServer({
  // These will be defined for both new or existing servers
  typeDefs,
  resolvers,
});

server.applyMiddleware({ app }); // app is from an existing express app

为什么我要调用 .applyMiddleware() 并将它提供给我的 app 而不是使用 app.use(),甚至从文档看来,Apollo 只回答 /graphql 上的请求 遵循 Express API 不是更好吗,

Why is that I'm calling .applyMiddleware() and feeding it my app rather than using app.use(), it even seems from the docs that Apollo is only answering requests on /graphql wouldn't it be better to follow the Express API of,

let apollo = require('apollo-server').ApolloMiddleware
app.use( '/graphql', apollo({ typeDefs, resolvers }) );

似乎 Apollo 正在颠倒 Express 的正常中间件流程?用 Apollo 的方式有什么好处?

It seems like Apollo is inverting the normal middleware flow of Express? What is the advantage of doing it the Apollo way?

推荐答案

我不和那些人一起工作,但根据那里的代码,我认为这与它添加了 body-parser 的事实有关,上传功能,websockets(如果你选择加入这个)到应用程序,以及引擎健康检查的众所周知的配置.根据该规范,众所周知的必须是根.此外,如果您使用订阅和 websockets,它们会侦听 httpServer.once('listening'),因此它们必须有权访问您的应用程序.如果他们只是给你一个你可以挂载的,你就不会app.listen,因为你自己app.listen.

I don't work with those guys, but based off the code that is there, I assume it has to do with the fact that it adds body-parser, upload capability, websockets (if you opt into this one) to the app, as well as a .well-known configuration for engine's health checks. The well-known would have to be at root according to that spec. Additionally, if you're using the subscriptions and websockets, they listen for httpServer.once('listening'), so they have to have access to your app. If they simply handed you one that you could mount, you wouldn't app.listen on it, since you'd app.listen on your own instead.

就内部而言,这至少是我所期望的.

Just from the internals, this is what I would expect, at least.

这篇关于为什么 Apollo Server 不是 Express 的中间件,而是接受 Express 作为中间件的服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆