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.5
Changes are available in CHANGELOG.md
Just include the SearchObject.module and define your search options and their types:
classPostResolver < GraphQL::Schema::ResolverincludeSearchObject.module(:graphql)type[PostType],null: falsescope{Post.all}option(:name,type: String){ |scope,value| scope.wherename: value}option(:published,type: Boolean){ |scope,value| value ? scope.published : scope.unpublished}endThen you can just use PostResolver as GraphQL::Schema::Resolver:
field:posts,resolver: PostResolverOptions 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:
classPostResolver < GraphQL::Schema::ResolverincludeSearchObject.module(:graphql)description'Lists all posts'option(:name,type: String,description: 'Fuzzy name matching'){ ... }option(:published,type: Boolean,description: 'Find published/unpublished'){ ... }endclassPostResolver < GraphQL::Schema::ResolverincludeSearchObject.module(:graphql)scope{Post.all}option(:published,type: Boolean,default: true){ |scope,value| value ? scope.published : scope.unpublished}endSometimes you need to pass additional options to the graphql argument method.
classPostResolver < GraphQL::Schema::ResolverincludeSearchObject.module(:graphql)scope{Post.all}option(:published,type: Boolean,argument_options: {pundit_role: :read}){ |scope,value| value ? scope.published : scope.unpublished}endSometimes you want to scope posts based on parent object, it is accessible as object property:
classPostResolver < GraphQL::Schema::ResolverincludeSearchObject.module(:graphql)# lists only posts from certain categoryscope{object.posts}# ...endIf you need GraphQL context, it is accessible as context.
classPostSearchincludeSearchObject.module(:graphql)OrderEnum=GraphQL::EnumType.definedoname'PostOrder'value'RECENT'value'VIEWS'value'COMMENTS'endoption:order,type: OrderEnum,default: 'RECENT'defapply_order_with_recent(scope)scope.order'created_at DESC'enddefapply_order_with_views(scope)scope.order'views_count DESC'enddefapply_order_with_comments(scope)scope.order'comments_count DESC'endendSearch objects can be used as Relay Connections:
classPostResolver < GraphQL::Schema::ResolverincludeSearchObject.module(:graphql)typePostType.connection_type,null: false# ...endfield:posts,resolver: PostResolverMake sure all dependencies are installed with bundle install
rake
rake release
- 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
- Radoslav Stankov - creator - RStankov
See also the list of contributors who participated in this project.