# Introduction

## Motivation

While the first goal of AutoRelay always has been to adhere to the Relay spec, pretty quickly it became clear that more powerful features could be needed in day to day operation. The first of those were to [Extend edges](https://github.com/wemaintain/auto-relay/tree/2f719fbc23bc4ec6cf181564d33f137b194470a2/docs/sorting/pagination-1/extending-edges-relationship-metadata.md) to add relationship metadata and later [Extending PageInfo / Connection](https://github.com/wemaintain/auto-relay/tree/2f719fbc23bc4ec6cf181564d33f137b194470a2/docs/sorting/pagination-1/extending-pageinfo-connection.md) to be able to implement custom desired logic such as `totalCount`.

In the same vein as those, we quickly came upon the need to implement and standardise a way to sort fields, whether be it in a `Relayed` query or not.

As to not bloat the main `auto-relay` package, all sorting features have been ported in `@auto-relay/sorting` and can be used besides or without the main pagination features.

## Quick look

Currently, the sorting package allows automatically generating all the necessary types for sorting, as well as providing simple API to convert those to input expected by ORMs.

* [x] Sorting by fields that are columns
* [ ] Sorting by custom logic (to be implemented)

```typescript
@ObjectType()
@Entity()
class Entity {

  @Column()
  @Field({ name: "fieldA" })
  public columnA: string

  @Column()
  @Field({ name: "fieldB })
  public columnB: string

}

@Resolver(type => Entity)
export class EntityResolver {

  @Query(() => [Entity])
  @Sortable()
  public async entities(
    @OrderOptions() order: TypeORMOrdering
  ) {
    return this.entityManager.findMany({ order })
  }

}
```

will generate the following working SDL:

```graphql
type Entity {
  fieldA: String!
  fieldB: String!
}

type Query {
  entities(order: [EntityOrdering!]): [Entity!]!
}

type EntityOrderOptions {
  direction: OrderingDirection = OrderingDirection.ASC
  sort: EntitySortableField
}

enum EntitySortableField {
  FieldA
  FieldB
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://superd001.gitbook.io/autorelay/dependabot-npm_and_yarn-lodash-4.17.19/sorting/introduction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
