【发布时间】:2021-11-06 03:17:40
【问题描述】:
以下简单查询未返回由created_at 排序的Posts -
根据终端的说法,Rails 是通过id 订购的,无论我在::Post.order(column: :direction) 中输入什么
Post Load (0.9ms) SELECT `posts`.* FROM `posts `
ORDER BY `posts `.`id` ASC LIMIT 24
我无法想象什么会通过:id 重新排序查询,或者阻止排序顺序。
这是解析器:
module Resolvers
class Posts < Resolvers::BaseResolver
type Types::Models::PostType.connection_type, null: false
description "List or filter all observations"
def resolve
::Post.order(created_at: :desc)
end
end
end
基础解析器
module Resolvers
class BaseResolver < GraphQL::Schema::Resolver
end
end
查询类型
# graphql/types/query_type.rb
require("graphql/batch")
require("loaders/record_loader")
require("search_object")
require("search_object/plugin/graphql")
module Types
class QueryType < Types::BaseObject
field :posts, Types::Models::PostType.connection_type, null: false, resolver: Resolvers::Posts
查询(使用 Relay 风格的光标分页)
query getPosts {
posts {
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
}
edges {
cursor
node {
id
thumbImageId
textName
where
when
createdAt
updatedAt
}
}
}
}
应用架构:
class MyAppSchema < GraphQL::Schema
query(Types::QueryType)
mutation(Types::MutationType)
default_max_page_size 24
connections.add(ActiveRecord::Relation, GraphQL::Connections::Stable)
# GraphQL::Batch setup:
use GraphQL::Batch
end
背景:我计划为此模型查询使用解析器,因为最终版本将使用search_object_graphql 构建大量过滤器,并且在测试排序过滤器时我注意到它忽略了排序顺序。所以我将解析器剥离回 graphql 模式继承,使用 orderby 子句对查询范围进行硬编码,但它仍然没有对查询进行排序。
【问题讨论】:
标签: ruby-on-rails graphql graphql-ruby