Apollo Server 和 Express-GraphQL 有什么区别?

31

我想要开发一个应用程序,并且建议使用GraphQl作为API,

但我不确定应该选择哪个平台以及它们之间的区别。

apollo server vs express-graphql

我也需要在这个项目中使用TypeScript。 欢迎任何好的想法。

3个回答

10
以下是来自apollo-server README的现已删除的部分,比较apollo-serverexpress-graphql
请注意,其中一些论点不再适用,例如express-grapqhl现在是用TypeScript编写的。因此,从README中删除了这个部分。
一个观察是apollo-server太臃肿了,而且正在慢慢显示出缺乏维护的问题。如果我今天要选择一个,我会选择express-graphql。但这是个人偏好,您应该自行进行尽职调查。

还有一个由社区维护的Koa版本的express-graphql,称为koa-graphql。使用express-graphql或koa-graphql之一,再加上类似envelop的东西,您可以以更模块化的方式实现Apollo“生态系统”提供的一切,甚至更多。

Comparison with express-graphql

Both Apollo Server and express-graphql are GraphQL servers for Node.js, built on top of the graphql-js reference implementation, but there are a few key differences:

  • express-graphql works with Express and Connect, Apollo Server supports Express, Connect, Hapi, Koa and Restify.
  • Compared to express-graphql, Apollo Server has a simpler interface and supports exactly one way of passing queries.
  • Apollo Server separates serving GraphiQL (an in-browser IDE for exploring GraphQL) from responding to GraphQL requests.
  • express-graphql contains code for parsing HTTP request bodies, Apollo Server leaves that to standard packages like body-parser.
  • Apollo Server includes an OperationStore to easily manage whitelisting.
  • Apollo Server is built with TypeScript.

application/graphql requests

express-graphql supports the application/graphql Content-Type for requests, which is an alternative to application/json request with only the query part sent as text. In the same way that we use bodyParser.json to parse application/json requests for apollo-server, we can use bodyParser.text plus one extra step in order to also parse application/graphql requests. Here's an example for 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); ```

5

Express-GraphQL 是一种中间件,可以快速设置GraphQL服务器,无论是使用Express还是任何支持中间件的Web框架。

Apollo-server 是一个包,它将存在的node服务器上的GraphQL查询解析。 (与express-graphql非常相似)您可以与express,Koa等一起使用它。

我的建议是使用Graphql-yoga,因为它是使用apollo-server和express-graphql构建的。而且它由Prisma Team构建和维护。


4
Express-GraphQL不是一个修改过的Express服务器来处理GraphQL请求。它是一个中间件,可以快速设置GraphQL服务器,无论是使用Express还是任何支持中间件的Web框架。参考链接:https://www.npmjs.com/package/express-graphql - Plaul
更新@Plaul。 - Blessing
1
graphql-yoga现在看起来已经过时了。 https://github.com/dotansimha/graphql-yoga/issues/629 - Romain TAILLANDIER

2
我建议使用apollo-server-express而不是express-graphql。它们非常相似,但在我看来,apollo-server-express具有更多功能,同时具有更简单和更清晰的API。
对我来说,apollo-server-express最大的改进是playground:https://github.com/prisma/graphql-playground playground比express-graphql的graphiql更好,原因有几个,其中一个重要原因是它允许您在请求中放置HTTP标头,这更适合处理会话。 www.graphqlbin.com将允许您在任何没有cors的端点上使用playground。如果您有cors,则需要直接从服务器运行playground。
以下是一个示例代码,可帮助您入门:
const { ApolloServer } = require('apollo-server-express')
const graphqlServer = new ApolloServer({
  schema,
  introspection: true,
  playground: true,
})
graphqlServer.applyMiddleware({
  app
})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接