graphql

  • What is GraphQL

    什么是GraphQL

  • How it works

    这个怎么运作

  • GraphQL Queries

    GraphQL查询

    • Fields and arguments

      字段和参数

    • Aliases

      别名

    • Fragments

      碎片

    GraphQL Queries

    GraphQL查询

  • GraphQL Variables

    GraphQL变量

    • Making variables required

      使变量为必需

    • Specifying a default value for a variable

      指定变量的默认值

    GraphQL Variables

    GraphQL变量

  • GraphQL Directives

    GraphQL指令

    • @include(if: Boolean)

      @include(如果:布尔值)

    • @skip(if: Boolean)

      @skip(如果是布尔值)

    GraphQL Directives

    GraphQL指令

什么是GraphQL (What is GraphQL)

GraphQL is the new frontier in APIs (Application Programming Interfaces) design, and in how we build and consume them.

GraphQL是API( 应用程序编程接口 )设计以及我们如何构建和使用它们的新领域。

It’s a query language, and a set of server-side runtimes (implemented in various backend languages) for executing queries. It’s not tied to a specific technology, but you can implement it in any language.

它是一种查询语言,还有一组用于执行查询的服务器端运行时(以各种后端语言实现)。 它与特定技术无关,但是您可以使用任何语言来实现它。

It is a methodology that directly competes with REST (REpresentational State Transfer) APIs, much like REST competed with SOAP at first.

它是一种直接与REST ( 代表性状态转移 )API 竞争的方法,就像REST首先与SOAP竞争一样。

And as we’ll see, it’s very different from REST. It creates a whole new dimension for API design.

正如我们将看到的,它与REST有很大的不同。 它为API设计创建了一个全新的维度。

GraphQL was developed at Facebook, like many of the technologies that are shaking the JavaScript world lately, like React and React Native, and it was publicly launched in 2015 - although Facebook used it internally for a few years before.

GraphQL是在Facebook上开发的 ,就像最近撼动JavaScript世界的许多技术一样,例如React和React Native,它于2015年公开发布 -尽管Facebook在内部使用了几年。

Many big companies are adopting GraphQL beside Facebook, including GitHub, Pinterest, Twitter, Sky, The New York Times, Shopify, Yelp and thousands many other.

许多大公司都在Facebook旁边采用GraphQL,包括GitHub,Pinterest,Twitter,Sky,《纽约时报》,Shopify,Yelp等数千种。

I’ve first been in touch with GraphQL when GitHub decided to implement the v4 of their API using that technology, and I joined their beta program. That’s when I discovered it’s a game changer in many aspects.

当GitHub决定使用该技术实现其API的v4时,我第一次与GraphQL联系,我加入了他们的beta程序。 从那时起,我发现它在许多方面都改变了游戏规则。

这个怎么运作 (How it works)

GraphQL exposes a single endpoint from your server.

GraphQL从您的服务器公开一个端点

You send a query to that endpoint by using a special Query Language syntax. That query is just a string.

可以使用特殊的查询语言语法将查询发送到该端点 。 该查询只是一个字符串

The server responds to a query by providing a JSON object.

服务器通过提供JSON对象来响应查询。

Let’s see a first example of such a query. This query gets the name of a person with id=1:

让我们看看这种查询的第一个例子。 此查询获取id=1的人的名字:

GET /graphql?query={ person(id: "1") { name } }

or:

要么:

{person(id: "1") {name}
}

We’ll get this JSON response back:

我们将返回此JSON响应:

{"name": "Tony"
}

Let’s add a bit more complexity: we get the name of the person, and the city where the person lives, by extracting it from the address object. We don’t care about other details of the address, and the server does not return them back to us because we didn’t ask for them:

让我们增加一点复杂性:通过从address对象中提取人的名字,以及人的居住城市。 我们不在乎地址的其他详细信息,并且服务器不会将它们退还给我们,因为我们没有要求它们:

GET /graphql?query={ person(id: "1") { name, address { city } } }

or

要么

{person(id: "1") {nameaddress {city}}
}

This is what we get back:

这就是我们得到的:

{"name": "Tony","address": {"city": "York"}
}

As you can see the data we get is basically the same structure of the request we sent, filled with values that were fetched.

如您所见,我们获取的数据基本上与我们发送的请求的结构相同,其中填充了所获取的值。

GraphQL查询 (GraphQL Queries)

In this section you’ll learn how is a GraphQL query composed.

在本节中,您将学习如何构成GraphQL查询。

The concepts I’ll introduce are

我将介绍的概念是

  • fields and arguments字段和参数
  • aliases别名
  • fragments碎片

字段和参数 (Fields and arguments)

Take this simple GraphQL query:

进行以下简单的GraphQL查询:

{person(id: "1") {name}
}

In this query you see 2 fields, person and name, and 1 argument.

在此查询中,您看到2个字段personname ,以及1个参数

The field person returns an Object which has another field in it, a String.

现场person返回其中包含另一个字段StringObject

The argument allows us to specify which person we want to reference. We pass an id, but we could as well pass a name argument, if the API we talk to has the option to find a person by name.

该参数允许我们指定要引用的人。 我们传递一个id ,但是我们也可以传递一个name参数,如果与我们交谈的API可以选择按名称查找人。

Arguments are not limited to any particular field. We could have a friends field in person that lists the friends of that person, and it could have a limit argument, to specify how many we want the API to return:

参数不限于任何特定字段。 我们可以person一个friends字段,其中列出该person的朋友,并且可以有一个limit参数,以指定我们希望API返回多少:

{person(id: "1") {namefriends(limit: 100)}
}

别名 (Aliases)

You can ask the API to return a field with a different name. For example here you request the name field, but you want it returned as fullname:

您可以要求API返回具有不同名称的字段。 例如,您在此处请求name字段,但希望它以fullname name返回:

{owner: person(id: "1") {fullname: name}
}

will return

将返回

{"data": {"owner": {"fullname": "Tony"}}
}

This feature, beside creating more ad-hoc naming for your client code, in case you need, is the only thing that can make the query work if you need to reference the same endpoint 2 times in the same query:

如果需要,此功能除了为您的客户端代码创建更多临时命名之外,如果您需要在同一查询中两次引用相同的端点 ,则该功能也是唯一可以使查询正常工作的功能:

{owner: person(id: "1") {fullname: name}first_employee: person(id: "2") {fullname: name}
}

碎片 (Fragments)

In the above query we replicated the person structure. Fragments allow us to specify the structure just once (a very useful thing when you have many similar fields):

在上面的查询中,我们复制了人员结构。 片段允许我们只指定一次结构(当您有许多相似字段时,这非常有用):

{owner: person(id: "1") {...personFields}first_employee: person(id: "2") {...personFields}
}fragment personFields on person {fullname: name
}

GraphQL变量 (GraphQL Variables)

More complex GraphQL queries need to use variables, a way to dynamically specify a value that is used inside a query.

更复杂的GraphQL查询需要使用变量 ,这是一种动态指定在查询中使用的的方法。

In this case we added the person id as a string inside the query:

在这种情况下,我们将人员ID作为字符串添加到查询中:

{owner: person(id: "1") {fullname: name}
}

The id will most probably change dynamically in our program, so we need a way to pass it, and not with string interpolation.

id很可能会在我们的程序中动态更改,因此我们需要一种传递它的方法,而不是使用字符串插值

With variables, the same query can be written as this:

使用变量,可以这样编写相同的查询:

query GetOwner($id: String) {owner: person(id: $id) {fullname: name}
}{"id": "1"
}

In this snippet we have assigned the GetOwner name to our query. Think of it as named functions, while previously you had an anonymous function. Named queries are useful when you have lots of queries in your application.

在此代码段中,我们已将GetOwner名称分配给我们的查询。 可以将其视为命名函数,而以前您拥有匿名函数。 当您的应用程序中有很多查询时,命名查询很有用。

The query definition with the variables looks like a function definition, and it works in an equivalent way.

具有变量的查询定义看起来像函数定义,并且以等效方式工作。

使变量为必需 (Making variables required)

Appending a ! to the type:

附加一个! 类型:

query GetOwner($id: String!)

instead of $id: String will make the $id variable required.

而不是$id: String将使$ id变量成为必需。

指定变量的默认值 (Specifying a default value for a variable)

You can specify a default value using this syntax:

您可以使用以下语法指定默认值:

query GetOwner($id: String = "1")

GraphQL指令 (GraphQL Directives)

Directives let you include or exclude a field if a variable is true or false.

指令允许您在变量为true或false时包含或排除字段。

query GetPerson($id: String) {person(id: $id) {fullname: name,address: @include(if: $getAddress) {citystreetcountry}}
}{"id": "1","getAddress": false
}

In this case if getAddress variable we pass is true, we also get the address field, otherwise not.

在这种情况下,如果我们传递的getAddress变量为true,那么我们还将获取地址字段,否则返回。

We have 2 directives available: include, which we have just seen (includes if true), and skip, which is the opposite (skips if true)

我们有2个可用的指令: include ,我们刚刚看过(如果为true则包含),而skip ,则相反(如果为true则跳过)

@include(如果:布尔值) (@include(if: Boolean))

query GetPerson($id: String) {person(id: $id) {fullname: name,address: @include(if: $getAddress) {citystreetcountry}}
}{"id": "1","getAddress": false
}

@skip(如果是布尔值) (@skip(if: Boolean))

query GetPerson($id: String) {person(id: $id) {fullname: name,address: @skip(if: $excludeAddress) {citystreetcountry}}
}{"id": "1","excludeAddress": false
}

翻译自: https://flaviocopes.com/graphql/

graphql

graphql_GraphQL简介相关推荐

  1. etcd 笔记(01)— etcd 简介、特点、应用场景、常用术语、分布式 CAP 理论、分布式原理

    1. etcd 简介 etcd 官网定义: A highly-available key value store for shared configuration and service discov ...

  2. Docker学习(一)-----Docker简介与安装

    一.Docker介绍 1.1什么是docker Docker是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源 Docker可以让开发者打包他们的应用以及依赖包到一个轻量级,可移植 ...

  3. 【Spring】框架简介

    [Spring]框架简介 Spring是什么 Spring是分层的Java SE/EE应用full-stack轻量级开源框架,以IOC(Inverse Of Control:反转控制)和AOP(Asp ...

  4. TensorRT简介

    TensorRT 介绍 引用:https://arleyzhang.github.io/articles/7f4b25ce/ 1 简介 TensorRT是一个高性能的深度学习推理(Inference) ...

  5. 谷粒商城学习笔记——第一期:项目简介

    一.项目简介 1. 项目背景 市面上有5种常见的电商模式 B2B.B2C.C2B.C2C.O2O B2B 模式(Business to Business),是指商家和商家建立的商业关系.如阿里巴巴 B ...

  6. 通俗易懂的Go协程的引入及GMP模型简介

    本文根据Golang深入理解GPM模型加之自己的理解整理而来 Go协程的引入及GMP模型 一.协程的由来 1. 单进程操作系统 2. 多线程/多进程操作系统 3. 引入协程 二.golang对协程的处 ...

  7. Linux 交叉编译简介

    Linux 交叉编译简介 主机,目标,交叉编译器 主机与目标 编译器是将源代码转换为可执行代码的程序.像所有程序一样,编译器运行在特定类型的计算机上,输出的新程序也运行在特定类型的计算机上. 运行编译 ...

  8. TVM Operator Inventory (TOPI)简介

    TOPI简介 这是 TVM Operator Inventory (TOPI) 的介绍.TOPI 提供了比 TVM 具有更高抽象的 numpy 风格的,通用操作和调度.TOPI 如何在 TVM 中,编 ...

  9. 计算机视觉系列最新论文(附简介)

    计算机视觉系列最新论文(附简介) 目标检测 1. 综述:深度域适应目标检测标题:Deep Domain Adaptive Object Detection: a Survey作者:Wanyi Li, ...

最新文章

  1. elasticsearch 查询模板
  2. Windows 8.1 新增控件之 DatePicker
  3. hexo的yelee主题中的标签字体大小的修改
  4. 向SAML响应中添加自定义声明–(如何为WSO2 Identity Server编写自定义声明处理程序)...
  5. Vijos P1196吃糖果游戏[组合游戏]
  6. 1 使用WPE工具分析游戏网络封包
  7. bpsk调制matlab,MATLAB仿真-BPSK调制.doc
  8. Java 简单工厂模式
  9. 如何实现网易公开课的倍速播放?
  10. 将字符串中的大写字母转换为小写字母
  11. get请求400错误 vue_VUE 配置proxy代理后,前台报错400 bad request
  12. 数据挖掘常用算法整理
  13. 二元对数正态分布 (bivariate lognormal distribution) 的几个性质
  14. 【NAS】Samba配置文件解析
  15. 还在付费下论文吗?快来跟我一起白piao知网
  16. 眼图测试(信号完整性测试)-嵌入式多媒体卡eMMC存储芯片
  17. Nexus搭建私服(记录)
  18. 中企故事汇:铁匠之乡借东风出海
  19. 杰理6905A更改蓝牙名字
  20. 若不用计算机tan35 怎么算,三角函数计算器-三角函数计算器

热门文章

  1. Javascript中删除数组中重复出现的元素
  2. 开发管理---配置管理与文档管理
  3. 融入动画技术的粒子效果文字动画交互应用
  4. 60万奖金“人脸攻防大战”,全部进阶妙招奉上丨独家公开课实录(3)
  5. opencv之图像翻转、平移、缩放、旋转、仿射学习笔记
  6. ChatGPT:从问题解答到赚钱
  7. 2021-04-29 微信登录简易版
  8. 智付科技集团2018全球合作伙伴大会成功举办 5大战略布局首度公开
  9. 无人驾驶引发的变革比想象的更快,留给车企和老司机的时间已不多
  10. matlab极坐标系给定圆心画圆,matlab画极坐标