GraphQL Relay Mutation配置RANGE_ADD的连接的parentName

lus*_*nte 6 graphql graphql-js relayjs

我有一个使用GraphQL中继连接的页面drafts.

query {
    connections {
        drafts(first: 10) {
            edges {
                node {
                    ... on Draft {
                        id
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个页面中,我还创建了草稿CreateDraftMutation.

mutation {
    createDraft(input: {
        clientMutationId: "1"
        content: "content"
    }) {
        draft {
            id
            content
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此突变之后,我希望Relay将创建的草稿添加到其商店中.变异配置的最佳候选者是RANGE_ADD,其记录如下:

https://facebook.github.io/relay/docs/guides-mutations.html

RANGE_ADD给定父级,连接以及响应有效负载中新创建的边的名称中继将根据指定的范围行为将节点添加到存储并将其附加到连接.

参数

parentName:string响应中表示连接父级的字段名称

parentID:string包含连接的父节点的DataID

connectionName:string响应中表示连接的字段名称

edgeName:string响应中表示新创建的边的字段名称

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

按字母顺序打印的以点分隔的GraphQL调用之间的映射,以及在这些调用的影响下将新边添加到连接时我们希望Relay显示的行为.行为可以是"追加","忽略","前置","重新获取"或"删除"之一.

文档中的示例如下:

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',
      },
    }];
  }
  /* ... */
}
Run Code Online (Sandbox Code Playgroud)

如果父母和派系一样明显,这是小菜一碟,但如果它直接来自查询连接,我一直很难识别parentName和parentID.

我该怎么做呢?

编辑:

这是查询的导出方式.

export default new GraphQLObjectType({
  name: 'Query',
  fields: () => ({
    node: nodeField,
    viewer: {
      type: viewerType,
      resolve: () => ({}),
    },
    connections: {
      type: new GraphQLObjectType({
        name: 'Connections',
Run Code Online (Sandbox Code Playgroud)

作为回报,在继电器容器中使用

export default Relay.createContainer(MakeRequestPage, {
    fragments: {
        connections: () => Relay.QL`
          fragment on Connections {
Run Code Online (Sandbox Code Playgroud)

Ahm*_*ous 1

如果它直接来自查询连接,我一直很难识别parentName 和parentID 。

faction和在中继文档的示例中与您的情况ships完全相同。每个都有一个 ID,你的对象也有一个 ID 。因此,对于您的突变, 是且是 的 ID 。connectionsdraftsGraphQLObjectconnectionsparentNameconnctionsparentIDconnections

query {
    connections {
        id
        drafts(first: 10) {
            edges {
                node {
                    ... on Draft {
                        id
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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