#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
6 min read
•Question 5 of 28medium

What is GraphQL?

Understanding GraphQL query language.

What You'll Learn

  • What GraphQL is
  • GraphQL vs REST
  • Basic queries and mutations

What is GraphQL?

GraphQL is a query language for APIs that allows clients to request exactly the data they need.

GraphQL vs REST

FeatureRESTGraphQL
EndpointsMultipleSingle
Data fetchingFixed responseClient specifies
Over-fetchingCommonAvoided
Under-fetchingCommonAvoided

Basic Query

code.txtGRAPHQL
# Request only what you need
query {
  user(id: 1) {
    name
    email
    posts {
      title
    }
  }
}

# Response
{
  "data": {
    "user": {
      "name": "John",
      "email": "john@example.com",
      "posts": [{ "title": "My Post" }]
    }
  }
}

Mutation Example

code.txtGRAPHQL
mutation {
  createUser(input: { name: "John", email: "john@example.com" }) {
    id
    name
  }
}

Schema Definition

code.txtGRAPHQL
type User {
  id: ID!
  name: String!
  email: String!
  posts: [Post!]!
}

type Query {
  user(id: ID!): User
  users: [User!]!
}