在GraphQL突变中为字段分配类型

时间:2019-02-08 21:39:50

标签: graphql graphql-js

在下面的示例中,我尝试在数据库中创建一个Creature,每个生物都具有许多属性,因此我创建了一个CreatureAttribute Type,其中包含许多不同的必需String和Int字段。如何将该类型附加到生物类型突变?

mutation{
  createCreature(data: {
    creature_name: "Drake"
    creature_type: "Dragon"
    creature_size: "Huge"
    description: "description Text..."
    habitat: "habitat text..."
    combat: "combat text..."
    additional_info: "additional info text..."
    attributes: ********this is where I would like to bring in my CreatureAttributes Type********

    )
    {
    creature_name
    creature_type
    description
    habitat
    combat
    additional_info
    attributes

  }
}

谢谢您的回答:)

1 个答案:

答案 0 :(得分:0)

根据我的理解,您想创建一个“生物具有属性”关系。因此属性应接受数组,如以下示例所示。

type Creature { 
    id: ID! @unique 
    creature_name: String! 
    creature_type: String! 
    creature_size: String! 
    description: String! 
    habitat: String! 
    combat: String! 
    additional_info: String! 
    attributes: [CreatureAttributes]!
} 

type CreatureAttributes { 
    strength: Int! 
    health: Int! 
    stamina: Int! 
    mana: Int! 
    reaction: Int! 
...}

Ref-https://graphql.org/learn/schema/#object-types-and-fields