【问题标题】:In Rails, simple graphQL-ruby queries are ignoring sort order在 Rails 中,简单的 graphQL-ruby 查询会忽略排序顺序
【发布时间】: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


    【解决方案1】:

    想通了。

    这是 graphql-ruby 的搜索对象插件的记录行为。

    我在没有这种依赖关系的情况下重新编写了查询过滤器,并且排序顺序正常。

    【讨论】:

      猜你喜欢
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 2020-06-21
      • 2014-11-14
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多