GraphQL

維基百科,自由的百科全書
跳至導覽 跳至搜尋
GraphQL
File:GraphQL Logo.svg
開發者Facebook及社群
首次釋出2015年9月14日 (2015-09-14)
目前版本2018年6月 (2018-06)[1]
原始碼庫github.com/facebook/graphql
程式語言JavaScriptRubyScala和其他各種語言中實現。
引擎
    Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
    作業系統跨平台
    特許條款
      Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
      網站graphql.org

      GraphQL是一個開源的,面向API而創造出來的數據查詢操作語言以及相應的執行環境。[2] 於2012年仍處於Facebook內部開發階段,直到2015年才公開發布。 2018年11月7日,Facebook將GraphQL專案轉移到新成立的GraphQL基金會(隸屬於非營利性的Linux基金會)。[3][4]

      GraphQL相較於REST以及其他web service架構提供了一種更加高效、強大和靈活的開發web APIs的方式。它通過由客戶端根據所需定義數據結構,同時由伺服器端負責返回相同數據結構的對應數據的方式避免了伺服器端大量冗餘數據的返回,但與此同時也意味着這種方式不能有效利用起查詢結果的web快取。GraphQL這種查詢語言所帶來的靈活性和豐富性的同時也增加了複雜性,導致簡單的APIs有可能並不適合這種方式。[5][6][7]

      GraphQL支援數據讀取、寫入(操作)和數據變更訂閱(即時更新)。[8]

      主要的GraphQL客戶端有Apollo Client[9] 和 Relay.[10] GraphQL的伺服器端在多個語言都有實現包括Haskell, JavaScript, Python,[11] Ruby, Java, C#, Scala, Go, Elixir,[12] Erlang, PHP, R,和 Clojure.

      2018年2月9日GraphQL的部分模式定義語言(SDL)規範制定完成。

      例子[編輯]

      POST請求:

      {
          orders {
              id
              productsList {
                  product {
                      name
                      price
                  }
                  quantity
              }
              totalAmount
          }
      }
      

      響應:

      {
          "data": {
              "orders": [
                  {
                      "id": 1,
                      "productsList": [
                          {
                              "product": {
                                  "name": "orange",
                                  "price": 1.5
                              },
                              "quantity": 100
                          }
                      ],
                      "totalAmount": 150
                  }
              ]
          }
      }
      

      參見[編輯]

      參考文獻[編輯]

      1. ^ GraphQL June 2018 Release Notes. [26 March 2019]. (原始內容存檔於2020-10-16). 
      2. ^ GraphQL: A query language for APIs.. [2018-11-08]. (原始內容存檔於2016-04-26). 
      3. ^ Facebook’s GraphQL gets its own open-source foundation. TechCrunch. [2018-11-07]. (原始內容存檔於2019-01-22) (en-US). 
      4. ^ The Linux Foundation Announces Intent to Form New Foundation to Support GraphQL - The Linux Foundation. The Linux Foundation. 2018-11-06 [2018-11-07]. (原始內容存檔於2019-01-28) (en-US). 
      5. ^ GraphQL vs REST: Overview. Phil Sturgeon. [2018-11-25]. (原始內容存檔於2019-04-08) (British English). 
      6. ^ Why use GraphQL, good and bad reasons. Honest Engineering. 2018-08-04 [2018-11-26]. (原始內容存檔於2018-11-26) (en-US). 
      7. ^ GraphQL Fundamentals. Howto GraphQL. [2018-07-04]. (原始內容存檔於2018-07-05). 
      8. ^ GraphQL. facebook.github.io. Facebook. [2018-07-04]. (原始內容存檔於2018-07-18). 
      9. ^ Apollo Client: The flexible, production ready GraphQL client for React, and all JavaScript and native apps. [2019-01-24]. (原始內容存檔於2018-07-20). 
      10. ^ Relay: A JavaScript framework for building data-driven React applications. [2019-01-24]. (原始內容存檔於2019-01-29). 
      11. ^ Graphene. graphene-python.org. [2017-06-18]. (原始內容存檔於2015-10-12). 
      12. ^ Absinthe: The GraphQL toolkit for Elixir. [2018-07-19]. (原始內容存檔於2018-07-06). 

      外部連結[編輯]