Discrepancy when using @id instead of ID

Hi. Recently, I moved away from using ID which gives me auto-generated IDs from Dgraph to @id which gives me control of the IDs I generate and this works well except for some problems:

When I was using ID before and was doing mutations, ref nodes were automatically created if I do not specify the IDs. For instance, in the below case, the linked node refInput will automatically get created with its own ID since I did not specify any here.

{
	"inputs": [
		{
			"key1": "val1",
			"key2": "val2",
			"refInput": [
				{
					"keyA": "val1",
					"keyB": "val2"
				}
			]
		}
	]
}

Now, I am unsure, how to replicate this behavior when using @id and specifying my own ID for the node refInput without doing 2 separate mutation calls. That is, I was looking to do something like this, and have the refNode automatically created with the ID I specify in a single mutation call:

{
	"inputs": [
		{
      		"id":"my-generated-id-1",
			"key1": "val1",
			"key2": "val2",
			"refInput": [
				{
          			"id": "my-generated-id-1-for-referred-node",
					"keyA": "val1",
					"keyB": "val2"
				}
			]
		}
	]
}

When I try this, I get the error that the referred node does not exist.

I was also facing another issue when using @id where recreating nodes with same ID did not return any output which I have filed here: Returning data when doing a mutation trying to re-create record which was already created - #3 by aman-bansal

Not sure what other challenges I may face with @id. Yet to see. Any way to fix these? Thanks.

Hi @tvvignesh, If you notice Ref object doesn’t have data types check like non null constraint. This is by design. If you need the refInput to be created, then please provide all the necessary fields that are required to create the object. The idea is this, if reference object presents then don’t create it else with the other information provided, try to create the child nodes.

For Example, say for schema

type Book {
  bookId: String! @id
  name: String!
  desc: String
  chapter: Chapter
}

type Chapter {
  chapterId: String! @id
  name: String!
  bookId: String! @search
}

If I use the mutation mentioned below, it will result in error that the reference node doesn’t exist, and chapter node is malformed. Second error means we are trying to create the object and it fails.

mutation{
  addBook(input:[{
    bookId: "bookId"
    name: "name"
    chapter:{
      chapterId: "chapter1"
    }
  }]) {
    
    book{
      bookId
      name
      chapter {
        chapterId
        name
        bookId
      }
    }
  }
}

But this mutation works

mutation{
  addBook(input:[{
    bookId: "bookId"
    name: "name"
    chapter:{
      chapterId: "chapter1"
      name: "chapter name"
      bookId: "bookId"
    }
  }]) {
    
    book{
      bookId
      name
      chapter {
        chapterId
        name
        bookId
      }
    }
  }
}

Ideally you shouldn’t. We try to manage the symmetry between ID and @id. Though has different use cases for users, graphql layer of Dgraph make sure that we treat this in the same way.

1 Like

Thanks @aman-bansal for your elaborate reply. If I remember right, I did specify all the required fields for the ref node as you mentioned in the second case but still it threw that the ref node does not exist. Will try again and revert back, I may be wrong.