【问题标题】:Issue with RANGE_ADD in Relay Mutations中继突变中的 RANGE_ADD 问题
【发布时间】:2017-06-27 02:13:57
【问题描述】:

我正在阅读中继文档并在 RANGE_ADD 中找到以下代码。

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',
      },
    }];
  }
  /* ... */
}

现在在这里提到edgeName 是添加新节点到连接所必需的。看起来不错。

现在,我将文档往下移,到达了这个突变的 GraphQL 实现。

mutation AddBWingQuery($input: IntroduceShipInput!) {
  introduceShip(input: $input) {
    ship {
      id
      name
    }
    faction {
      name
    }
    clientMutationId
  }
}

现在根据文档,这个突变给了我输出

{
  "introduceShip": {
    "ship": {
      "id": "U2hpcDo5",
      "name": "B-Wing"
    },
    "faction": {
      "name": "Alliance to Restore the Republic"
    },
    "clientMutationId": "abcde"
  }
}

我看不到edgeName 出现在这里。

我在我的项目中使用了石墨烯。那边我也只看到类似的东西

class IntroduceShip(relay.ClientIDMutation):
  class Input:
    ship_name = graphene.String(required=True)
    faction_id = graphene.String(required=True)

ship = graphene.Field(Ship)
faction = graphene.Field(Faction)

@classmethod
def mutate_and_get_payload(cls, input, context, info):
    ship_name = input.get('ship_name')
    faction_id = input.get('faction_id')
    ship = create_ship(ship_name, faction_id)
    faction = get_faction(faction_id)
    return IntroduceShip(ship=ship, faction=faction)

在这里,我也无法在任何地方看到edgeName

有什么帮助吗?我正在研究第一个突变,所以想确认我错过了什么或者这里有什么问题?

【问题讨论】:

    标签: graphql relayjs relay


    【解决方案1】:

    这个例子可能会被简化或有点过时,因为在实践中需要返回边缘,而这正是中继获取的内容(RANGE_ADD 中的其他字段更多是一种声明和不一定要获取)。

    在石墨烯中你可以这样做:

    # Import valid as of graphene==0.10.2 and graphql-relay=0.4.4
    from graphql_relay.connection.arrayconnection import offset_to_cursor
    
    class IntroduceShip(relay.ClientIDMutation):
        class Input:
            ship_name = graphene.String(required=True)
            faction_id = graphene.String(required=True)
    
        ship = graphene.Field(Ship)
        faction = graphene.Field(Faction)
        new_ship_edge = graphene.Field(Ship.get_edge_type().for_node(Ship))
    
        @classmethod
        def mutate_and_get_payload(cls, input, context, info):
            ship_name = input.get('ship_name')
            faction_id = input.get('faction_id')
            ship = create_ship(ship_name, faction_id)
            faction = get_faction(faction_id)
    
            ship_edge_type = Ship.get_edge_type().for_node(Ship)
            new_ship_edge = edge_type(
                # Assuming get_ships_number supplied
                cursor=offset_to_cursor(get_ships_number())
                node=ship
            )
    
            return cls(ship=ship, faction=faction, new_ship_edge=new_ship_edge)
    

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 2020-03-14
      • 2017-08-04
      • 2017-06-13
      • 2017-07-13
      • 2017-10-30
      • 2016-09-12
      • 2016-02-19
      • 1970-01-01
      相关资源
      最近更新 更多