我怎样才能让一个字段 updating/changing 另一个字段?
How can I have one field updating/changing another one?
我希望通过另一个字段解析字段。
我有一个根据一些参数生成的列表,想更新总字段
我的方法可能不正确。
显然,我试图避免重新运行相同的数据库查询并在查询字符串中向上传递一个级别的过滤器。
因此,假设我的查询输入以下 ruby 类型:
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
我的查询是这样的:
query ProspectList {
posts(filter:"example", page: 2) {
total
page
per_page
posts {
id
...
}
}
}
你想要的是 returning post with total, page, per_page without re-运行 database query.
我想修改查询定义,因为posts, total, page, per_page 应该在类型上合并。
像这样: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
然后 return 解析对象,其中包含 个帖子、总计、页数、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
也可以定义一个对象 return。 包装器::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])
}
...
我希望通过另一个字段解析字段。
我有一个根据一些参数生成的列表,想更新总字段
我的方法可能不正确。
显然,我试图避免重新运行相同的数据库查询并在查询字符串中向上传递一个级别的过滤器。
因此,假设我的查询输入以下 ruby 类型:
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
我的查询是这样的:
query ProspectList {
posts(filter:"example", page: 2) {
total
page
per_page
posts {
id
...
}
}
}
你想要的是 returning post with total, page, per_page without re-运行 database query.
我想修改查询定义,因为posts, total, page, per_page 应该在类型上合并。
像这样: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
然后 return 解析对象,其中包含 个帖子、总计、页数、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
也可以定义一个对象 return。 包装器::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])
}
...