【发布时间】:2016-09-17 11:22:47
【问题描述】:
我有一个使用 GraphQL Relay 连接的页面,它获取 drafts。
query {
connections {
drafts(first: 10) {
edges {
node {
... on Draft {
id
}
}
}
}
}
}
在这个页面,我也是通过CreateDraftMutation创建草稿的。
mutation {
createDraft(input: {
clientMutationId: "1"
content: "content"
}) {
draft {
id
content
}
}
}
在这个突变之后,我希望 Relay 将创建的草稿添加到它的存储中。变异配置的最佳候选者是 RANGE_ADD,记录如下:
https://facebook.github.io/relay/docs/guides-mutations.html
RANGE_ADD 给定一个父节点、一个连接以及响应负载中新创建的边的名称,Relay 会将节点添加到存储中,并根据指定的范围行为将其附加到连接。
参数
父名:字符串 响应中代表连接父级的字段名称
父ID:字符串 包含连接的父节点的DataID
连接名称:字符串 响应中表示连接的字段名称
edgeName: 字符串 响应中代表新创建边的字段名称
rangeBehaviors: {[call: string]: GraphQLMutatorConstants.RANGE_OPERATIONS}
按字母顺序打印的、以点分隔的 GraphQL 调用之间的映射,以及我们希望 Relay 在这些调用的影响下将新边添加到连接时表现出的行为。行为可以是“append”、“ignore”、“prepend”、“refetch”或“remove”之一。
文档中的示例如下:
class IntroduceShipMutation extends Relay.Mutation {
// This mutation declares a dependency on the faction
// into which this ship is to be introduced.
static fragments = {
faction: () => Relay.QL`fragment on Faction { id }`,
};
// Introducing a ship will add it to a faction's fleet, so we
// specify the faction's ships connection as part of the fat query.
getFatQuery() {
return Relay.QL`
fragment on IntroduceShipPayload {
faction { ships },
newShipEdge,
}
`;
}
getConfigs() {
return [{
type: 'RANGE_ADD',
parentName: 'faction',
parentID: this.props.faction.id,
connectionName: 'ships',
edgeName: 'newShipEdge',
rangeBehaviors: {
// When the ships connection is not under the influence
// of any call, append the ship to the end of the connection
'': 'append',
// Prepend the ship, wherever the connection is sorted by age
'orderby(newest)': 'prepend',
},
}];
}
/* ... */
}
如果父级和派系一样明显,这是小菜一碟,但如果它直接来自查询连接,我一直很难识别 parentName 和 parentID。
我该怎么做?
编辑:
这就是查询的导出方式。
export default new GraphQLObjectType({
name: 'Query',
fields: () => ({
node: nodeField,
viewer: {
type: viewerType,
resolve: () => ({}),
},
connections: {
type: new GraphQLObjectType({
name: 'Connections',
作为回报用于中继容器
export default Relay.createContainer(MakeRequestPage, {
fragments: {
connections: () => Relay.QL`
fragment on Connections {
【问题讨论】:
-
您的查询中的
connections是 GraphQL 对象还是连接? -
@AhmadFerdousBinAlam 我在下面添加了 sn-p。 'Connections' GraphQLObjectType 带有作为连接字段的字段。
标签: graphql relayjs graphql-js