Auto-generated Mutation Does Not Create Relationship

130 Views Asked by At

I want to test auto-generated CRUD mutations created by calling makeAugmentedSchema from 'neo4j-graphql-js'. There is no problem with creating nodes but creating relationship does not work for me. Please advise on what I am doing wrong here.

Schema:

type Bio{
    id: ID!
    description: String
}

type Person{
    id: ID!
    name: String
    dob: Date
    gender: String
    bioRelation: [Bio] @relation(name: "HAS_BIO", direction: "OUT")
}

Mutation: I am following the Interface Mutations guidance https://grandstack.io/docs/graphql-interface-union-types to create mutation.

mutation {
    p: CreatePerson(
        name: "Anton",
        gender: "Male") {
    name
    gender
    id
  }
    b: CreateBio(
        description: "I am a developer") {
    description
        id
  }
  r: AddPersonBioRelation(
    from: {id: "p"},
    to:{id: "b"}
  ){
    from{
      name
    }
    to{
      description
    }
  }
}

It create Person and Bio nodes but no any relationship gets created between the two:

{
  "data": {
    "p": {
      "name": "Anton",
      "gender": "Male",
      "id": "586b63fd-f9a5-4274-890f-26ba567c065c"
    },
    "b": {
      "description": "I am a developer",
      "id": "a46b4c22-d23b-4630-ac84-9d6248bdda89"
    },
    "r": null
  }
}

This is how AddPersonBioRelation looks like: enter image description here

Thank you.

2

There are 2 best solutions below

0
On

I am new to GRANDstack, and I have also been struggling with these types of issues myself. I have typically broken this out separate mutations (in javascript) and used the return value for each as values for the next mutation. for example:

await createIncident({
  variables: {
    brief: values.brief,
    date,
    description: values.description,
    recordable: values.recordable,
    title: values.title
  }
}).then((res) => {
  addIncidentUser({
    variables: {
      from: user.id,
      to: res.data.CreateIncident.id
    }
  });
});

the problem that i see in the example you've provided is that you are specifying a string value for from and to as "p" and "b" respectively and NOT the p.id and b.id return values from the parent mutations.

it's fine of me to point that out but what i can't for the LIFE of me figure out is how to properly reference p.id and b.id in the mutation itself. in other words you are trying to send

from: { id: "586b63fd-f9a5-4274-890f-26ba567c065c"}
to: { id: "a46b4c22-d23b-4630-ac84-9d6248bdda89" } 

but in reality you are sending

from: { id: "p"}
to: { id: "b" } 

which aren't actually references in neo4j so it fails.

if we can figure out how to properly reference p.id and b.id we should get this working.

0
On

Thank you, @dryhurst. It appears that there is no way to reference id of newly created nodes, but I found a solution by introducing temp id property. Please see the discussion of this matter and final solution on:

https://community.neo4j.com/t/auto-generated-mutation-does-not-create-relationship/21412/16.