我想要开发一个应用程序,并且建议使用GraphQl作为API,
但我不确定应该选择哪个平台以及它们之间的区别。
apollo server vs express-graphql
我也需要在这个项目中使用TypeScript。 欢迎任何好的想法。
我想要开发一个应用程序,并且建议使用GraphQl作为API,
但我不确定应该选择哪个平台以及它们之间的区别。
apollo server vs express-graphql
我也需要在这个项目中使用TypeScript。 欢迎任何好的想法。
还有一个由社区维护的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 thegraphql-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 theapplication/graphql
Content-Type for requests, which is an alternative toapplication/json
request with only the query part sent as text. 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. 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); ```
Express-GraphQL
是一种中间件,可以快速设置GraphQL服务器,无论是使用Express还是任何支持中间件的Web框架。
Apollo-server
是一个包,它将存在的node服务器上的GraphQL查询解析。 (与express-graphql非常相似)您可以与express,Koa等一起使用它。
我的建议是使用Graphql-yoga,因为它是使用apollo-server和express-graphql构建的。而且它由Prisma Team
构建和维护。
const { ApolloServer } = require('apollo-server-express')
const graphqlServer = new ApolloServer({
schema,
introspection: true,
playground: true,
})
graphqlServer.applyMiddleware({
app
})