1. GraphQL介绍

GraphQL 是来自 facebook应用层查询语言。通过使用GraphQL,你可以基于 模式定义你的后端。随后,客户端就可以根据需求查询你的数据集了。

GraphQL Overview

因此,您不必因为客户端数据需求的变更而去修改你的后端。这从根本上解决了管理REST API中的一个最大的问题。

GraphQL还能让客户端程高效地批量获取数据。看一看下面GraphQL请求的🌰:

{
  latestPost {
    _id,
    title,
    content,
    author {
      name
    },
    comments {
      content,
      author {
        name
      }
    }
  }
}

这次 GraphQL 查询用于获取博客文章以及评论的相关信息。下面是请求返回结果:

{
  "data": {
    "latestPost": {
      "_id": "03390abb5570ce03ae524397d215713b",
      "title": "New Feature: Tracking Error Status with Kadira",
      "content": "Here is a common feedback we received from our users ...",
      "author": {
        "name": "Pahan Sarathchandra"
      },
      "comments": [
        {
          "content": "This is a very good blog post",
          "author": {
            "name": "Arunoda Susiripala"
          }
        },
        {
          "content": "Keep up the good work",
          "author": {
            "name": "Kasun Indi"
          }
        }
      ]
    }
  }
}

如果你使用的是REST,那么你需要调用多个REST API来收集这些信息。

GraphQL 是一种规范。

因此,它可以应用于任何平台或者语言。 它有一个由Facebook维护的基于JavaScript的参考实现。 还有由社区维护的其他语言的实现

这是规范文档: <http://facebook.github.io/graphql/>

你一旦已经尝试过GraphQL,你会想要将他应用到你的每一个项目中。

results matching ""

    No results matching ""