GraphQL

本页使用了标题或全文手工转换
维基百科,自由的百科全书
GraphQL
開發者Facebook及社区
首次发布2015年9月14日 (2015-09-14)
源代码库github.com/facebook/graphql
编程语言JavaScriptRubyScala和其他各种语言中实现。
操作系统跨平台
网站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) (美国英语). 
  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) (美国英语). 
  5. ^ GraphQL vs REST: Overview. Phil Sturgeon. [2018-11-25]. (原始内容存档于2019-04-08) (英国英语). 
  6. ^ Why use GraphQL, good and bad reasons. Honest Engineering. 2018-08-04 [2018-11-26]. (原始内容存档于2018-11-26) (美国英语). 
  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). 

外部链接