Authors:
Ankit Mehta, Movyn John, Cille Schliebitz, Anita Gu
Changed on:
3 Feb 2025
In this lesson, we'll look at the benefits of using GraphQL, as well as explore the differences between GraphQL and REST based APIs. We will also look at some examples of a GraphQL query and a GraphQL mutation.
Introduction to GraphQL
For developers who've used REST-based APIs in the past, here are some key differences to note:
Next, let's look at the benefits of using GraphQL.
Benefits of Using GraphQL
To understand the benefits, let's look at GraphQL from an Efficiency, Flexibility, Unification and Introspection & Validation standpoint.
Efficiency | GraphQL allows the API client to send multiple queries or mutations within a single HTTP request. This reduces network latency and chattiness and allows developers to retrieve or write the specific data they need at once if it makes optimal and logical sense to do so. |
Flexibility | Retrieving data from the server can be done in a Query Language-like approach, where the API client defines which fields and objects they would like to receive, rather than being forced to retrieve predefined Data Transfer Objects, as is typical with REST-based APIs. |
Unification | In addition to being highly efficient, GraphQL provides the ability to query multiple data objects and declare the fields to be returned for each data object, allowing developers to work with unified data objects on the client side. |
Introspection & Validation | GraphQL APIs provide a schema that allows clients to introspect and validate their queries, mutations, and overall interaction with the API on the client side before executing or sending the HTTP request. |
Structure of a GraphQL Query
Query operations are used for retrieving data. GraphQL provides two types of query operations:
Seen below is a simple GraphQL query.
order vs orders | For a Get command, we would say Order. And, a Search will always end with an S in our schema. Here 'orders' is used as we're searching through the orders. |
status: "BOOKED" | This is an example of a GraphQL query that retrieves all orders that are currently under the status "BOOKED". |
First 100 records | "first: 100" means that it will retrieve the first 100 orders. So, if there are more than 100 orders, then it would only get the first 100. This is one of the paging-based parameters, GraphQL uses cursor-based paging, and you can also use 'last'. |
Edges and Nodes | When searching through records, we will always get edges and nodes, where each node represents a result of that Search. But, for the Get command, there wouldn't be edges and nodes, instead the specified fields will be returned. |
By default, every GraphQL query will return 10 results unless the count is specified using 'first' or 'last'. The maximum count for this 'first' or 'last' is 5000, and the use of pagination is recommended to extract a larger data set.
Please consider the Query Complexity while writing your query, as per the information posted here (opens in a new tab).
GraphQL Mutations
Mutation operations are used for creating and updating data. All mutation operations take a single parameter and have the following naming convention:
<<mutationName>>(<<MutationNameInput>>):<<Type>>
Create: Create mutations are used to create new data.
Update: Update mutations are used for updating existing data.
Examples
Create Order Mutation:
1mutation {
2 createOrder(input:{
3 ref:"HD_2341"
4 type:"HD"
5 retailer:{id:1}
6 customer:{id:34}
7 items:[{
8 ref:"VPRD_13"
9 productRef:"VPRD_13"
10 quantity:2
11 }]
12 }){
13 id
14 createdOn
15 }
16}
Language: json
Name: Json Snippet
Description:
The above mutation creates an order of type HD with 1 item - VPRD_13
Update Order Mutation:
1mutation {
2 updateOrder(input:{
3 id:5432
4 status:"BOOKED"
5 }){
6 id
7 createdOn
8 }
9}
Language: json
Name: Json Snippet
Description:
The above mutation updates the status of order to BOOKED
Some common integrations that use GraphQL are:
Copyright © 2025 Fluent Retail Pty Ltd (trading as Fluent Commerce). All rights reserved. No materials on this docs.fluentcommerce.com site may be used in any way and/or for any purpose without prior written authorisation from Fluent Commerce. Current customers and partners shall use these materials strictly in accordance with the terms and conditions of their written agreements with Fluent Commerce or its affiliates.