ruby-on-railsgraphqlgraphql-ruby

In Rails, simple graphQL-ruby queries are ignoring sort order


The following simple query is not returning Posts ordered by created_at - According to the terminal, Rails is ordering by id, no matter what i put in ::Post.order(column: :direction)

Post Load (0.9ms)  SELECT  `posts`.* FROM `posts ` 
                ORDER BY `posts `.`id` ASC LIMIT 24

I can't imagine what could be reordering the query by :id, or blocking sort order.

Here's the resolver:

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

Base resolver

module Resolvers
  class BaseResolver < GraphQL::Schema::Resolver
  end
end

Query type

# 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

Query (using Relay style cursor pagination)

query getPosts {
  posts {
    pageInfo {
      startCursor
      endCursor
      hasNextPage
      hasPreviousPage
    }
    edges {
      cursor
      node {
        id
        thumbImageId
        textName
        where
        when
        createdAt
        updatedAt
      }
    }
  }
}

The app schema:

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

Background: I'm planning on using a resolver for this model query because the eventual version will have a lot of filters built with search_object_graphql, and while testing the sorting filters i noticed it was ignoring the sort order. So I stripped the resolver back down to the graphql schema inheritance, hardcoded the query scope with an orderby clause, and it's still not ordering the query.


Solution

  • Figured it out.

    This is a documented behavior of the Search Object plugin for graphql-ruby.

    I redid my query filters without this dependency, and sort order is working.