hydra
Search…
Graphql Queries
GraphQL queries are used to fetch data from the server.

Introduction

Hydra cli tooling auto-generates queries as part of the GraphQL schema from your input schema. It generates a range of possible queries and operators that also work with relationships defined in your input schema.
All entities of the input schema tracked by the cli (re-generation is required when any change happens to the input schema) can be queried over the GraphQL endpoint.

Exploring queries

You can explore the entire schema and the available queries using the GraphiQL interface by running your graphql-server or looking at the graphql-server/generated/schema.graphql file.
Let’s take a look at the different queries you can run using the GraphQL server. We’ll use examples based on a typical channel/video schema for reference.
  • Simple entity queries
  • Relation entity queries
  • Filter query results / search queries
  • Sort query results
  • Paginate query results

Simple entity queries

You can fetch a single entity or multiple entities of the same type using a simple entity query.
Fetch list of entities
Example: Fetch a list of channels:
1
query {
2
channels {
3
id
4
handle
5
}
6
}
Copied!
Fetch an entity using its unique fields
Example: Fetch a channel using by unique id:
1
query Query1 {
2
channelByUniqueInput(where: { id: "1" }) {
3
id
4
handle
5
}
6
}
7
8
query Query2 {
9
channelByUniqueInput(where: { handle: "Joy Channel" }) {
10
id
11
handle
12
}
13
}
Copied!

Relation entity queries

Please look at the cross-filters documantation.

Filter query results / search queries

The where argument
You can use the where argument in your queries to filter results based on some field’s values. You can even use multiple filters in the same where clause using the AND or the OR operators.
For example, to fetch data for Joy Channel:
1
query {
2
channels(where: { handle_eq: "Joy Channel" }) {
3
id
4
handle
5
}
6
}
Copied!

Comparison operators

Supported Scalar Types
Hydra supports following scalar types:
  • String
  • Int
  • Float
  • BigInt
  • Boolean
  • Bytes
  • DateTime
Equality Operators (_eq)
_eq is supported by all the scalar types
The following are examples of using this operator on different types:
  1. 1.
    Fetch a list of videos where title is "Bitcoin"
  2. 2.
    Fetch a list of videos where isExplicit is "true"
  3. 3.
    Fetch a list of videos publishedOn "2021-01-05"
1
query Query1 {
2
videos(where: { title_eq: "Bitcoin" }) {
3
id
4
title
5
}
6
}
7
8
query Query2 {
9
videos(where: { isExplicit_eq: true }) {
10
id
11
title
12
}
13
}
14
15
query Query3 {
16
videos(where: { publishedOn_eq: "2021-01-05" }) {
17
id
18
title
19
}
20
}
Copied!
Greater than or less than operators (gt, lt, gte, lte)
The _gt (greater than), _lt (less than), _gte (greater than or equal to), _lte (less than or equal to) operators are available on Int, BigInt, Float, DataTime types.
The following are examples of using these operators on different types:
  1. 1.
    Fetch a list of videos published before "2021-01-05"
  2. 2.
    Fetch a list of channels before block "999"
1
query Query1 {
2
videos(where: { publishedOn_gte: "2021-01-05" }) {
3
id
4
title
5
}
6
}
7
8
query Query2 {
9
channels(where: { block_lte: "999" }) {
10
id
11
handle
12
}
13
}
Copied!
List based search operators (_in)
_in operator is available on all scalar types except DataTime.
The following are examples of using this operators on different types:
  1. 1.
    Fetch a list of videos titled with "For Children" or "For Kids"
  2. 2.
    Fetch a list of channels created in block 1, 2 or 3
1
query Query1 {
2
videos(where: { title_in: ["For Children", "For Kids"] }) {
3
id
4
title
5
}
6
}
7
8
query Query2 {
9
channels(where: { block_in: [1, 2, 3] }) {
10
id
11
handle
12
}
13
}
Copied!
Text search or pattern matching operators (_contains, _startsWith, _endsWith)
The _contains, _startsWith, _endsWith operators are used for pattern matching on string fields.
Example:
1
query Query1 {
2
videos(where: { title_contains: "Bitcoin" }) {
3
id
4
title
5
}
6
}
7
8
query Query2 {
9
videos(where: { title_endsWith: "cryptocurrency" }) {
10
id
11
title
12
}
13
}
Copied!

Using multiple filters in the same query (AND, OR)

You can group multiple parameters in the same where argument using the AND or the OR operators to filter results based on more than one criteria.
Example AND:
Fetch a list of videos published in a specific time-frame:
1
query {
2
videos(
3
where: {
4
AND: [
5
{ publishedOn_gte: "2021-01-05" }
6
{ publishedOn_lte: "2020-01-05" }
7
]
8
}
9
) {
10
id
11
title
12
publishedOn
13
}
14
}
Copied!
Example OR:
Fetch a list of videos isExplicit "true" or published after "2021-01-05":
1
query {
2
videos(
3
where: { OR: [{ publishedOn_gte: "2021-01-05" }, { isExplicit_eq: true }] }
4
) {
5
id
6
title
7
isExplicit
8
}
9
}
Copied!
Last modified 6mo ago