ปลั๊กอิน 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
)ดูรายชื่อผู้ร่วมให้ข้อมูลที่เข้าร่วมโครงการนี้ด้วย
ใบอนุญาตเอ็มไอที