graphql-crystal / graphql

GraphQL server library for Crystal
https://graphql-crystal.github.io/graphql/index.html
MIT License
134 stars 13 forks source link

Support for relationship, pagination and cursors #32

Closed kefahi closed 2 years ago

kefahi commented 2 years ago

It would be great if we can demonstrate how relationship, pagination and cursors could be implemented.

Here is some documentation: https://graphql.org/learn/pagination

It is easy to return a list of items e.g. Array(Bar) but not sure how to implement pagination.

Thanks!

jgillich commented 2 years ago

GraphQL is not tied to any specific data store, you can implement these things in any way you want. The example from your link would look something like this:

@[GraphQL::Field]
def friends(ctx: MyContext, first: Int32?, offset: Int32?): Array(Hero)
  first = 100 if first.nil?
  offset = 0 if offset.nil?
  rows = ctx.database_query("SELECT foo from bar LIMIT #{first} OFFSET #{offset}")
  rows.map { |row| Hero.new row }
end
kefahi commented 2 years ago

That was very helpful. Thank you!