首页 > 解决方案 > 在 DGraph 中定义一个 GraphQL 突变,在另一个节点中添加一个联合类型的节点

问题描述

我是 GraphQL 和 Dgraph 的新手,并且一直在阅读手册。

我正在关注此处发布的手册中的示例: https ://dgraph.io/docs/graphql/schema/types/#union-type

我正在寻求使用突变来添加节点或节点列表,并将它们关联为 addHome 输入函数中可接受的成员。

然而,这些节点也是 Union 类型,因此我需要以某种方式将 HomeMember 转换为 Dog 并将类别定义为动物。

我看到有人在这里发布了一些东西,但我无法将这些部分拼凑在一起,因为它建议的代码似乎已经由 DGraph 生成: https ://discuss.dgraph.io/t/union-types-in-graphql/9581

我不确定,但有一种感觉,我可能需要以某种方式覆盖由 DGraph 自动生成到模式中的输入 HomeMemberRef 并禁用鹦鹉和人类?

到目前为止,我的代码如下所示:

mutation {
    addHome(input: [ 
    {address: "London", 
      
    // Here I need to add something a member that casts the
    // HomeMember to a Dog, defines the category as an animal and is accepted within 
    // this addHome input function
      
    }
    ]) {
    home{
    address
    id
    members
    }
    } 
    }

标签: graphqldgraph

解决方案


在另一个页面上找到了答案: https ://dgraph.io/docs/graphql/mutations/mutations-overview/

我的 GraphQL 使用这个:

mutation {
addHome(input: [ 
{address: "London", 
  
  
 members: [
            { dogRef: { category: Mammal, breed: "German Shepherd"} },
   
]
}
]) {

    home {
      address
      members {
        ... on Dog {
          breed
        }
        ... on Parrot {
          repeatsWords
        }
        ... on Human {
          name
        }
      }
    }
}
}

推荐阅读