البرنامج المساعد 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
)انظر أيضًا قائمة المساهمين الذين شاركوا في هذا المشروع.
رخصة معهد ماساتشوستس للتكنولوجيا