GraphQL Query Complexity Limitation
In addition to the rate limit imposed on the API key, GraphQL requests must adhere to complexity limitations. Any extensive queries that surpass the complexity upper threshold will be declined. To pass query validation, GraphQL requests must satisfy the following two conditions:
- List fields must include the
first
parameter to specify the maximum number of items to be returned in the response. - Ensure that queries have a size of fewer than 50,000 fields. Refer to the examples below for guidance on calculating query size.
Example 1: List fields
{
getOrder(id: "ORDER-ID") {
status
tags {
id
name
}
}
}
This query is invalid since tags
is a list field and first
parameter needs to be supplied. A valid query should be:
{
getOrder(id: "ORDER-ID") {
status
tags (first: 100) {
id
name
}
}
}
To determine whether a field is a list, consult the documentation or test it using the editor in the playground.
Example 2: Calculate the number of fields on a simple query
query Query1 {
getFundSubscription(id: "FUND-ID") {
id
name
currency
}
}
The JSON-formatted response for Query1 should resemble the example below, consistently containing four fields, resulting in a query size of 4
.
{
"getFundSubscription" {
"id": "FUND-ID"
"name": "Demo fund"
"currency": "$"
}
}
Example 3: Calculate the number of fields on query with list field
query Query2 {
getFundSubscription(id: "FUND-ID") {
id
name
currency
admins(first: 10) {
email
firstName
lastName
}
}
}
In the context of Query2
outlined above, the request is made to retrieve the first 10
administrators of the fund. For each administrator, 3
fields are extracted. The corresponding response segment of the query is illustrated below, comprising a total of 31
fields, which include the admins
field itself.
{
...
"admins": [
{
"email": "[email protected]",
"firstName": "Harry",
"lastName": "Williams"
},
{
"email": "[email protected]",
"firstName": "Jenny",
"lastName": "Yoo"
},
...
]
}
The Query2
response hence can have 31
more fields in comparison to Query1
so the size of it is 4 + 31 = 35
Essentially, the size of a list field can be calculated as 1 + (number of items) * (size of each item)
, with the number of items being specified by the first
parameter.
Example 4: More complex query
query Query3 {
getFundSubscription(id: "FUND-ID") {
id
name
currency
admins(first: 10) {
email
firstName
lastName
}
orders(first: 50) {
id
status
contacts(first: 10) {
email
firstName
lastName
}
tags(first: 20) {
name
}
}
}
}
The orders
field adds 1 + 50 * (2 + (1 + 10 * 3) + (1 + 20)) = 2701
to the query size and make Query3
having a size of 35 + 2701 = 2736
.
Updated 12 months ago