@relay的目的(pattern:true)

Jar*_*rda 10 relayjs

新的表达@relay(pattern: true)是在推出更改日志中relay.js 0.5.

但是无法从描述中找出来,也不能测试它究竟是做什么以及什么时候我应该在写作时使用它fatQueries.

一些例子会非常有帮助.

ste*_*her 7

考虑如下的GraphQL查询:

viewer {
  friends(first: 10) {
    totalCount
    edges { node { name } }
    pageInfo { hasNextPage }
  }
}
Run Code Online (Sandbox Code Playgroud)

为继电器突变定义胖查询时,要包括字段名称而不指定其任何子字段,则告诉Relay该字段的任何子字段都可能因该突变而发生变化.

不幸的是,省略连接参数,例如find,firstlastfriends现场将导致连接参数的依赖字段验证错误edgespageInfo:

getFatQuery() {
  return Relay.QL`
    fragment on AddFriendMutationPayload {
      viewer { 
        friends { edges, pageInfo }  # Will throw the validation error below
      }
    }
  `;
}

// Uncaught Error: GraphQL validation/transform error ``You supplied the `pageInfo` 
// field on a connection named `friends`, but you did not supply an argument necessary 
// to do so. Use either the `find`, `first`, or `last` argument.`` in file 
// `/path/to/MyMutation.js`.
Run Code Online (Sandbox Code Playgroud)

您可以使用该@relay(pattern: true)指令指示要使用fat查询来模式匹配跟踪的查询,而不是将其用作完全成熟的查询.

getFatQuery() {
  return Relay.QL`
    fragment on AddFriendMutationPayload @relay(pattern: true) {
      viewer {
        friends { edges, pageInfo }  # Valid!
      }
    }
  `;
}
Run Code Online (Sandbox Code Playgroud)

有关突变的更多信息,请参阅:https://facebook.github.io/relay/docs/guides-mutations.html#content