relay/graphql 查询中的 3 dots/periods/ellipsis 是什么意思?

What do 3 dots/periods/ellipsis in a relay/graphql query mean?

relay docs 包含此片段:

query RebelsRefetchQuery {
  node(id: "RmFjdGlvbjox") {
    id
    ... on Faction {
      name
    }
  }
}

语法上的 ... on Faction 是什么意思?

啊。解释 here:

Fragments are consumed by using the spread operator (...). All fields selected by the fragment will be added to the query field selection at the same level as the fragment invocation. This happens through multiple levels of fragment spreads.

与片段相关的...有两种用法。

通过引用合并片段

query Foo {
  user(id: 4) {
    ...userFields
  }
}

fragment userFields on User {
  name
}

具有将片段中的字段组合到嵌入查询中的效果:

query Foo {
  user(id: 4) {
    name
  }
}

请注意,片段可以组成其他片段。

内联片段

来自文档:

If you are querying a field that returns an interface or a union type, you will need to use inline fragments to access data on the underlying concrete type.

https://graphql.org/learn/queries/#inline-fragments

这些可用于以 type-dependent 方式组合字段。例如:

query Foo {
  profile(id: $id) {
    url
    ... on User {
      homeAddress
    }
    ... on Business {
      address
    }
  }
}

在此示例中,服务器将根据请求的对象是User 还是UserBusiness.