问题描述
嗨,我试图在我的 https://www.graph.cool/ db上突变. 我的项目是一个React Web应用程序,我将Apollo用作GraphQl客户端和GraphQl-TAG NPM软件包作为模板字面解析器.
问题是我不知道如何使用嵌套数据安排正确的突变. 我的架构看起来像这样,例如,请注意类型"公司"的字段"地址"是"地址"对象类型的数组.
type Company { name: String! website: String Owner: User Addresses: [Addresses] } type User { name: String! email: String } type Address { street: String! city: String! country: String contacts: [Contact] } type Contact { name: String email: String phone: String }
例如,我想在一个突变中同时创建一家新公司,其新所有者和多个地址.对于地址,我还需要创建一个新的联系人.
推荐答案
您可以使用我们所谓的嵌套突变来实现这一目标. 首先,让我们看看如何从GraphiQL操场上做到这一点:
mutation createNestedCompany { createCompany( owner: { name: "Mickey" email: "mickey@mouse.com" } addresses: [{ street: "A street" city: "A city" country: "A country" contacts: [{ name: "Mickey" email: "mickey@mouse.com" phone: "+1 23456789" }] }, { street: "B street" city: "B city" country: "B country" contacts: [{ name: "Minney" email: "minney@mouse.com" phone: "+9 87654321" }] }] ) { id owner { id } addresses { id contacts { id } } } }
请注意,createCompany突变具有对象参数owner和列表对象参数addresses. addresses有一个嵌套contacts列表对象参数.
使用Apollo客户端,我们使用GraphQl变量指定输入参数,因此让我们看看它的外观:
const createNestedCompany = gql` mutation createNestedCompany( $owner: CompanyownerUser $addresses: [CompanyaddressesAddress!] ) { createCompany( owner: $owner addresses: $addresses ) { id owner { id } addresses { id contacts { id } } } } `
用阿波罗调用突变时,我们现在必须将变量指定为一个对象:
const variables = { owner: { name: "Mickey" email: "mickey@mouse.com" }, addresses: [{ street: "A street" city: "A city" country: "A country" contacts: [{ name: "Mickey" email: "mickey@mouse.com" phone: "+1 23456789" }] }, { street: "A street" city: "A city" country: "A country" contacts: [{ name: "Minney" email: "minney@mouse.com" phone: "+9 87654321" }] }] }
并用变量调用突变:
this.props.createNestedCompany({ variables }) .then((response) => { console.log('Company, owner and addresses plus contacts created'); }).catch((e) => { console.error(e) })
变量类型CompanyownerUser和[CompanyaddressesAddress!]取决于多样性(to-One; to-many)的组合,相关模型(
问题描述
Hi I am trying to write data on my https://www.graph.cool/ db with a mutation. My project is a React web-app and I am using Apollo as graphql client and graphql-tag npm package as template literal parser.
The problem is that i don't know how to arrange the gql template string for the correct mutation with nested data. My schema looks like this, for example note the field "Addresses" for the type "Company" is an array of "Address" objects type.
type Company { name: String! website: String Owner: User Addresses: [Addresses] } type User { name: String! email: String } type Address { street: String! city: String! country: String contacts: [Contact] } type Contact { name: String email: String phone: String }
For example, I want to create a new company, its new owner and multiple addresses at the same time in one mutation. For the addresses I need to create a new contact as well.
推荐答案
You can make use our so called nested mutations to accomplish that. First of all, let's see how we can do it from the GraphiQL playground:
mutation createNestedCompany { createCompany( owner: { name: "Mickey" email: "mickey@mouse.com" } addresses: [{ street: "A street" city: "A city" country: "A country" contacts: [{ name: "Mickey" email: "mickey@mouse.com" phone: "+1 23456789" }] }, { street: "B street" city: "B city" country: "B country" contacts: [{ name: "Minney" email: "minney@mouse.com" phone: "+9 87654321" }] }] ) { id owner { id } addresses { id contacts { id } } } }
Note that the createCompany mutation has the object argument owner and the list object argument addresses. addresses has a nested contacts list object argument.
Using Apollo Client, we specify input arguments with GraphQL variables, so let's see how it looks in this case:
const createNestedCompany = gql` mutation createNestedCompany( $owner: CompanyownerUser $addresses: [CompanyaddressesAddress!] ) { createCompany( owner: $owner addresses: $addresses ) { id owner { id } addresses { id contacts { id } } } } `
When calling the mutation with Apollo, we now have to specify the variables as an object:
const variables = { owner: { name: "Mickey" email: "mickey@mouse.com" }, addresses: [{ street: "A street" city: "A city" country: "A country" contacts: [{ name: "Mickey" email: "mickey@mouse.com" phone: "+1 23456789" }] }, { street: "A street" city: "A city" country: "A country" contacts: [{ name: "Minney" email: "minney@mouse.com" phone: "+9 87654321" }] }] }
and call the mutation with the variables:
this.props.createNestedCompany({ variables }) .then((response) => { console.log('Company, owner and addresses plus contacts created'); }).catch((e) => { console.error(e) })
The variable types CompanyownerUser and [CompanyaddressesAddress!] depend on a combination of the multiplicity (to-one; to-many), the related models (Company and User; Company and Address) and the related fields (owner; addresses). You can find all type names in the GraphiQL playground docs when you navigate to the createCompany mutation.