Skip to content
Santekno.com | Tech Tutorials and Trends
EN
📖 0%
16 Jul 2025 · 6 min read ·Article 16 / 125
Go

16 Getting to Know GraphQL Queries and Their Standard Format

IH
Ihsan Arif
Writer at Santekno · Backend Engineer

title: “16 Getting to Know GraphQL Queries and Their Standard Format” date: 2024-06-18 author: “Engineer Dev” tags: [GraphQL, Backend, API, Query Language]

Introduction

As a backend engineer, I run into questions about GraphQL fairly often. But even though it is widely used these days, understanding of queries and their standard format is sometimes still lacking. This article breaks down 16 important aspects of getting to know GraphQL Queries and their canonical format, complete with practical examples, simulations, and a bit of a flow diagram to make it easier to digest.


1. What Is GraphQL?

GraphQL is a query language for APIs created by Facebook in 2012 and released as open source in 2015. Unlike REST, GraphQL lets clients specify exactly what data they need.

2. Query Syntax Basics

Queries in GraphQL use a declarative form rather than an imperative one. For example, to fetch a user:

graphql
1{
2  user(id: "42") {
3    id
4    name
5    email
6  }
7}

3. Standard Query Types

At its core, GraphQL recognizes three main request types:

TypeDescription
queryRetrieves data
mutationModifies data
subscriptionListens for data changes in real time

Typically, query is used for read-only requests.

4. Basic Query Structure

The standard query format structure:

graphql
1{
2  <entity>(<arguments>) {
3    <fields>
4    <sub-entity> {
5      <fields>
6    }
7  }
8}

For example:

graphql
1{
2  posts(limit: 2) {
3    id
4    title
5    author {
6      name
7    }
8  }
9}

5. Queries with Arguments

Arguments are applied after the entity. Here is an example with filtering and pagination:

graphql
1{
2  posts(title: "GraphQL", limit: 2, offset: 0) {
3    id
4    title
5  }
6}

6. Aliases in Queries

Aliases are used when you need two different sets of data from the same field:

graphql
1{
2  firstUser: user(id: "1") {
3    name
4  }
5  secondUser: user(id: "2") {
6    name
7  }
8}

7. Variables in Queries

To make queries more dynamic and secure against injection, use variables:

Query:

graphql
1query getUser($userId: ID!) {
2  user(id: $userId) {
3    id
4    name
5  }
6}

Variables (JSON):

json
1{
2  "userId": "5"
3}

8. Nested Queries

GraphQL supports nested requests, for example a user along with their posts:

graphql
 1{
 2  user(id: "5") {
 3    id
 4    name
 5    posts {
 6      title
 7      content
 8    }
 9  }
10}

9. Fragments

To make things reusable, use fragments. For example:

graphql
 1fragment userFragment on User {
 2  id
 3  name
 4  email
 5}
 6
 7{
 8  user(id: "10") {
 9    ...userFragment
10    posts {
11      title
12    }
13  }
14}

10. Inline Fragments

For schemas with union types:

graphql
 1{
 2  search(text: "foo") {
 3    ... on User {
 4      name
 5      email
 6    }
 7    ... on Post {
 8      title
 9      content
10    }
11  }
12}

11. Directives

Directives add logic to a query, such as @skip and @include:

graphql
1query getUser($includeEmail: Boolean!) {
2  user(id: "7") {
3    name
4    email @include(if: $includeEmail)
5  }
6}

12. Handling Errors and Responses

A standard GraphQL response:

json
 1{
 2  "data": {/* ... */},
 3  "errors": [
 4    {
 5      "message": "User not found",
 6      "locations": [{"line":3, "column":5}],
 7      "path": ["user"]
 8    }
 9  ]
10}

13. Mutations: Updating and Adding Data

Mutations to update/add data:

graphql
1mutation {
2  addPost(title: "Format GraphQL", content: "Penjelasan.") {
3    id
4    title
5  }
6}

14. Subscriptions

For real-time updates:

graphql
1subscription {
2  postAdded {
3    id
4    title
5    author {
6      name
7    }
8  }
9}

15. Standard Response Format

A GraphQL response is always in the following format:

json
1{
2  "data": {
3    "user": {
4      "id": "5",
5      "name": "Andi"
6    }
7  },
8  "errors": []
9}

Difference from REST: GraphQL has only a single endpoint (for example /graphql). All queries go through it.

16. Query Execution Flow

Let’s look at how a GraphQL query flow is executed on the server. Here is the diagram using Mermaid:

MERMAID
flowchart TD
    A[Client mengirim Query] --> B[GraphQL Server menerima request]
    B --> C[Parsing Query & Validasi]
    C --> D[Ambil Resolver masing-masing Field]
    D --> E[Eksekusi Resolver (ambil dari DB/Service/Cache)]
    E --> F[Build Response Object]
    F --> G[Kirim Response ke Client]

Simulation: Everything in a Single Query

Suppose you want to fetch two different users, along with their posts and the total number of comments on each post. With GraphQL, you can get it all at once:

graphql
 1query GetUsersPosts {
 2  user1: user(id: "1") {
 3    name
 4    posts {
 5      title
 6      commentsCount
 7    }
 8  }
 9  user2: user(id: "2") {
10    name
11    posts {
12      title
13      commentsCount
14    }
15  }
16}

The response:

json
 1{
 2  "data": {
 3    "user1": {
 4      "name": "Andi",
 5      "posts": [
 6        {"title": "Intro GraphQL", "commentsCount": 4}
 7      ]
 8    },
 9    "user2": {
10      "name": "Budi",
11      "posts": [
12        {"title": "Belajar REST", "commentsCount": 1}
13      ]
14    }
15  }
16}

Table: REST vs GraphQL Query

AspectRESTGraphQL
EndpointMany (one per resource)1 (usually /graphql)
Data receivedFixed (predefined payload)Flexible, based on your needs
Overfetching/UnderfetchingHappens oftenNo, fields match the request
FilteringHard and inconsistent across APIsNative via arguments

Conclusion

Getting to know GraphQL queries and their standard format is the foundation for building modern APIs that are flexible and efficient. With a GraphQL architecture, clients are free to fetch exactly the data they need without overfetching, with fewer endpoints, and strong typing.

From basic structure, fragments, variables, and mutations all the way to subscriptions—everything is governed by a single standard format that is easy to learn yet powerful in practice. For backend engineers, understanding GraphQL’s query format and standard response is an important investment toward a more solid API experience going forward.

Happy querying! 🚀


References:


Want to explore further?

Drop a comment below to ask about implementation, best practices, and real-world use cases of GraphQL in production!

Related Articles

💬 Comments