Object Identification #Facebook Relay文档翻译#

原文地址
上一篇 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.
FactionShip都有标识符,我们可以用它来重新获取他们。我们通过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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值