首页 文章

GraphQL Relay Mutation配置RANGE_ADD的连接的parentName

提问于
浏览
6

我有一个使用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给定父级,连接以及响应有效负载中新创建的边缘的名称 . 中继将根据指定的范围行为将节点添加到存储并将其附加到连接 . 参数parentName:string响应中表示连接父节点的字段名称parentID:string包含连接connectionName的父节点的DataID:string响应中表示连接的字段名称edgeName:string字段名称in代表新创建的边缘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',
      },
    }];
  }
  /* ... */
}

如果父母和派系一样明显,这是小菜一碟,但如果它直接来自查询连接,我一直很难识别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 {

1 回答

  • 1

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

    在Relay文档的示例中, factionshipsconnectionsdrafts 完全相同 . 每个 GraphQLObject 都有一个ID, connections 对象也是如此 . 因此,对于您的突变, parentNameconnctionsparentIDconnections 的ID .

    query {
        connections {
            id
            drafts(first: 10) {
                edges {
                    node {
                        ... on Draft {
                            id
                        }
                    }
                }
            }
        }
    }
    

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

相关问题