I'd like to get fields resolved by another field.
I have a list generated according to some arguments and would like to update the total field
My approach is probably incorrect.
Obviously, I'm trying to avoid re-running the same database query and passing filter up a level in the query string.
So assume the following ruby type for my query:
Types::PostListType = GraphQL::ObjectType.define do
name 'PostList'
field :total, !types.Int, default_value: 0 # <-- this is what I'd like to update in :posts resolution
field :page, !types.Int, default_value: 0 # <-- this is what I'd like to update in :posts resolution
field :per_page, !types.Int, default_value: 0 # <-- this is what I'd like to update in :posts resolution
field :posts, types[Types::PostType] do
argument :page, types.Int, default_value: 1
argument :per_page, types.Int, default_value: 10 # <-- also need a limit here (hence why I need a field to tell what's been used in the end)
argument :filter, types.String, default_value: ''
resolve ->(user, *_args) {
posts = function_to_filter(args[:filter])
# how do I update total with posts.count here?
posts.paginate(page: args[:page], per_page: args[:per_page])
# how do I update per_page and page?
}
end
end
My query is something like this:
query ProspectList {
posts(filter:"example", page: 2) {
total
page
per_page
posts {
id
...
}
}
}
What you want is returning posts with total, page, per_page without re-running database query.
I'd like to modify query definition, because posts, total, page, per_page should be combined in type.
like this: Types::PostListType
Types::PostListType = GraphQL::ObjectType.define do
name 'PostList'
field :total, !types.Int
field :page, !types.Int
field :per_page, !types.Int
field :posts, types[Types::PostType]
end
Then return object in resolve which contains posts, total, page, per_page
Types::QueryType = GraphQL::ObjectType.define do
name "Query"
field :postlist, Types::PostListType do
argument :page, types.Int, default_value: 1
argument :per_page, types.Int, default_value: 1
argument :filter, types.String
resolve ->(_obj, args, _ctx) {
result = Post.paginate(page: args[:page], per_page: args[:per_page])
Posts = Struct.new(:posts, :page, :per_page, :total)
Posts.new(
result,
args[:page],
args[:per_page],
result.total_entries
)
}
end
end
It's also possible define an object and return. Wrapper::PostList
module Wrapper
class PostList
attr_accessor :posts, :total, :page, :per_page
def initialize(posts, page, per_page)
@posts = posts
@total = posts.total_entries
@page = page
@per_page = per_page
end
end
end
...
field :postlist, Types::PostListType do
argument :page, types.Int, default_value: 1
argument :per_page, types.Int, default_value: 1
argument :filter, types.String
resolve ->(_obj, args, _ctx) {
Wrapper::PostList.new(Post.paginate(page: args[:page], per_page: args[:per_page]), args[:page], args[:per_page])
}
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With