Create Child nodes with addParent(...)

Is it possible to create child nodes “on-the-fly” while creating parents?

I don’t want to reference existing childs, I want to create new ones.

E.g.

addParent(input: [
   {
      id: "abc"
      child: {  id: "123" } 
   }
]) {
   parent {
      child {
         id
      }
   }
}
1 Like

Yes. It is possible.

But how? I just get:

Dgraph execution failed because Some variables are used but not defined\

Can you share your schema and the mutation which you are trying to execute ?

Yes, sure. Sorry, I couldn’t come up with a shorter example.

I’m using latest master from yesterday (~20 hours ago)

Schema:

type User {
  username: String! @id

  password: String!

  projects: [Project!] @hasInverse(field: owner)
}

type Project {
  id: String! @id

  owner: User!

  name: String! @search(by: [hash])

  datasets: [Dataset!] @hasInverse(field: project)
}

type Dataset {
  id: String! @id

  owner: User!

  project: Project!

  name: String! @search(by: [hash])
}

Create User:

mutation {
  addUser(input: 
  [
    {
      username: "user"
      password: "useruser"
    }
  ]) {
    user {
      username
    }
  }
}

Add Project including one dataset (this fails):

mutation {
  addProject(input:
  [
    {
      id: "p1"
      owner: {
        username: "user"
      }
      name: "project"
      datasets: [
        {
          id: "d1"
      		owner: {
        		username: "user"
      		}
      		name: "dataset"
        }
      ]
    }
  ]) {
    project  {
      id
    }
  }
}

Error Message:

"message": "mutation addProject failed because Dgraph execution failed because Some variables are used but not defined\nDefined:[Dataset4 Project2 Project5 __dgraph__0]\nUsed:[Dataset4 Project2 Project5 User7 __dgraph__0]\n",

Edit Came up with a shorter example

Okay, I found the bug. The nested UserRef tries to create a new User instead of linking to an existing user. Therefore, the mutation fails as not all fields are present in the input.

The following mutation works, but does not do what I try to achieve. (Which is linking the new project and dataset to the same owner instead of creating a new one for the dataset)

mutation {
  addProject(input:
  [
    {
      id: "p1"
      owner: {
        username: "user"
      }
      name: "project"
      datasets: [
        {
          id: "d1"
      		owner: {
        		username: "differentuser"
                        password: "12345678"
      		}
      		name: "dataset"
        }
      ]
    }
  ]) {
    project  {
      id
    }
  }
}

Can you please confirm the bug?

This bug does not appear using the update-mutations. There, I can create a new Dataset and inside the dataset-creation I can reference the correct user.

Thanks for providing schema and step by step instructions to reproduce the issue.
I could reproduce this on master. I am accepting this as a bug. This in a way is similar to what @spinelsun has reported over here, Can't insert data .

Comment about your schema:
You could also use @secret directive for password field. You may read more about it here, https://dgraph.io/docs/master/graphql/schema/types/#password-type

@

Thank you for this suggestion - unfortunately their is currently another bug which prevents usage of @secret in combination with @auth directives. See: Combining @secret and @auth directives - #7 by amaster507

My current workaround is a @custom createAccount mutation where the 2nd server calculates the password-hash and calls the addUser mutation with the calculated hash. Similar I have dedicated login mutations.

1 Like

@rajas Is this bug relevant also for DQL?
also any work around?

Note: I cannot separate queries because I have typs that their childs are required and I have has inverse from the child to the parent which is also required.
I don’t want to remove these connections…

I don’t think that this is relevant to DQL as well. With the error which is thrown, it looks like a bug in converting the GraphQL mutation to DQL mutation.
Although, things will be clear when a deeper investigation is carried out.

The bug seems to occur when a node is added referencing other nodes at the second and third level. (Notice how user is referenced inside versions and branch). The workaround could be to add data in a step by step way. i.e. adding versions first and then adding branch.

The problem is that I have inverse between branch and version that obligates to create the branch with at least one version or when creating a version specify the branch it belongs to…
Until this bug will be solved I will remove this constrain in one of them

Hi there,

I confirm this bug after an upgrade to
Dgraph version : v20.07.2
Dgraph codename : shuri-2

Exemple of query that failed with errors

mutation addNode failed because Dgraph execution failed because Some variables are used but not defined                           
Defined:[Node2 Node5 Node6 __dgraph__0]
Used:[Node2 Node5 Node6 User9 __dgraph__0]

Schema

Type Node {
  id: ID! 
  createdBy: User!
  nameid: String!  @id
  children: [Node!]   @hasInverse(field: parent)
}

Type User {
  id: ID! 
  username: String! @id
}

Query

mutation addOneNode($n: AddNodeInput!) {
    addNode(input: [$n]) { node { id } }
}

Variable

{ "n":
  {  "createdBy": {"username": "dtrckd"},
     "nameid":"my_name",
     "children":[{
      "createdBy": {"username": "dtrckd"},
      "nameid":"my_child_name",
       }],
  }
}

Note that when changing the reference of the User in the createdBy field with its id instead of its username in the children node, dgraph doesnt complain anymore and the request run without the error.

@rajas is there any update on this?

It is on our roadmap and will soon be fixed within the next few weeks. Thanks!

2 Likes

This has been fixed in master branch. Related PR: Fix(GraphQL): Refactor Mutation Rewriter for Add and Update Mutations by vmrajas · Pull Request #7409 · dgraph-io/dgraph · GitHub
Cherry Pick to 20.11: Fix(GraphQL): Refactor Mutation Rewriter for Add and Update Mutations… by vmrajas · Pull Request #7413 · dgraph-io/dgraph · GitHub

2 Likes

@rajas how can I get the cherry picked 20.11.1 executable?
I tried reinstalling with the curl script, but that didn’t work - the error still persists

Edit: I checked out 20.11 and build from source, now it works. When will this find it’s way into the downloadable executables?

The bug fix is going to be part of 20.11.2 and 21.03 and any releases after that.

1 Like