Skip to content

Repository files navigation

Gem VersionCode ClimateCode coverage

SearchObject::Plugin::GraphQL

SearchObject plugin for GraphQL Ruby.

Installation

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'

Dependencies

  • SearchObject >= 1.2
  • Graphql >= 1.5

Changelog

Changes are available in CHANGELOG.md

Usage

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}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') { ... }

Example

You can find example of most important features and plugins - here.

Features

Documentation

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'){ ... }end

Default Values

classPostResolver < GraphQL::Schema::ResolverincludeSearchObject.module(:graphql)scope{Post.all}option(:published,type: Boolean,default: true){ |scope,value| value ? scope.published : scope.unpublished}end

Additional Argument Options

Sometimes 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}end

Accessing Parent Object

Sometimes 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}# ...end

If you need GraphQL context, it is accessible as context.

Enums Support

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'endend

Relay Support

Search objects can be used as Relay Connections:

classPostResolver < GraphQL::Schema::ResolverincludeSearchObject.module(:graphql)typePostType.connection_type,null: false# ...end
field:posts,resolver: PostResolver

Running tests

Make sure all dependencies are installed with bundle install

rake

Release

rake release

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Run the tests (rake)
  6. Create new Pull Request

Authors

See also the list of contributors who participated in this project.

License

MIT License

About

GraphQL plugin for SearchObject gem

Topics

Resources

Stars

161 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages