My GraphQL Study
October 16, 2021
I haven't finished my Angular tutorial yet but I'm going to have to shift gears here and start learning GraphQL. It's been said that it's a great alternative to Rest API as you only need one end point to get data for a group of tables in your database. You implement GraphQL in the back-end of your project. I haven't started coding in it but what I always do is watch a lot of YouTube videos and documents to narrow down the resource(s) that I need to look back to. Here are my notes.
PROS for GraphQL from watching Pedro Youtube:
PROS for GraphQL from watching Pedro Youtube:
- You only need one end point to get data from a group of tables which is /graphql
- You can query to receive only the data you need.
- GraphQL is strongly-typed.
- You can utilize the GraphiQL to test a query in the browser and try the end point
- If using PostgreSQL, you will need to use pg-promise library so there is familiarity if you're used to PostgreSQL.
- You won't need a body parser?!
- Gets rid of REST
- You don't have to make many calls. Just 1 for data you're looking for even if it's nested information
- You'll be required to install a few additional libraries and add more folders and files like schema or import schema from the graphql library
Differences for GraphQL versus Rest API that stood out to me:
- Instead of using req.body or req.send, you use resolve. You send your queries with resolve and don't need to create database adapters except to create your table.
- npm install express-graphql graphql
- This doesn't work anymore: const expressGraphQL = require('express-graphql'); Replace it with const expressGraphQL = require('express-graphql').graphqlHTTP
- This is an example of a query using GraphiQL:
query { person(id: "1") { id firstname lastname emails { id email primary } } }
This a great resource to read on starting up the files: https://medium.com/codingthesmartway-com-blog/creating-a-graphql-server-with-node-js-and-express-f6dddc5320e1
If your stack includes PostgreSQL, this is a great GraphQL article: https://blog.cloudboost.io/postgresql-and-graphql-2da30c6cde26
Journal:
October 16, 2021 at 2:21PM: I spent a good couple hours looking at 3 Youtube videos about GraphQL and a few readings from online so I'm ready to create a new project and utilize GraphQL for the first time. I'll build my server with Express first.
If your stack includes PostgreSQL, this is a great GraphQL article: https://blog.cloudboost.io/postgresql-and-graphql-2da30c6cde26
Journal:
October 16, 2021 at 2:21PM: I spent a good couple hours looking at 3 Youtube videos about GraphQL and a few readings from online so I'm ready to create a new project and utilize GraphQL for the first time. I'll build my server with Express first.
Posted by Ninja Space Content. Posted In : GraphQL