Skip to main content
Version: v3.x (DDN)

OrderBy Expressions

Introduction

Hasura provides powerful tools to control sorting and ordering data. OrderByExpression types let you define customizable sorting rules for a type.

How OrderByExpressions work

There are two types of OrderByExpressions:

TypeDescription
ScalarThis specifies how a user is able to order a scalar field.
ObjectThis specifies how fields of a type can be ordered.

Examples

Scalar

This specifies how a user is able to order a scalar field. For instance, you might want to say that a user can only sort a String type in ascending order instead of both ascending and descending order. You can do that with the following metadata:

kind: OrderByExpression
version: v1
definition:
name: String_orderby_exp_asc
operand:
scalar:
orderedType: String
enableOrderByDirections:
enableSpecific:
- Asc
graphql:
expressionTypeName: StringOrderByExp

Object

An object OrderByExpression is used to define how fields of a type can be ordered. You can specify which OrderByExpression is used to order each field or relationship, and then defer the mappings of individual scalar types to those OrderByExpressions.

kind: OrderByExpression
version: v1
definition:
name: Album_orderby_exp
operand:
object:
orderedType: Album
orderableFields:
- fieldName: AlbumId
orderByExpression: Int_orderby_exp
- fieldName: ArtistId
orderByExpression: Int_orderby_exp_asc
- fieldName: Address
orderByExpression: Address_orderby_exp
orderableRelationships:
- relationshipName: artist
orderByExpression: Artist_orderby_exp
- relationshipName: genre
graphql:
expressionTypeName: Album_orderby_Exp

Note here that we can specify different orderBy operators for AlbumId and ArtistId by using different OrderByExpressions for them. We are also able to define ordering on nested objects such as Address.


Metadata structure

OrderByExpression

Definition of an order by expression on an OpenDD type.

KeyValueRequiredDescription
kindOrderByExpressiontrue
versionv1true
definitionOrderByExpressionV1trueDefinition of a type representing an order by expression on an OpenDD type.

Examples:

kind: OrderByExpression
version: v1
definition:
name: Album_order_by_exp
operand:
object:
orderedType: Album
orderableFields:
- fieldName: AlbumId
orderByExpression: Int_order_by_exp
- fieldName: ArtistId
orderByExpression: Int_order_by_exp
- fieldName: Address
orderByExpression: Address_order_by_default_exp
orderableRelationships:
- relationshipName: artist
orderByExpression: Artist_order_by_default_exp
graphql:
expressionTypeName: App_Album_order_by_exp
description: Order by expression for Albums
kind: OrderByExpression
version: v1
definition:
name: Int_order_by_exp
operand:
scalar:
orderedType: Int
enableOrderByDirections:
enableAll: true
graphql:
expressionTypeName: App_Int_order_by_exp
description: Order by expression for Int

OrderByExpressionV1

Definition of a type representing an order by expression on an OpenDD type.

KeyValueRequiredDescription
nameOrderByExpressionNametrueThe name used to refer to this expression. This name is unique only in the context of the orderedType
operandOrderByExpressionOperandtrueThe type that this expression applies to.
graphqlOrderByExpressionGraphQlConfiguration / nullfalseConfiguration for how this order by expression should appear in the GraphQL schema.
descriptionstring / nullfalseThe description of the order by expression.

OrderByExpressionGraphQlConfiguration

GraphQL configuration settings for a type representing an order by expression on an OpenDD type.

KeyValueRequiredDescription
expressionTypeNameGraphQlTypeNametrueThe name of a GraphQL type.

GraphQlTypeName

The name of a GraphQL type.

Value: string

OrderByExpressionOperand

Configuration for object or scalar order by expression

Must have exactly one of the following fields:

KeyValueRequiredDescription
objectOrderByExpressionObjectOperandfalseDefinition of an type representing an order by expression on an OpenDD object type.
scalarOrderByExpressionScalarOperandfalseDefinition of a type representing an order by expression on an OpenDD scalar type.

OrderByExpressionScalarOperand

Definition of a type representing an order by expression on an OpenDD scalar type.

KeyValueRequiredDescription
orderedTypeTypeNametrueThe type that this expression applies to.
enableOrderByDirectionsEnableAllOrSpecifictrueOrder by directions supported by this scalar type.

EnableAllOrSpecific

Enable all or specific values.

Must have exactly one of the following fields:

KeyValueRequiredDescription
enableAllbooleanfalse
enableSpecific[OrderByDirection]false

OrderByDirection

Value: Asc / Desc

TypeName

The type that this expression applies to.

One of the following values:

ValueDescription
InbuiltTypeAn inbuilt primitive OpenDD type.
CustomTypeName

InbuiltType

An inbuilt primitive OpenDD type.

Value: ID / Int / Float / Boolean / String

OrderByExpressionObjectOperand

Definition of an type representing an order by expression on an OpenDD object type.

KeyValueRequiredDescription
orderedTypeCustomTypeNametrueThe type that this expression applies to.
orderableFields[OrderByExpressionOrderableField]trueOrderable fields of the orderedType
orderableRelationships[OrderByExpressionOrderableRelationship]trueOrderable relationships

OrderByExpressionOrderableRelationship

Definition of a relationship in a type representing an order by expression on an OpenDD type.

KeyValueRequiredDescription
relationshipNameRelationshipNametrueThe name of the relationship.
orderByExpressionOrderByExpressionName / nullfalseThe OrderByExpression to use for this relationship. This is optional for model relationships. If not specified we use the model's OrderByExpression configuration. For local command relationships this is required.

RelationshipName

The name of the GraphQL relationship field.

Value: string

OrderByExpressionOrderableField

Definition of a field in a type representing an order by expression on an OpenDD type.

KeyValueRequiredDescription
fieldNameFieldNametrue
orderByExpressionOrderByExpressionNametrueOrderByExpression to use for this field.

FieldName

The name of a field in a user-defined object type.

Value: string

CustomTypeName

The name of a user-defined type.

Value: string

OrderByExpressionName

The name of an order by expression.

Value: string