本篇文章給大家?guī)?lái)了關(guān)于GraphQL的相關(guān)知識(shí),其中主要跟大家聊一聊有關(guān)基于webman的GraphQL實(shí)現(xiàn),感興趣的朋友下面一起來(lái)看一下吧,希望對(duì)大家有幫助。
基于PHP的GraphQL由于加載指令(directive)和解析schema的開(kāi)銷較大,性能不是很理想。webman是常駐內(nèi)存的,所以GraphQL在webman上的性能表現(xiàn)非常不錯(cuò)。
GrahpQL 是基于YiAdmin的一個(gè)模塊,用于快速創(chuàng)建GraphQL服務(wù),可以開(kāi)啟多個(gè)服務(wù),模塊內(nèi)置了多個(gè)指令用于快速開(kāi)發(fā)api接口。
調(diào)試接口地址:/graphql-dev/api/服務(wù)名稱
正式接口地址:/graphql-api/服務(wù)名稱
后臺(tái)接口管理可以建立接口名稱與Query的映射關(guān)系,通過(guò)接口名稱訪問(wèn)以簡(jiǎn)化前端輸入,curl -X POST -d "{"variables": VARIABLES}" -H "Content-type:application/json" "HOST/graphql-api/SERVER_NAME?api=接口名稱"
零依商城 是基于YiAdmin的uniapp商城系統(tǒng),Api接口基于GrahpQL進(jìn)行了重構(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] }
通過(guò)模型獲取記錄,支持模型 scope
// Query "通過(guò)文章ID獲取文章" article( "文章ID" id: Int! @eq ): Article @model(name: "\app\test\model\api\ArticleModel", scopes: ["published"]) @find
支持分頁(yè) 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時(shí)間格式化、validate驗(yàn)證器、water打碼脫敏、upper轉(zhuǎn)大寫(xiě)、lower轉(zhuǎn)小寫(xiě)等各種指令。
推薦學(xué)習(xí):《PHP視頻教程》
站長(zhǎng)資訊網(wǎng)