Relay'd FieldResolver
Adding relay to a FieldResolver (almost) automatically
In some scenarios we might need finer control on the fetching logic, while still taking advantage of the automatic ObjectTypes creation. AutoRelay offers two decorators for that job : @RelayedField and @RelayedFieldResolver
@Resolver(of => MyObject)
export class MyResolver {
constructor(
protected readonly myRepository: Repository<MyNestedObject>
) {}
@RelayedFieldResolver(() => MyNestedObject)
public async collection(
@RelayLimitOffset() { limit, offset }: RelayLimitOffsetArgs
): Promise<[number, MyNestedObject[]]> {
return this.myRepository.findAndCount({
where: {
// any business logic you might have
},
skip: offset,
take: limit
})
}
}And that's it! Again, AutoRelay has taken care under the hood of a few things:
It's created all the necessary GraphQL types for us
It's ensured the
usersquery expects Connection Arguments, but conveniently translated them to limit/offset for us.It takes the return of our
findAndCountcalls and automatically transforms it to a RelayConnectionas expected by GraphQL.
In situations where you want your field to appear in the SDL, but do not want to actually create the resolver logic, AutoRelay provides a @RelayedField decorator that acts the same way as @Field while still providing a shorthand to creating Collection types.
Last updated
Was this helpful?