CompanyCam/graphql-searchkick

View on GitHub
lib/graphql/searchkick/searchable_extension.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

require 'graphql'

module GraphQL
  module Searchkick
    class SearchableExtension < GraphQL::Schema::FieldExtension
      def apply
        field.argument(:query, String, required: false, description: 'A search query')
      end

      def resolve(object:, arguments:, context:)
        next_args = arguments.dup
        result = yield(object, next_args)

        if defined?(ActiveRecord::Relation) && result.is_a?(ActiveRecord::Relation)
          result
        else
          model = options[:model_class]
          LazySearch.new(result, query: next_args[:query], model_class: model)
        end
      end
    end
  end
end