• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GraphQL plugin for SearchObject gem

Gem Version Code Climate Code 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:

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

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:

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

Default Values

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

Additional Argument Options

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

Accessing Parent Object

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.

Enums Support

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

Relay Support

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

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

More Repositories

1

SearchObject

Search object DSL
Ruby
180
star
2

FocusedTask

MacOS menu bar todo app built with Electron, React and Redux. It goals is to help you do deep work.
TypeScript
131
star
3

backbone-bind-to

Backbone.js extension for automatic binding and unbinding of model events to views.
CoffeeScript
81
star
4

backbone-handlebars

Mixing Backbone and Handlebars
CoffeeScript
78
star
5

MiniForm

Sugar around ActiveModel::Model
Ruby
29
star
6

config_files

My config files
Vim Script
20
star
7

talks-code

Demo code from my talks
JavaScript
15
star
8

RDSActionLabel

Custom text highlighting in UILabel
Swift
9
star
9

angular-simple-format

Angular directive for applying simple html formatting to text
8
star
10

graphql-playground-rails

A mountable GraphQL Playground endpoint for Rails
Ruby
7
star
11

playground

My playground for various javascript/css/ruby and other ideas
JavaScript
6
star
12

controldepo-3-widgets

A collection of widgets
JavaScript
6
star
13

rstankov_com

My Personal Website
TypeScript
5
star
14

CD3.UI.Upload

ControlDepo 3 UI Upload is multiple file uploader via flash, build on top of Prototype.js
ActionScript
4
star
15

ConferenceBox

CMS for conferences
Ruby
4
star
16

OpenFest-2010

My OpenFest 2010 presentation - JavaScript event-driven architecture - demo
Ruby
4
star
17

BackToScriptaculous

A backward lib between scriptaculous and scripty2
JavaScript
3
star
18

setty

Minimal application configuration for Rails projects.
Ruby
3
star
19

Taskar

Todo Project Manager, used for my diploma thesis
Ruby
2
star
20

toolbox

Files I copy from project to project
2
star
21

blog

My personal blog
CSS
2
star
22

BachkovoTheTurnaround

Sofia Game Jam project
Objective-C
1
star
23

cyoa

Technology playground project
Go
1
star
24

rstankov.github.io

rstankov.com
CSS
1
star
25

talk-angular-tips-tricks

Sample code for my Angular tips and tips talk
JavaScript
1
star
26

Backbone.js-brown-bag-seminar

Backbone.js demo. I created for a brown bag seminar
CoffeeScript
1
star
27

WLHeatMap

RSSI heatmap generator
Java
1
star
28

CoinBox

Hackathon project
Ruby
1
star
29

rails-angular-form-builder

Ruby
1
star
30

backbone-presentation

Slides and code from my Backbone presentation at initLab
JavaScript
1
star