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

查看:326
本文介绍了为什么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而不是使用

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的事实有关,将功能,websocket(如果您选择启用)上传到应用程序,以及用于引擎运行状况检查的.众所周知的配置.根据该规范,知名人士必须扎根.此外,如果您使用的是订阅和网络套接字,它们会监听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天全站免登陆