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
)另請參閱參與專案的貢獻者清單。
麻省理工學院許可證