[英]What is the difference between apollo server and express-graphql
I would like to build an application and its recommended to use GraphQl for API,我想构建一个应用程序,建议将 GraphQl 用于 API,
I am not sure which platform to select and what are the differences.我不确定 select 是哪个平台,有什么区别。
apollo server vs express-graphql
I need to use TypeScript for the project too.我也需要为项目使用 TypeScript。 Any good Idea would be appreciated.任何好主意将不胜感激。
Both are almost same.两者几乎相同。
Here are some trends, If you are intrested.这里有一些趋势,如果你感兴趣的话。
https://www.npmtrends.com/apollo-server-vs-express-graphql-vs-graphql-yoga-vs-prisma-vs-apollo-server-express https://www.npmtrends.com/apollo-server-vs-express-graphql-vs-graphql-yoga-vs-prisma-vs-apollo-server-express
Express-GraphQL
is a piece of middleware, to quickly setup a GraphQL Server, either with Express, or any web-framework that supports middleware. Express-GraphQL
是一个中间件,可以使用 Express 或任何支持中间件的 Web 框架快速设置 GraphQL 服务器。
Apollo-server is a package that will sit on an existing node server and parse the GraphQL queries. Apollo-server 是一个 package,它将位于现有节点服务器上并解析 GraphQL 查询。 (Very similar to express-graphql) You can use it with express, Koa etc. (与 express-graphql 非常相似)您可以将其与 express、Koa 等一起使用。
My recommendation is use Graphql-yoga as it's built with apollo-server and express-graphql.我的建议是使用 Graphql-yoga,因为它是用 apollo-server 和 express-graphql 构建的。 And it's built and maintained by the Prisma Team
.它由Prisma Team
构建和维护。
Below is the now deleted section from the apollo-server README comparing apollo-server to express-graphql .下面是 apollo-server README 中现已删除的部分,将apollo-server与express-graphql进行比较。
Note that some of these arguments do not apply anymore eg express-grapqhl is now written in TypeScript.请注意,其中一些 arguments 不再适用,例如 express-grapqhl 现在用 TypeScript 编写。 Hence the removal of this section from the README.因此从 README 中删除了这一部分。
One observation is that apollo-server is too bloated, and is slowly showing a lack of maintenance.一个观察结果是 apollo-server 过于臃肿,并且慢慢地显示出缺乏维护。 I would go with express-graphql instead if I were to pick one today.如果我今天要选择一个,我会用express-graphql代替 go。 But this is personal preference and you should do your own due diligence.但这是个人喜好,你应该做自己的尽职调查。
There's also a community-maintained Koa port of express-graphql, called koa-graphql .还有一个社区维护的 Koa 端口 express-graphql,称为koa-graphql 。 Using either express-graphql, or koa-graphql, combined with something like envelop , you can achieve everything, if not more, the Apollo "ecosystem" provides in a more modular manner.使用 express-graphql 或 koa-graphql,结合诸如envelop之类的东西,你可以实现一切,如果不是更多的话,Apollo“生态系统”以更加模块化的方式提供。
Comparison with
express-graphql
与express-graphql
比较Both Apollo Server and
express-graphql
are GraphQL servers for Node.js, built on top of thegraphql-js
reference implementation , but there are a few key differences: Apollo Server 和express-graphql
都是 Node.js 的 GraphQL 服务器,构建在graphql-js
参考实现之上,但有一些关键区别:
express-graphql
works with Express and Connect, Apollo Server supports Express, Connect, Hapi, Koa and Restify.express-graphql
与 Express 和 Connect 一起使用,Apollo Server 支持 Express、Connect、Hapi、Koa 和 Restify。- Compared to
express-graphql
, Apollo Server has a simpler interface and supports exactly one way of passing queries.与express-graphql
相比,Apollo Server 的接口更简单,并且只支持一种传递查询的方式。- Apollo Server separates serving GraphiQL (an in-browser IDE for exploring GraphQL) from responding to GraphQL requests. Apollo Server 将服务GraphiQL (用于探索 GraphQL 的浏览器内 IDE)与响应 GraphQL 请求分开。
express-graphql
contains code for parsing HTTP request bodies, Apollo Server leaves that to standard packages like body-parser.express-graphql
包含用于解析 HTTP 请求主体的代码,Apollo Server 将其留给像 body-parser 这样的标准包。- Apollo Server includes an
OperationStore
to easily manage whitelisting. Apollo Server 包含一个OperationStore
,可轻松管理白名单。- Apollo Server is built with TypeScript. Apollo 服务器使用 TypeScript 构建。
application/graphql requests应用程序/graphql 请求
express-graphql
supports theapplication/graphql
Content-Type for requests, which is an alternative toapplication/json
request with only the query part sent as text.express-graphql
支持请求的application/graphql
Content-Type,这是application/json
请求的替代方案,仅将查询部分作为文本发送。 In the same way that we usebodyParser.json
to parseapplication/json
requests for apollo-server, we can usebodyParser.text
plus one extra step in order to also parseapplication/graphql
requests.就像我们使用bodyParser.json
解析 apollo-server 的application/json
请求一样,我们可以使用bodyParser.text
加上一个额外的步骤来解析application/graphql
请求。 Here's an example for Express:这是 Express 的示例:'body-parser'; import { graphqlExpress } from 'apollo-server-express'; const myGraphQLSchema = //... define or import your schema here. const helperMiddleware = [ bodyParser,json(). bodyParser:text({ type, 'application/graphql' }), (req, res. next) => { if (req.is('application/graphql')) { req:body = { query. req;body }; } next(); } ]. express(),use('/graphql'. ..,helperMiddleware: graphqlExpress({ schema. myGraphQLSchema }));listen(3000); ```
I suggest using apollo-server-express over express-graphql.我建议在 express-graphql 上使用 apollo-server-express。 They are very similar, but apollo-server-express has more bells and whistles all while having a simpler and clearer API IMO.它们非常相似,但 apollo-server-express 有更多的花里胡哨,同时具有更简单和更清晰的 API IMO。
The biggest improvement in apollo-server-express, for me, is the playground: https://github.com/prisma/graphql-playground对我来说,apollo-server-express 的最大改进是操场: https://github.com/prisma/graphql-playground
The playground is better than express-graphql's graphiql for several reasons, but one big one is that it allows you to put HTTP headers in the request, which is more appropriate for handling session. Playground 优于 express-graphql 的 graphiql 有几个原因,但一大原因是它允许您将 HTTP 标头放入请求中,这更适合处理 session。
www.graphqlbin.com will allow you to use the playground on any endpoint which does not have cors. www.graphqlbin.com将允许您在没有 cors 的任何端点上使用操场。 If you have cors, then you will need to run playground directly from your server.如果你有 cors,那么你需要直接从你的服务器运行 Playground。
Here is a sample of code to get you started:以下是帮助您入门的代码示例:
const { ApolloServer } = require('apollo-server-express')
const graphqlServer = new ApolloServer({
schema,
introspection: true,
playground: true,
})
graphqlServer.applyMiddleware({
app
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.