• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

RStankov/SearchObjectGraphQL: GraphQL plugin for SearchObject gem

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

RStankov/SearchObjectGraphQL

开源软件地址(OpenSource Url):

https://github.com/RStankov/SearchObjectGraphQL

开源编程语言(OpenSource Language):

Ruby 100.0%

开源软件介绍(OpenSource Introduction):

Gem Version Code Climate Build Status Code coverage

SearchObject::Plugin::GraphQL

SearchObject plugin for GraphQL Ruby.

Table of Contents

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

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

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

License

MIT License




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
nfl/graphql-rxjava: GraphQL RxJava发布时间:2022-07-10
下一篇:
FormidableLabs/urql: The highly customizable and versatile GraphQL client with w ...发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap