Introduction about GraphQL in Magento 2.x – Part1

Introduction :

  • GraphQL is a query language for your API.
  • GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type.
  • New API standard that was invented and open sourced by Facebook
  • A runtime for fulfilling those queries with your existing data.
  • It is a single endpoint and responds to queries
  • Better than REST
  • We can create own schema with GraphQL.

Why GraphQL:

  1. Comfortable data loading in mobile device. Mobile user are increased now a days. GraphQL is the best solution to fulfill the mobile users data loading on there device.
  2. Facebook users GraphQL since 2012 in their native mobile apps.
  3. It can be used with any programming language and framework.
  4. No more Over-fetching & Under-fetching.
    Over-fetching : Downloading Unnecessary/Unwanted data
    Under-fetching : Sending Multiple (n number of) request when on endpoint doesn’t return right information or data.
  5. It is a common interface between client and server for fetching the data.

Example : To get the logged-in User Name with GraphQL

type Query {
me: User
}

type User {
id: ID
name: String
}

Along with functions for each field on each type:

function Query_me(request) {
return request.auth.user;
}

function User_name(user) {
return user.getName();
}

Sample query:

{
me {
name
}
}

JSON Result :

{
“me”: {
“name”: “Mr Jute”
}
}

For more information refer :
https://graphql.org/learn/
https://www.howtographql.com

Leave a Reply