Skip to main content
Version: v2.x

Postgres: Filter Using Values of Computed Fields

Introduction

You can use computed fields to filter your query results.

For example:

query {
author(where: { full_name: { _ilike: "%bob%" } }) {
id
first_name
last_name
}
}
Limitation

GraphQL does not allow arguments on input fields, unlike output fields. This limits the use of computed fields with arguments in where clauses.

For example, the get_articles computed field on the author table maps to the SQL function:

get_articles(table_input author, search text)

Here, search is passed as a GraphQL argument:

query {
author {
id
name
get_articles(search: "graphql") {
title
content
}
}
}

Computed fields with additional arguments (like search in this example) cannot currently be used for filtering.

The behavior of the comparison operators depends on whether the computed fields return scalar type values or set of table rows.

  • In case of scalar type, a row will be returned if the computed field returned scalar value satisfied the defined condition.
  • In case of table row type, a row will be returned if any of the returned rows sastisfy the defined condition.

Let's look at a few use cases based on the above:

Fetch if the scalar value returned by the computed field satisfies a condition

Example:

A computed field total_marks defined to a student table which computes the total sum of marks obtained from each subject. Fetch all students whose total marks is above "80":

GraphiQL
Query Variables
Request Headers
Documentation Explorer
No Schema Available

Fetch if any of the returned table rows by the computed field satisfy a condition

Example:

A computed field get_published_articles defined to a author table which returns set of article rows published. Fetch all authors who have atleast a published article in medicine field:

GraphiQL
Query Variables
Request Headers
Documentation Explorer
No Schema Available

Fetch if aggregate value of the returned table rows by the computed field satisfies a condition

Example:

A computed field get_published_articles defined to a author table which returns set of article rows published. Fetch all authors whose count of published articles is more than 10:

GraphiQL
Query Variables
Request Headers
Documentation Explorer
No Schema Available