Basic Querying¶
This section shows how to query the Denodo GraphQL Service.
Fields¶
GraphQL is about asking for specific fields on objects. Queries can also traverse related objects and their fields, letting users fetch related data in one request.
In the query of the example below, we are requesting the fields
order_id
, order_desc
and total_price
of the object order
.
Also, for each order
, we are retrieving the related object
order_detail
, with its fields prod_id
and price
.
{
"data": {
"order": [
{
"order_id": 111,
"order_desc": "Internet and phone",
"order_detail": [
{
"prod_id": 2000,
"price": 60
},
{
"prod_id": 2020,
"price": 40
}
],
"total_price": 100
},
{
"order_id": 222,
"order_desc": "Cable at discounted price",
"order_detail": [
{
"prod_id": 2010,
"price": 15
}
],
"total_price": 15
}
]
}
}
Arguments¶
GraphQL allows you to pass arguments to fields to narrow results.
In the query of the example below, we are requesting the fields
order_desc
and total_price
of the object order
that has 222
as order_id
.
{
"data": {
"order": [
{
"order_desc": "Cable at discounted price",
"total_price": 15
}
]
}
}
Notice that the following custom scalar types are not supported as an argument in GraphQL queries:
Array
Blob
IntervalDaySecond
IntervalYearMonth
Struct
Xml
Aliases¶
If you want to query for the same fields with different arguments you will need to use aliases to avoid name conflicts.
In the query of the example below, we use first_order
and second_order
as aliases of the two queries.
{
"data": {
"first_order": [
{
"order_desc": "Internet and phone",
"total_price": 100
}
],
"second_order": [
{
"order_desc": "Cable at discounted price",
"total_price": 15
}
]
}
}
Fragments¶
Fragments allow you to define a set of fields once and reuse them in all the queries you need, avoiding field repetition.
In the query of the example below, we define the orderFields
fragment
and use it in both queries first_order
and second_order
. This is a
sample, but in complex scenarios fragments are really useful.
{
"data": {
"first_order": [
{
"order_desc": "Internet and phone",
"total_price": 100
}
],
"second_order": [
{
"order_desc": "Cable at discounted price",
"total_price": 15
}
]
}
}
Variables¶
Variables are used when the arguments of a query are going to be dynamic. This way, when we want to use the same query with different argument values, we only have to pass a different variable rather than writing a new GraphQL query.
In the query of the example below, $price
is a GraphQL variable.
{
"data": {
"order_detail": [
{
"order_id": 333,
"prod_id": 2000,
"last_updated_time": "2018-06-13 17:08:02",
"price": 33
},
{
"order_id": 333,
"prod_id": 2010,
"last_updated_time": "2018-06-13 17:08:02",
"price": 33
}
]
}
}
Directives¶
The Denodo GraphQL Service includes the two directives defined as mandatory in the specification:
@include(if: Boolean)
: only include this field in the result if the argument is true.@skip(if: Boolean)
: skip this field if the argument is true.
{
"data": {
"order": [
{
"order_id": 111,
"order_desc": "Internet and phone"
},
{
"order_id": 222,
"order_desc": "Cable at discounted price"
},
{
"order_id": 333,
"order_desc": "3 in one offer"
}
]
}
}
Pagination¶
Denodo GraphQL Service implements offset-based pagination.
The query order_detail(_offset:1, _first:2)
asks for the next two
order_details in the list, skipping the first one.
The pagination arguments are:
_offset
: the number of results to skip_first
: the number of results to fetch. If_first
is not specified, its value is the one configured in the propertyquery.default-page-size
that has a default value of1000
.
{
"data": {
"order_detail": [
{
"prod_id": 2010,
"price": 33
},
{
"prod_id": 2020,
"price": 33
}
]
}
}