SearchObjectGraphQL
1.0.0
GraphQL Ruby 用の SearchObject プラグイン。
次の行をアプリケーションの Gemfile に追加します。
gem 'search_object_graphql'
そして、以下を実行します。
$ bundle
または、次のように自分でインストールします。
$ gem install search_object_graphql
プロジェクトで手動で要求する
require 'search_object'
require 'search_object/plugin/graphql'
SearchObject
>= 1.2Graphql
>= 1.5 変更は CHANGELOG.md で入手できます。
SearchObject.module
をインクルードし、検索オプションとそのタイプを定義するだけです。
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
次に、 PostResolver
GraphQL::Schema::Resolver として使用するだけです。
field :posts , resolver : PostResolver
オプションは、GraphQL クエリの引数として公開されます。
posts(name: 'Example') { ... }
posts(published: true) { ... }
posts(published: true, name: 'Example') { ... }
最も重要な機能とプラグインの例は、ここで見つけることができます。
検索オブジェクト自体とそのオプションを文書化できます。
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
場合によっては、追加のオプションをgraphql引数メソッドに渡す必要があります。
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
場合によっては、親オブジェクトに基づいて投稿のスコープを設定したい場合があります。親オブジェクトにはobject
プロパティとしてアクセスできます。
class PostResolver < GraphQL :: Schema :: Resolver
include SearchObject . module ( :graphql )
# lists only posts from certain category
scope { object . posts }
# ...
end
GraphQL コンテキストが必要な場合は、 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
検索オブジェクトはリレー接続として使用できます。
class PostResolver < GraphQL :: Schema :: Resolver
include SearchObject . module ( :graphql )
type PostType . connection_type , null : false
# ...
end
field :posts , resolver : PostResolver
すべての依存関係がbundle install
でインストールされていることを確認してください
rake
rake release
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)rake
)このプロジェクトに参加した貢献者のリストもご覧ください。
MITライセンス