gpt4 book ai didi

GraphQL 中继突变配置 RANGE_ADD 的连接父名称

转载 作者:行者123 更新时间:2023-12-04 15:55:01 25 4
gpt4 key购买 nike

我有一个使用 GraphQL 中继连接的页面,它获取 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 Given a parent, a connection, and the name of the newly created edge in the response payload Relay will add the node to the store and attach it to the connection according to the range behavior specified.

Arguments

parentName: string The field name in the response that represents the parent of the connection

parentID: string The DataID of the parent node that contains the connection

connectionName: string The field name in the response that represents the connection

edgeName: string The field name in the response that represents the newly created edge

rangeBehaviors: {[call: string]: GraphQLMutatorConstants.RANGE_OPERATIONS}

A map between printed, dot-separated GraphQL calls in alphabetical order, and the behavior we want Relay to exhibit when adding the new edge to connections under the influence of those calls. Behaviors can be one of 'append', 'ignore', 'prepend', 'refetch', or '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 {

最佳答案

I've been having hard time identifying parentName and parentID if it came directly from query connections.


factionships在 Relay 文档中的示例中与 connections 完全相同和 drafts在你的情况下。每个 GraphQLObject有 ID,你的 connections 也有 ID目的。因此,对于您的突变, parentNameconnctionsparentIDconnections的ID .
query {
connections {
id
drafts(first: 10) {
edges {
node {
... on Draft {
id
}
}
}
}
}
}

顺便说一句,我猜 connectionsdrafts是来自您的应用程序域的术语。否则, connections与 GraphQL 连接类型混淆。

关于GraphQL 中继突变配置 RANGE_ADD 的连接父名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37342541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com