Nesting Mutation Create with Relationship or Custom Mutation Create with Relationship

Hello
I have been searching but cannot find a satisfactory answer on this.

Y have:

type Country {
  countryId: ID
  name: String!
  shortName: String!
  companies: [Company] @relation(name: "LOCATED_IN", direction: "IN")
}

type Company {
  companyId: ID
  name: String!
  language: String
  countries: [Country] @relation(name: "LOCATED_IN", direction: "OUT")
}

By default, mutations and queries are automatically generated.
For create:

CreateCompany(
companyId: ID
name: String!
language: String
): Company

CreateCountry(
countryId: ID
name: String!
shortName: String!
): Country

For relationship:

AddCountryCompanies(
from: _CompanyInput!
to: _CountryInput!
): _AddCountryCompaniesPayload

In this way, to generate the relationship between a Country and a Company, I must have the country created first.
Then when creating the company, it only allows me to create it, but not to create the reaction. Only after creating the company, can I create the relationship with: "AddCountryCompanies".

is it possible to nest mutation CreateCompany with AddCountryCompanies? in the same request or is it necessary to create a custom mutation?
In the latter case, what would the mutation look like:

type Mutation {
  CreateCompany(
    name: String!
    language: String
    plan: Plans
    countries: [Country] @relation(name: "LOCATED_IN", direction: "OUT")
  ): Company
}

????????

2 Likes

You'll need to either chain your mutations together in the application i.e. create the new county first and then create the LOCATED_IN relationship after or write a custom mutation that will do it in one operation. To do that you'll do it just like you would if you were writing a Cypher query.

1 Like

Perhaps I don't understand the benefits of using GraphQL for mutations in Neo4j. I thought the two main benefits were 1) Save on database I/O and network traffic and 2) Not having to write as much Cypher. Neither seems achievable. I think I'd rather just send a single post request with my Cypher for mutations.

That's a pretty valid want. I'd say you're essentially doing the same by writing the mutation as cyper anyway. For anything that's bigger than a single mutation I usually just write a cyper query, as a mutation, and get it done in the one request as well.

1 Like