Skip to main content
Version: v3.x

Native Queries

Introduction

Native Queries allow you to run custom aggregation pipelines on your MongoDB database. This allows you to run queries that are not supported by Hasura's GraphQL engine.

Setup

Native Queries can be defined by

  1. Adding a native_queries directory if one doesn't already exist in your connector configuration directory
  2. Adding a .json file following the syntax laid out in the following sections.

Native Queries can take arguments using the "{{argument_name}}" syntax. Arguments must be specified along with their type.

Example

Here's an example of simple "hello" Native Query:

Configuration

{
"name": "hello",
"representation": "function",
"description": "Basic test of native queries",
"arguments": {
"name": { "type": { "scalar": "string" } }
},
"resultDocumentType": "Hello",
"objectTypes": {
"Hello": {
"fields": {
"__value": { "type": { "scalar": "string" } }
}
}
},
"pipeline": [
{
"$documents": [
{
"__value": "{{ name }}"
}
]
}
]
}

This will create a query called "hello" which takes a single argument called "name" of type string. The query will return an object with the key "__value" and the value of the argument "hello".

Valid Native Query syntax

Check out our page on writing valid Hasura DDN Native Operations syntax.

Usage

With the example above, you can then use the query in your GraphQL API like this:

query MyQuery {
hello(name: "world") {
__value
}
}
Loading...