Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
search_object_graphql
Advanced tools
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
Require manually in your project
require 'search_object'
require 'search_object/plugin/graphql'
SearchObject
>= 1.2Graphql
>= 1.5Changes are available in CHANGELOG.md
Just include the SearchObject.module
and define your search options and their types:
class PostResolver < GraphQL::Schema::Resolver
include SearchObject.module(:graphql)
type [PostType], null: false
scope { Post.all }
option(:name, type: String) { |scope, value| scope.where name: value }
option(:published, type: Boolean) { |scope, value| value ? scope.published : scope.unpublished }
end
Then you can just use PostResolver
as GraphQL::Schema::Resolver:
field :posts, resolver: 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.
Search object itself can be documented, as well as its options:
class PostResolver < GraphQL::Schema::Resolver
include SearchObject.module(:graphql)
description 'Lists all posts'
option(:name, type: String, description: 'Fuzzy name matching') { ... }
option(:published, type: Boolean, description: 'Find published/unpublished') { ... }
end
class PostResolver < GraphQL::Schema::Resolver
include SearchObject.module(:graphql)
scope { Post.all }
option(:published, type: Boolean, default: true) { |scope, value| value ? scope.published : scope.unpublished }
end
Sometimes you need to pass additional options to the graphql argument method.
class PostResolver < GraphQL::Schema::Resolver
include SearchObject.module(:graphql)
scope { Post.all }
option(:published, type: Boolean, argument_options: { pundit_role: :read }) { |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 < GraphQL::Schema::Resolver
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:
class PostResolver < GraphQL::Schema::Resolver
include SearchObject.module(:graphql)
type PostType.connection_type, null: false
# ...
end
field :posts, resolver: PostResolver
Make sure all dependencies are installed with bundle install
rake
rake release
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)rake
)See also the list of contributors who participated in this project.
FAQs
Unknown package
We found that search_object_graphql demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.