Ninja Space Content Blog

My GraphQL Study

Posted by Ninja Space Content on Saturday, October 16, 2021 Under: GraphQL
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:
  • You only need one end point to get data form 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
CONS for GraphQL:
  • 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.

In : GraphQL 


Tags: graphql cheat sheet  graphql pros and cons  graphiql 

About Ninja Space Content


Ninja Space Content I have been building simple websites on my own since 2008 and currently run several websites at the moment, including this one. I used to be an account manager for an affiliate/e-commerce company, helping affiliates grow their sales so I have some knowledge on the business side and client side of affiliate marketing. Most recently, I decided to complete a JavaScript coding bootcamp and graduated in Dec 2020.
Note: links to resources and promoting special deals may allow me to earn a small commission from each sale.
Work Station Accessories