SearchObject plugin for GraphQL Ruby.
Add this line to your application's Gemfile:
gem 'search_object_graphql'
And then execute:
$ bundle
Or install it yourself as:
$ gem install search_object_graphql
SearchObject
>= 1.2Graphql
>= 1.5
Just include the SearchObject.module
and define your search options and their types:
class PostResolver
include SearchObject.module(:graphql)
type types[PostType]
scope { Post.all }
option(:name, type: types.String) { |scope, value| scope.where name: value }
option(:published, type: types.Boolean) { |scope, value| value ? scope.published : scope.unpublished }
end
Then you can just use PostResolver
as GraphQL::Function:
field :posts, function: PostResolver
Options are exposed as arguments in the GraphQL query:
posts(name: 'Example') { ... }
posts(published: true) { ... }
posts(published: true, name: 'Example') { ... }
You can find example of most important features and plugins - here.
Custom types can be define inside the search object:
class PostResolver
include SearchObject.module(:graphql)
type do
name 'Custom Type'
field :id, !types.ID
field :title, !types.String
field :body, !types.String
end
# ...
end
Search object itself can be documented, as well as its options:
class PostResolver
include SearchObject.module(:graphql)
description 'Lists all posts'
option(:name, type: types.String, description: 'Fuzzy name matching') { ... }
option(:published, type: types.Boolean, description: 'Find published/unpublished') { ... }
end
class PostResolver
include SearchObject.module(:graphql)
scope { Post.all }
option(:published, type: types.Boolean, default: true) { |scope, value| value ? scope.published : scope.unpublished }
end
Sometimes you want to scope posts based on parent object, it is accessible as object
property:
class PostResolver
include SearchObject.module(:graphql)
# lists only posts from certain category
scope { object.posts }
# ...
end
If you need GraphQL context, it is accessible as context
.
class PostSearch
include SearchObject.module(:graphql)
OrderEnum = GraphQL::EnumType.define do
name 'PostOrder'
value 'RECENT'
value 'VIEWS'
value 'COMMENTS'
end
option :order, type: OrderEnum, default: 'RECENT'
def apply_order_with_recent(scope)
scope.order 'created_at DESC'
end
def apply_order_with_views(scope)
scope.order 'views_count DESC'
end
def apply_order_with_comments(scope)
scope.order 'comments_count DESC'
end
end
Search objects can be used as Relay Connections:
connection :posts, Types::PostType.connection_type, function: Resolvers::PostSearch
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Run the tests (
rake
) - Create new Pull Request