在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):RStankov/SearchObjectGraphQL开源软件地址(OpenSource Url):https://github.com/RStankov/SearchObjectGraphQL开源编程语言(OpenSource Language):Ruby 100.0%开源软件介绍(OpenSource Introduction):SearchObject::Plugin::GraphQLSearchObject plugin for GraphQL Ruby. Table of ContentsInstallationAdd this line to your application's Gemfile: gem 'search_object_graphql' And then execute:
Or install it yourself as:
Require manually in your project require 'search_object'
require 'search_object/plugin/graphql' Dependencies
UsageJust include the 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 field :posts, resolver: PostResolver Options are exposed as arguments in the GraphQL query:
ExampleYou can find example of most important features and plugins - here. FeaturesDocumentationSearch 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 Valuesclass 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 OptionsSometimes 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 ObjectSometimes you want to scope posts based on parent object, it is accessible as 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 Enums Supportclass 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 SupportSearch 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
License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论