原文地址
上一篇 GraphQL Relay Specification
Relay文档翻译目录
Both Faction
and Ship
have identifiers that we can use to refetch them. We
expose this capability to Relay through the Node
interface and the node
field on the root query type.
Faction
和 Ship
都有标识符,我们可以用它来重新获取他们。我们通过Node
接口和root query type上的node
字段来将这个能力暴露给Relay。
The Node
interface contains a single field, id
, which is a ID!
. The
node
root field takes a single argument, a ID!
, and returns a Node
.
These two work in concert to allow refetching; if we pass the id
returned in
that field to the node
field, we get the object back.
Node
接口包含一个单一的id
字段,它不能为空ID!
。root上的node
字段,接收一个不为空的ID作参数,返回一个Node
。他俩协调一致的工作使得重新获取对象成为可能。如果我们给node
字段传递id
,我们将得到对应对象。
Let’s see this in action, and query for the ID of the rebels:
我们来看如下实践,用于查询rebel的ID
query RebelsQuery {
rebels {
id
name
}
}
returns
{
"rebels": {
"id": "RmFjdGlvbjox",
"name": "Alliance to Restore the Republic"
}
}
So now we know the ID of the Rebels in our system. We can now refetch them:
现在我们知道了Rebel的ID,我们可以重新获取他们:
query RebelsRefetchQuery {
node(id: "RmFjdGlvbjox") {
id
... on Faction {
name
}
}
}
returns
{
"node": {
"id": "RmFjdGlvbjox",
"name": "Alliance to Restore the Republic"
}
}
If we do the same thing with the Empire, we’ll find that it returns a different
ID, and we can refetch it as well:
如果我们在Emipre上做同样的事情,它将返回不同的ID,我们同样可以重新获取它。
query EmpireQuery {
empire {
id
name
}
}
yields
{
"empire": {
"id": "RmFjdGlvbjoy",
"name": "Galactic Empire"
}
}
and
query EmpireRefetchQuery {
node(id: "RmFjdGlvbjoy") {
id
... on Faction {
name
}
}
}
yields
{
"node": {
"id": "RmFjdGlvbjoy",
"name": "Galactic Empire"
}
}
The Node
interface and node
field assume globally unique IDs for this
refetching. A system without globally unique IDs can usually synthesize them
by combining the type with the type-specific ID, which is what was done
in this example.
Node
接口和node
字段假设用的是全局的唯一ID。如果一个系统没有全局唯一ID,通常可以人工合成他们,可以根据该类型特定ID,这个例子中就是这么做的。
The IDs we got back were base64 strings. IDs are designed to be opaque (the
only thing that should be passed to the id
argument on node
is the
unaltered result of querying id
on some object in the system), and base64ing
a string is a useful convention in GraphQL to remind viewers that the string is
an opaque identifier.
我们获得的ID是base64的字符串。ID被设计为不透明的,唯一应该传递给node
的id参数是在本系统中用于查其他对象是所用的不可改变的id
。base64的字符串很好的提示了用户这个字符串是不透明的。
Complete details on how the server should behave are
available in the
GraphQL Object Identification spec.
服务端应该如何处理的详细介绍请见GraphQL Object Identification spec.