Dgraph Connect two nodes by the @id attribute

I want to update my node Course’s students with an existing student. I want it to do it by the @id attribute which is name. But it creates an new instance of Student. How can I achieve that?

mutation {
  updateCourse(input: 
    {
      filter: { name: {eq:"English"}}, 
      set: {students: [{name:"John Xyz"}]}}) {
    numUids
  }
}

type Course {
        id : ID!
        name: String! @id
        students : [Student] @hasInverse(field: courses)
    }
type Student {
        id : ID!
        name: String! @id
        courses : [Course]
}

Edit: Actually if it finds the node with specified name, updates edges.Howver if there is not a Student with that name I dont want it to create a new Student with given name. I need an error or something that blocks that behaviour, it is possible?

The only way to make sure no new entries are created via GraphQL is by doing a mutation with the id passed instead of the name →

mutation updateStudentInCourse {
  updateCourse(input: {filter: {name: {eq: "Spanish"}}, set: {students: {id: "0x1fbfc"}}}) {
    numUids
  }
}

If you wish to do it by name then you’ll have to do it from DQL via upsert (that’ll prevent new entries from being created).

However since there also exists an inverse property over here, we can use that and build a mutation for student (instead of mutation for course) →

mutation updateCourseInStudent {
  updateStudent(input: {filter: {name: {eq: "Messi"}}, set: {courses: {name: "Spanish"}}}) {
    numUids
  }
}

This will only update if an entry with the name Messi exists. No new object creation will be done

1 Like