0
0
GraphQLquery~5 mins

Basic query syntax in GraphQL

Choose your learning style9 modes available
Introduction
GraphQL lets you ask for exactly the data you want from a database in a simple way.
When you want to get specific information from a database without extra data.
When building apps that need to load only certain fields to save time and data.
When you want to combine multiple pieces of data in one request.
When you want to explore what data is available in a database.
When you want to avoid asking for too much or too little data.
Syntax
GraphQL
query {
  fieldName {
    subField1
    subField2
  }
}
The word query starts the request to get data.
Inside curly braces, you list the fields you want exactly.
Examples
Get the name and email of a user.
GraphQL
query {
  user {
    name
    email
  }
}
Get the book's title and the author's name.
GraphQL
query {
  book {
    title
    author {
      name
    }
  }
}
Get a list of products with their id and price.
GraphQL
query {
  products {
    id
    price
  }
}
Sample Program
This query asks for the id, first name, and last name of an employee.
GraphQL
query {
  employee {
    id
    firstName
    lastName
  }
}
OutputSuccess
Important Notes
GraphQL queries always start with the keyword query, but you can omit it if you want.
You only get the fields you ask for, so be sure to include all you need.
Nested fields let you get related data in one request.
Summary
GraphQL queries ask for specific data by naming fields inside curly braces.
You can get nested data by listing subfields.
This helps apps get just the data they need efficiently.