简体   繁体   中英

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,

I am not sure which platform to select and what are the differences.

apollo server vs express-graphql

I need to use TypeScript for the project too. Any good Idea would be appreciated.

Express-GraphQL is a piece of middleware, to quickly setup a GraphQL Server, either with Express, or any web-framework that supports middleware.

Apollo-server is a package that will sit on an existing node server and parse the GraphQL queries. (Very similar to express-graphql) You can use it with express, Koa etc.

My recommendation is use Graphql-yoga as it's built with apollo-server and express-graphql. And it's built and maintained by the Prisma Team .

Below is the now deleted section from the apollo-server README comparing apollo-server to express-graphql .

Note that some of these arguments do not apply anymore eg express-grapqhl is now written in TypeScript. Hence the removal of this section from the README.

One observation is that apollo-server is too bloated, and is slowly showing a lack of maintenance. I would go with express-graphql instead if I were to pick one today. 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 . 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.

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); ```

I suggest using apollo-server-express over express-graphql. They are very similar, but apollo-server-express has more bells and whistles all while having a simpler and clearer API IMO.

The biggest improvement in apollo-server-express, for me, is the playground: 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.

www.graphqlbin.com will allow you to use the playground on any endpoint which does not have cors. If you have cors, then you will need to run playground directly from your server.

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
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM