Combining Express.js with Apollo
First things first; we need to install the Apollo and GraphQL dependencies:
npm install --save apollo-server-express graphql @graphql-tools/schema
Apollo offers an Express.js-specific package that integrates itself into the web server. There is also a standalone version without Express.js. Apollo allows you to use the available Express.js middleware. In some scenarios, you may need to offer non-GraphQL routes to proprietary clients who do not implement GraphQL or are not able to understand JSON responses. There are still reasons to offer some fallbacks to GraphQL. In those cases, you can rely on Express.js since you are already using it.
Create a separate folder for services. A service can be GraphQL or other routes:
mkdir src/server/services/ mkdir src/server/services/graphql
Create an index.js
file in the graphql
folder to act as the start point for our GraphQL service. It must handle multiple things for initialization. Let's...