Плагин SearchObject для GraphQL Ruby.
Добавьте эту строку в 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