On this page, you'll learn in detail about how to query a GraphQL server.

在这个页面,你将会学习更多的关于如何查询GraphQl服务。

Fields

At its simplest, GraphQL is about asking for specific fields on objects. Let's start by looking at a very simple query and the result we get when we run it:

最简单的来说,GraphQl是在Object上请求特定的fields。让我们查看一个非常简单的查询和我们运行它的时候得到的结果:

//查询方法
{
hero {
name
}
}
//运行结果
{
"data": {
"hero": {
"name": "R2-D2"
}
}
}

You can see immediately that the query has exactly the same shape as the result. This is essential to GraphQL, because you always get back what you expect, and the server knows exactly what fields the client is asking for.

我们可以看到查询与结果的形状非常相似。这对于GraphQl是必需的,因为你总是得到你期望的,服务器端完全知道客户端请求的fields.

The field name returns a String type, in this case the name of the main hero of Star Wars,"R2-D2".

name字段返回一个String类型,在这个例子星际战争中主要的英雄的名字:R2-D2

Oh, one more thing - the query above is interactive. That means you can change it as you like and see the new result. Try adding an appearsIn field to the hero object in the query, and see the new result.

此外,上面的查询是交互性的。这也意味着你可以按你自己的意愿去改变他并且得到他的结果。尝试为hero的Object添加一个appearsIn字段,并且查看他的新的结果。

In the previous example, we just asked for the name of our hero which returned a String, but fields can also refer to Objects. In that case, you can make a sub-selection of fields for that object. GraphQL queries can traverse related objects and their fields, letting clients fetch lots of related data in one request, instead of making several roundtrips as one would need in a classic REST architecture.

 在之前的例子中,我们仅仅是查询英雄的名字(返回一个String类型),但是fields同样可以是一个Object.在这个例子中,你可以为这个Object创建一个子部。GraphQl可以便利相关的Object以及他们的fields,假如客户端在一个请求中获取很多相关的数据,而不是像在传统的REST结构中进行好几次往返。
//查询
{
hero {
name
# Queries can have comments!
friends {
name
}
}
}
//结果
{
"data": {
"hero": {
"name": "R2-D2",
"friends": [
{
"name": "Luke Skywalker"
},
{
"name": "Han Solo"
},
{
"name": "Leia Organa"
}
]
}
}
}

Note that in this example, the friends field returns an array of items. GraphQL queries look the same for both single items or lists of items, however we know which one to expect based on what is indicated in the schema.

注意在这个例子中,friends fields返回一个数组。GraphQl查询返回单个或一个集合看起来是相似的,然而,我们知道期望哪个是基于schema定义的。

查询和修改(Queries and Mutations)的更多相关文章

  1. LINQ之路 8: 解释查询(Interpreted Queries)

    LINQ提供了两个平行的架构:针对本地对象集合的本地查询(local queries),以及针对远程数据源的解释查询(Interpreted queries). 在讨论LINQ to SQL等具体技术 ...

  2. InfluxDB学习之InfluxDB连续查询(Continuous Queries)

    在上一篇:InfluxDB学习之InfluxDB数据保留策略(Retention Policies) 中,我们介绍了 InfluxDB的数据保留策略,数据超过保存策略里指定的时间之后,就会被删除. 但 ...

  3. MVC的EF编辑,不用查询直接修改

    EF中会为每个 管理的 实体对象 创建一个代理包装类对象,其中会跟踪 实体对象 的状态和每个属性的状态: 一.通常使用EF更新的方式,先查询出要修改的数据,然后再修改新的值:实体对象被修改的属性 在 ...

  4. SQL练习之不破坏应用程序现有查询的修改模式

    当我还是一个菜鸟的时候,当然现在也是,当我的软件需求发生变化时,并且数据库设计同样要求发生变化,我通常会放弃原有的代码(或者对原有的代码进行大改),先在我知道了两个不破坏应用程序现有查询的修改模式,下 ...

  5. 连续查询(Continuous Queries)

    当数据超过保存策略里指定的时间之后,就会被删除.如果我们不想完全删除掉,比如做一个数据统计采样:把原先每秒的数据,存为每小时的数据,让数据占用的空间大大减少(以降低精度为代价). 这就需要Influx ...

  6. 查询和修改mysql最大连接数的方法

    查询和修改mysql最大连接数的方法切换到mysql库里查询show variables like 'max_connections';show global status like 'Max_use ...

  7. Java对MySQL数据库进行连接、查询和修改(转)

    Java对MySQL数据库进行连接.查询和修改 0. 一般过程: (1) 调用Class.forName()方法加载驱动程序. (2) 调用DriverManager对象的getConnection( ...

  8. MYSQL语句:创建、授权、查询、修改、统计分析等 一 用户的创建、权限设置、删除等

    MYSQL语句:创建.授权.查询.修改.统计分析.. 一.用户的创建.权限设置.删除等 1.首先链接MySQL操作 连接格式:mysql -h 主机地址 -u 用户名 -p 用户密码 (注-u与roo ...

  9. 【层次查询】Hierarchical Queries之亲兄弟间的排序(ORDER SIBLINGS BY)

    http://blog.itpub.net/519536/viewspace-624176 有关层次查询之前的文章参考如下. [层次查询]Hierarchical Queries之"树的遍历 ...

随机推荐

  1. 在Myeclipse中将maven程序部署到tomcat中

    用新版的m2e插件就可以了,而且发布后修改jsp是不需要重新发布的.MyEclipse10.6自带新版m2e,只需在Run Configuration的Maven Build中new一个输入confi ...

  2. Java 注释说明

    注释 什么是注释呢?就是标注解释的意思,主要用来对Java代码进行说明.Java中有三种注释方式 (1):// :注释单行语句 示例: //定义一个值为10的int变量 int a = 10; (2) ...

  3. Unity3d Asset Server启动问题

    周末机房停电后asset server无法启动,点击启动出现“asset server could not start server”. 几经周折,找到原来是用户问题,解决办法如下: 1.命令行输入“ ...

  4. System V IPC(1)-消息队列

    一.概述                                                    System V三种IPC:消息队列,信号量,共享内存.这三种IPC最先出现在AT&am ...

  5. No 'Access-Control-Allow-Origin' header is present on the requested resource.

    今天做一个AJAX案例时,浏览器监控到如下错误: XMLHttpRequest cannot load http://54.169.69.60:8081/process_message. No 'Ac ...

  6. GTAC 2015 Schedule

    之前发的GTAC 2015将于11月10号和11号召开 现在时刻表也出来啦 https://developers.google.com/google-test-automation-conferenc ...

  7. tiny6410在I2c用户态中的程序设计eeprom

    在读写的过程中,发现写数据成功但是读取数据却失败,猜测是因为iic的读写操作过快,故在写操作后给一定的延迟,进而读写成功. 代码如下: #include <stdio.h>#include ...

  8. [No000066]python各种类型转换-int,str,char,float,ord,hex,oct等

    int(x [,base ]) #将x转换为一个整数 long(x [,base ]) #将x转换为一个长整数 float(x ) #将x转换到一个浮点数 complex(real [,imag ]) ...

  9. stl学习(三)crope的用法

    转载自http://blog.csdn.net/iamzky/article/details/38348653 曾经我不会写平衡树……于是在STL中乱翻……学到了pb_ds库中的SXBK的斐波那契堆. ...

  10. php多进程刷票

    $processNum=20; for($i=1;$i<=$processNum;$i++){ $pid=pcntl_fork(); if($pid==-1){ //todo log }else ...