Hello, Welcome to my blog!
Following up on my most recent post “RealWorld API – TypeScript, JWT, MongoDB, Express and Node.js”. I have modified the REST API I made into a Graph QL API.
What is GraphQL?
GraphQL is a query language for an API. GraphQL allows front-end applications to have control to specify the data received per request. All the data is available to you when you create a request, which means you can mix and match data per request. Drastically cutting down on the number of calls the front-end application has to make to the API.
My experience using GraphQL
I enjoyed using GraphQL, and I think it allows more flexibly than a REST API. There are many different flavours of GraphQL. I used Apollo Server Express. It was a bit of a steep learning curve, but once I built a few queries, I saw how easy to use it was.
Here is a list of some things that I learned about GraphQL. Examples can be found by viewing my code on git-hub.
- GraphQL is not a database, It replaces your API router. It can be used with any database.
- GraphQL uses a single POST route. On that POST route, you will make your Querys using the query language inside the HTTP body.
- Unless specified, GraphQL will look for queries under the Query type.
- The keyword mutation is used to tell GraphQL to change from looking at the default Query type to the Mutation Type.
- It is best to practise to put all data fetching queries under the Query type and all data writing queries under the Mutation Type.
- Postman works great to test your queries.
- You need to define your schema using the query language GQL.
- With Apollo authentication and error handling can be done when you create the Apollo server.
I definitely will use GraphQL for my future projects, and I like how flexible it is and that it is available for many different programming languages.
The code for this project can be found on my Git-Hub.