A Bird's Eye View of GraphQL

Let us start looking at GraphQL. GraphQL is a query language and a server-side runtime for APIs developed by Meta (then facebook) in 2012 and made public in 2015.

GraphQL exists as a layer between the frontend and backend of the application. Client specifies the data they need in a particular format and the server responds with the required data.

GraphQL has a number of design principles:

  1. Product-Centric - Driven by the needs of the client.
  2. Hierarchical - Request is structured like the data it expects to be returned.
  3. Strong-Typing - Type of data returned by each field in its schema is explicitly defined acting as a contract between the client and the server.
  4. Client-specified Response - Unlike REST, in GraphQL the client specifies the shape and format of the data they need.
  5. Introspective - A GraphQL services’ type system is queryable by GraphQL language itself.
  6. Single-Endpoint - GraphQL APIs typically expose a single endpoint for all queries, mutations and subscriptions simplifying API management and number of network requests.?

Key Components of GraphQL APIs

  1. Schema - ?Schema specifies the types of data that can be queried and how they are related. This schema acts as a contract between the client and the server.
  2. Query Language - The query language of GraphQL allows clients to specify the exact data they need, along with the correct data types for each field.
  3. Resolvers - Functions that determine how to fetch the data associated with a particular field in the GraphQL schema.

A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type. E.g. The below is a GraphQL schema for a query called bookById that returns the details of a specified book.

type Query {
  bookById(id: ID): Book
}

type Book {
  id: ID
  name: String
  pageCount: Int
  author: Author
}

type Author {
  id: ID
  firstName: String
  lastName: String
}        

This is served by the following functions:

public Book bookById(@Argument String id) {

        return Book.getById(id);

    }

public Author author(Book book) {

        return Author.getById(book.authorId());

    }        

GraphQL supports mainly 2 types of operations:-

  1. Queries to fetch data

e.g.?

query hero{
	name
	appearsIn
	Id
}        

We should get something like this as a result

{
  "data": {
    "hero": {
      "name": "R2-D2",
      "appearsIn": [
        "NEWHOPE",
        "EMPIRE",
        "JEDI"
      ],
      "id": "2001"
    }
  }
}
        

We can add arguments to further filter things out.

  1. Mutations to edit server-side data.

e.g.

mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
  createReview(episode: $ep, review: $review) {
    stars
    commentary
  }
}        

This would result in the something like the below

{
  "ep": "JEDI",
  "review": {
    "stars": 5,
    "commentary": "This is a great movie!"
  }
}
        

Some Public GraphQL APIs

https://www.postman.com/devrel/workspace/graphql-examples/documentation/13191452-8892708a-95d0-40d2-902f-c3f22b7d4af0

#apis #fundamentals #graphql

Aniruddha Bhoot

Senior Consultant |Product Owner | PMP | Test Management | JIRA | Agile | Scrum |Azure CI/CD | PLM

10 个月

Very helpful!

回复
Krishnendu Halder

Software Engineer III at Walmart || Mentor at crio.do || Expertise in Software Quality Engineering || Building problem solving skills || Content creator @youtube/CodewithKrishnendu

10 个月

Thanks Ravi for this, this is something in my to-do list as well, soon, I will be preparing something on this too. GraphQL is something which we need to explore more. Good initiative??

Juni Kumari

Salesforce Trailblazer Community Speaker, Senior Architect

10 个月

Insightful!

回复

要查看或添加评论,请登录

Ravi Shankar的更多文章

  • Comparing Some Messaging Protocols

    Comparing Some Messaging Protocols

    Here we will compare some of the common messaging protocols. Compatibility Matrix with some common tools #fundamentals…

    1 条评论
  • An Introduction to Webhooks

    An Introduction to Webhooks

    Webhooks are a mechanism which enables communication between two applications or services where one service (client) is…

    2 条评论
  • MQTT - The messaging standard for IoT

    MQTT - The messaging standard for IoT

    MQTT is a lightweight messaging protocol. It was designed for communication with/between devices constrained by low…

社区洞察

其他会员也浏览了