本篇文章給大家?guī)砹岁P(guān)于GraphQL的相關(guān)知識,其中主要跟大家聊一聊有關(guān)基于webman的GraphQL實現(xiàn),感興趣的朋友下面一起來看一下吧,希望對大家有幫助。
基于PHP
的GraphQL
由于加載指令(directive
)和解析schema
的開銷較大,性能不是很理想。webman
是常駐內(nèi)存的,所以GraphQL
在webman
上的性能表現(xiàn)非常不錯。
GrahpQL 是基于YiAdmin
的一個模塊,用于快速創(chuàng)建GraphQL
服務(wù),可以開啟多個服務(wù),模塊內(nèi)置了多個指令用于快速開發(fā)api
接口。
調(diào)試接口地址:/graphql-dev/api/服務(wù)名稱
正式接口地址:/graphql-api/服務(wù)名稱
后臺接口管理
可以建立接口名稱與Query
的映射關(guān)系,通過接口名稱訪問以簡化前端輸入,curl -X POST -d "{"variables": VARIABLES}" -H "Content-type:application/json" "HOST/graphql-api/SERVER_NAME?api=接口名稱"
零依商城 是基于YiAdmin
的uniapp商城系統(tǒng),Api接口基于GrahpQL
進行了重構(gòu)。
例如有如下 schema
// Type type Article { id: Int category_id: Int title: String description: String created_at: Int create_time: String @alias(key: "created_at") @date status: Int } type ArticlePagination { pagination: Pagination data: [Article] }
通過模型獲取記錄,支持模型 scope
// Query "通過文章ID獲取文章" article( "文章ID" id: Int! @eq ): Article @model(name: "\app\test\model\api\ArticleModel", scopes: ["published"]) @find
支持分頁 paginate
articles: ArticlePagination @model(name: "\app\test\model\api\ArticleModel", scopes: ["published"]) @paginate(perPage: 15)
查詢條件 where
articles( title: String ): ArticlePagination @model(name: "\app\test\model\api\ArticleModel", scopes: ["published"]) @where(value: { title: ["like", "$title"] }) @paginate(perPage: 15)
延遲加載 defer
// Type type article { ... category: Category @defer(resolver: "\app\test\loaders\Cms@getCategoryById", keys: "category_id") } type Category { id: Int parent_id: Int title: String parent: Category @defer(resolver: "\app\test\loaders\Cms@getCategoryById", keys: "parent_id") }
除此以外,還有包括auth
權(quán)限管理、resolver
自定義處理方法、date
時間格式化、validate
驗證器、water
打碼脫敏、upper
轉(zhuǎn)大寫、lower
轉(zhuǎn)小寫等各種指令。
推薦學(xué)習(xí):《PHP視頻教程》