0
0
GraphQLquery~5 mins

Why GraphQL exists

Choose your learning style9 modes available
Introduction

GraphQL exists to make getting data from servers easier and faster. It helps clients ask for exactly what they need, nothing more or less.

When you want to get only specific pieces of data from a server without extra information.
When your app needs to get data from many sources in one request.
When you want to avoid asking the server multiple times for related data.
When your app changes often and you want flexible data requests without changing the server.
When you want to improve performance by reducing the amount of data sent over the network.
Syntax
GraphQL
query {
  fieldName {
    subField1
    subField2
  }
}

This is a simple GraphQL query asking for specific fields.

You list exactly what data you want inside the curly braces.

Examples
Get the user's name and email only.
GraphQL
query {
  user {
    name
    email
  }
}
Get posts with their titles and the author's name for each post.
GraphQL
query {
  posts {
    title
    author {
      name
    }
  }
}
Get the name and price of a product with a specific ID.
GraphQL
query {
  product(id: "123") {
    name
    price
  }
}
Sample Program

This query asks for the user with ID 1, requesting their name, email, and titles of their posts.

GraphQL
# Sample GraphQL query to get user info
query {
  user(id: "1") {
    name
    email
    posts {
      title
    }
  }
}
OutputSuccess
Important Notes

GraphQL reduces over-fetching and under-fetching of data.

It allows clients to get all needed data in one request.

It requires a GraphQL server that understands the schema and queries.

Summary

GraphQL helps clients ask for exactly the data they want.

It improves app performance by reducing unnecessary data transfer.

It makes APIs flexible and easier to evolve over time.