Implementing a GraphQL API in a backend
We are now going to set up GraphQL in our existing blog application backend in addition to the REST API. Doing so will allow us to see how GraphQL compares to and differs from a REST API. Follow these steps to get started setting up GraphQL on the backend:
- Copy the existing
ch10
folder to a newch11
folder, as follows:$ cp -R ch10 ch11
- Open the
ch11
folder in VS Code. - First, let’s install a VS Code extension to add GraphQL language support. Go to the Extensions tab and search for the
GraphQL.vscode-graphql
extension developed by the GraphQL Foundation. Install the extension. - Next, install the
graphql
and@apollo/server
libraries in the backend using the following commands:$ cd backend/ $ npm install graphql@16.8.1 @apollo/server@4.10.0
Apollo Server is a production-ready GraphQL server implementation that supports multiple backend web frameworks, including Express.
- Create a new
backend/src/graphql/
folder. Inside it...