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
)另请参阅参与该项目的贡献者列表。
麻省理工学院许可证