Labs ICT
Pro Login

Learn GraphQL

Learn GraphQL

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. Instead of having multiple endpoints that return fixed data structures, GraphQL gives you a single endpoint where you ask for exactly what you need.

Think of it like ordering food at a restaurant. REST is like ordering from a fixed menu where you get the entire dish whether you want all the ingredients or not. GraphQL is like ordering from a build-your-own bowl place where you pick exactly what you want.

Why GraphQL Exists

GraphQL was created by Facebook in 2012 and open-sourced in 2015. They needed a better way to power their mobile app, which had to work with slow network connections and needed to fetch data efficiently.

The main problems GraphQL solves are over-fetching and under-fetching. With REST, you often get more data than you need (over-fetching) or have to make multiple requests to get all the data you need (under-fetching). GraphQL eliminates both problems.

Here's a simple comparison:

// REST: Multiple endpoints
GET /api/users/1
GET /api/users/1/posts
GET /api/users/1/followers

// GraphQL: One endpoint, ask for what you need
query {
  user(id: 1) {
    name
    email
    posts {
      title
    }
  }
}

Core Concepts

Before diving into GraphQL, let's understand the three core concepts:

Schema: This is the contract between your client and server. It defines what data is available and how to access it. Think of it as a blueprint for your API.

Queries: These are requests for data. When you ask GraphQL for information, you write a query that describes exactly what you want.

Resolvers: These are functions that actually fetch the data. When a query comes in, resolvers figure out how to get the requested information from your database or other sources.

// A simple schema definition
const typeDefs = `
  type User {
    id: ID!
    name: String!
    email: String!
  }

  type Query {
    user(id: ID!): User
  }
`;

// A simple resolver
const resolvers = {
  Query: {
    user: (parent, args, context) => {
      return context.db.users.find(user => user.id === args.id);
    },
  },
};

Your First GraphQL Query

Let's see GraphQL in action. Imagine you have a simple API with users and posts. With GraphQL, you can write a query like this:

query {
  users {
    name
    posts {
      title
      comments {
        text
      }
    }
  }
}

This single query fetches all users, their posts, and the comments on each post. The server responds with exactly the data you asked for, nothing more, nothing less.

The response would look something like this:

{
  "data": {
    "users": [
      {
        "name": "Alice",
        "posts": [
          {
            "title": "My First Post",
            "comments": [
              { "text": "Great post!" }
            ]
          }
        ]
      }
    ]
  }
}

What You'll Learn

In this tutorial series, we'll cover everything from the basics to advanced topics. You'll learn how to define schemas, write queries and mutations, handle authentication, and build real-world applications with GraphQL.

By the end, you'll be comfortable building GraphQL APIs and using them in your projects. Let's get started!