0
0
GraphQLquery~5 mins

First GraphQL query

Choose your learning style9 modes available
Introduction

GraphQL lets you ask for exactly the data you want from a server in a simple way.

You want to get user details like name and email from a website.
You need to fetch a list of books with their titles and authors.
You want to load product information for an online store.
You want to get weather data for a city from a weather service.
Syntax
GraphQL
query {
  fieldName {
    subField1
    subField2
  }
}

The query keyword starts your request for data.

Inside curly braces, you list the fields you want from the server.

Examples
This asks for the name and email of a user.
GraphQL
query {
  user {
    name
    email
  }
}
You can omit the query keyword and just start with braces.
GraphQL
{
  book {
    title
    author
  }
}
You can name your query like GetProduct for clarity.
GraphQL
query GetProduct {
  product {
    id
    price
  }
}
Sample Program

This query asks for the id and name of a user.

GraphQL
query {
  user {
    id
    name
  }
}
OutputSuccess
Important Notes

GraphQL queries return only the data you ask for, no more and no less.

Always check the server schema to know what fields you can request.

Queries can be nested to get related data in one request.

Summary

GraphQL queries ask for specific data using a simple syntax.

You list the fields you want inside curly braces.

Queries can be named or anonymous.