2017-02-09 17 views
0

我正在浏览中继文档,并在RANGE_ADD中找到以下代码。RANGE_ADD在Relay Mutations中的问题

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

请帮忙吗?我正在为第一个突变工作,所以想确认一个我错过了什么,或者在这里出了什么问题?

回答

1

这个例子可能是要么简化还是有点obsoloete,因为在实践中有需要返回边缘而这正是是由继电器取出(在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 IntroduceShip(ship=ship, faction=faction, new_ship_edge=new_ship_edge) 
相关问题