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:
Note that the
createCompany
mutation has the object argumentowner
and the list object argumentaddresses
.addresses
has a nestedcontacts
list object argument.Using Apollo Client, we specify input arguments with GraphQL variables, so let's see how it looks in this case:
When calling the mutation with Apollo, we now have to specify the variables as an object:
and call the mutation with the variables:
The variable types
CompanyownerUser
and[CompanyaddressesAddress!]
depend on a combination of the multiplicity (to-one; to-many), the related models (Company
andUser
;Company
andAddress
) and the related fields (owner
;addresses
). You can find all type names in the GraphiQL playground docs when you navigate to thecreateCompany
mutation.