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

Query for ‘order’ type fields

Response 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
             }
      ]
  }
}

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

Query with arguments

Response for 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:

  • 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.

Query with aliases

Query with aliases

Response for 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

Query with fragment

Response for 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

Query with variables

Response for 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

Query with directive @include

Response for 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"
             }
     ]
  }
}