As for an example, let’s have a look at room_stays
query arguments:
room_stays(
filter: RoomStayFilter
first: Int
after: String
last: Int
before: String
): RoomStayConnection!
Every query will have:
- a common set of arguments, dedicated for
limiting
andpaginating
the resultset.first
,after
,last
, andbefore
are common arguments to all queries - a specific set of arguments, like
filter
. In the case ofRoomStays
, we are dealing withRoomStayFilter
object type.
Filtering¶
filter
argument¶
Lets you specify one or multiple search criteria.
By id
¶
query RoomStays {
room_stays(
filter: {
id: { in: [23838, 23839] }
}
) {
...
}
By date
¶
By an exact date
query RoomStays {
room_stays(
filter: {
date: "2022-02-18"
}
) {
...
}
By reservation_from
and/or reservation_to
¶
Filtering by a date range
.
query RoomStays {
room_stays(
filter: {
reservation_from: { gt: "2022-02-18" }
reservation_to: { lt: "2022-02-28" }
}
) {
...
}
By reservationStatus
¶
query RoomStays {
room_stays(
filter: {
reservationStatus: { in: [ ACTIVE, INVOICED] }
}
) {
...
}
By not
¶
Can be used as a negation
with all the same filtering terms.
For example, to find all Reservations
with date > 2022-02-18
AND status != ACTIVE
the following filter combination can be used:
query RoomStays {
room_stays(
filter: {
reservation_from: { gt: "2022-02-18" }
not : {
reservationStatus: { eq: ACTIVE }
}
}
) {
...
}
Limiting¶
first
argument¶
Lets you specify how many items you want the API to return from the beginning of the resultset.
In the following example, we request to return 100 Reservations
from 2022-02-18
:
query RoomStays {
room_stays(
filter: {
reservation_from: { gt: "2022-02-18" }
}
first: 100
) {
...
}
last
argument¶
Lets you specify how many items you want the API to return from the end of the resultset.
Pagination¶
after
argument¶
Lets you specify the cursor (identifier of the resultset item) after which you want the API to return the next records from
before
argument¶
Lets you specify the cursor (identifier of the resultset item) after which you want the API to return the next records from
Examples¶
Pagination with limits & filters combined¶
In this example, we will go through an actual pagination use case.
Step 1¶
We execute the first query of our interest:
Variables
{
"date": "2022-02-18",
"cursor": null
}
Query
query ArrivingPrimaryGuests($date: Date!, $cursor: String) {
room_stays(
filter: { reservation_from: { gt: $date } } # our search criteria
first: 10 # page size
after: $cursor # offset
) {
edges {
node {
arrival: reservation_from
departure: reservation_to
gross
}
}
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
}
totalCount
}
}
Response
{
"data": {
"room_stays": {
"edges": [
{
...
}
],
"pageInfo": {
"startCursor": "MjM4NTI=",
"endCursor": "MjM4NjE=",
"hasNextPage": true,
"hasPreviousPage": false
},
"totalCount": 8152
}
}
}
In the pageInfo
of the response we see that:
startCursor
(cursor of the first record in the returned batch) isMjM4NTI
endCursor
(cursor of the last record in the returned batch) isMjM4NjE=
hasNextPage
reports the next page we could request existshasPreviousPage
reports the previous page we could request doesn’t exist
totalCount
reports that there are 8152 records in total matching our search criteria
Step 2¶
Knowing the cursor position of the last record we retrieved ("endCursor": "MjM4NjE="
), we can issue the next query fetching the next batch of records:
Variables
{
"date": "2022-02-18",
"cursor": "MjM4NjE="
}
Query
Exactly the same as in Step 1
Response
{
"data": {
"room_stays": {
"edges": [
{
...
}
],
"pageInfo": {
"startCursor": "MjM4NjI=",
"endCursor": "MjM4NzE=",
"hasNextPage": true,
"hasPreviousPage": true
},
"totalCount": 8152
}
}
}
In the pageInfo
of the response we see that:
startCursor
(cursor of the first record in the returned batch) isMjM4NjI=
endCursor
(cursor of the last record in the returned batch) isMjM4NzE=
hasNextPage
reports the next page we could request existshasPreviousPage
reports the previous page we could request exists
Step 3¶
In order to continue the pagination and fetch next batches, we again should take the last returned endCursor
and pass on to after
query argument.
Variables
{
"date": "2022-02-18",
"cursor": "MjM4NzE="
}