AutoRelay
Last updated
Was this helpful?
Last updated
Was this helpful?
AutoRelay is a librairy designed to work alongside TypeGraphQL and make it easy to paginate your results using the Relay spec.
Please note this is currently a W.I.P, expect frequent breaking changes in the API until 1.0.0
AutoRelay is meant to be plug and play with TypeGraphQL and TypeORM (more orm support such as sequelize-typescript could be supported). Simply decorate your relations with @RelayedConnection
as such:
This will result in the following working SDL:
where User.recipes
is now a fully working field resolver, that automatically takes Relay ConnectionArguments (first, before, after, last) and returns a Relay Connection containing the results.
This lib is meant to be used with TypeGraphQL. It will not work with other code-first graphql librairies
Install the npm package:
npm install auto-relay --save
(Install an ORM if you plan to use @RelayedConnection
)
currently only TypeORM is supported
npm install @auto-relay/typeorm
Simply configure AutoRelay to use your ORM of choice, and you're ready to go !
index.ts
AutoRelay was designed with two goals in mind : Firstly, to automate the pagination between two entities that share a relationship in TypeORM. Secondly, to make it easy and boilerplate-free to implement your own Relay logic. This guide will showcase both of those.
Let's say you currently have two entities / graphql objects, User
and Recipe
. A recipe is always linked to an user, and a given user can have multiple recipes. Your classes might look like that :
With some custom logic (either lazy-loading or field resolvers) to fetch User.recipes / Recipe.user.
With AutoRelay, we're gonna replace all that logic with a single decorator, @RelayedConnection
.
Our User
will now look like this :
This will auto-magically create a few GraphQL types, such as UserRecipeConnection
and UserRecipeEdge
. Our User.recipes field now takes Relay's ConnectionArguments and returns UserRecipeConnection!
.
Our TypeORM integration gets the repository for Recipe
, translates the ConnectionArguments to an offset/limit tuple and fetches recipes connected to this User
.
@RelayedConnection can optionally accept an order parameter in its options, that will allow you to fetch while sorting on the column of your entities. For example, if we wanted to get our recipes by best ratings :
Let's imagine we now have an users
query, that we want to paginate using Relay. AutoRelay offers a few helpers with that.
And that's it! Again, AutoRelay has taken care under the hood of a few things: 1. It's created all the necessary GraphQL types for us. 2. It's ensured the users
query expects Connection Arguments, but conveniently translated them to limit/offset for us. 3. It takes the return of our findAndCount
calls and automatically transforms it to a Relay Connection
as expected by GraphQL.
Often, we have relationships that contains metadata. This is particulary the case for N:M relationships, where the join table might contain data our graphql client might want.
AutoRelay offers a simple API to extend the returned Edges
with information contained in a join table.
Let's say we want EntityB to be queryable with Relay arguments from EntityA. Our code would simply become :
This would result in the following working SDL: