【问题标题】:Assigning a Type to a field in GraphQL Mutation为 GraphQL Mutation 中的字段分配类型
【发布时间】:2019-02-11 22:26:41
【问题描述】:

在下面的示例中,我试图在我的数据库中创建一个 Creature,每个生物都有许多属性,因此我创建了一个 CreatureAttribute 类型,其中包含许多不同的必需 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

  }
}

提前感谢您的回答:)

【问题讨论】:

  • 您是否尝试创建一个“类型”并将其作为数组分配给属性? ex - 属性:[creatureAttribute]
  • 所以也许我只是不明白 GraphQL 和突变是如何工作的,但这是我的数据模型,也许这会对我的总体目标有所帮助。 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! ...}

标签: graphql graphql-js


【解决方案1】:

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

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! 
...}

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

【讨论】:

    猜你喜欢
    • 2019-03-25
    • 1970-01-01
    • 2021-11-25
    • 2019-04-03
    • 2018-08-15
    • 2020-04-21
    • 2020-09-27
    • 1970-01-01
    • 2019-03-13
    相关资源
    最近更新 更多