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
.
![Query for 'order' type fields](/docs/html/img/9.1/image5.png)
Query for ‘order’ type fields¶
{
"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
}
]
}
}
Compound Types¶
For each field coming from Virtual DataPort compound types like array
and
register
, a new graphql object type is created. Its name will be the
same as the Virtual DataPort compound type prefixed with _STRUCT__
or _ARRAY__ depending if it corresponds to an array
or a register
.
To query these fields, we need to include the specific compound type subfields that we are interested in retrieving, in the same way as any other related object type.
In the query of the example below, we are requesting the field emp_id
of
the object struct_type
. Furthermore, we are requesting the subfields
name
and salary
from the struct_type
compound field called
employee
field.
![Query for 'struct_type' type fields](/docs/html/img/9.1/image25.png)
Query for ‘struct_type’ type fields¶
{
"data": {
"struct_type": [
{
"emp_id": 1,
"employee": {
"name": "John Smith",
"salary": 20090
}
},
{
"emp_id": 2,
"employee": {
"name": "Diane O'Donnell",
"salary": 32330
}
}
]
}
}
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
.
![Query with arguments](/docs/html/img/9.1/image8.png)
Query with arguments¶
{
"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:
Blob
IntervalDaySecond
IntervalYearMonth
Xml
Besides these custom scalar types, GraphQL object types originated from VDP register
and array
data types are not supported either as arguments.
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.
![Query with aliases](/docs/html/img/9.1/image1.png)
Query with aliases¶
{
"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.
![Query with fragment](/docs/html/img/9.1/image12.png)
Query with fragment¶
{
"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.
![Query with variables](/docs/html/img/9.1/image3.png)
Query with variables¶
{
"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.
![Query with directive @include](/docs/html/img/9.1/image22.png)
Query with directive @include¶
{
"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
.
![Query with pagination](/docs/html/img/9.1/image10.png)
Query with pagination¶
{
"data": {
"order_detail": [
{
"prod_id": 2010,
"price": 33
},
{
"prod_id": 2020,
"price": 33
}
]
}
}