Dql upsert @id instead of ID!

I am trying to create an upsert in a dql lambda in slash dgraph. Here is my schema:

type User {
  email: String! @search(by: [hash]) @id
  displayName: String! @search
  posts: @hasInverse(field: user)
}

type Post {
  id: ID!
  name: String! @search(by: [fulltext])
  description: String! @search(by: [fulltext])
  published: Boolean!
  user: User
}

I want to add a Post by a certain user. Here is my query, args being my incoming data:

upsert {
    query {
      q(func: eq(email, "${args.email}")) {
        v as uid
      }
    }
    mutation {
      set { 
        _:blank-0 <Post.name> "${args.name}" .
        _:blank-0 <Post.description> "${args.description}" .
        _:blank-0 <Post.user> uid(v) .
        _:blank-0 <Post.published> "${args.published}" .
        _:blank-0 <dgraph.type> "Post" .
      }
    }
  }

The mutation works fine, but the User field is empty.

I believe there is something wrong with this line:

_:blank-0 <Post.user> uid(v) .

What I mentally want to do is this:

_:blank-0 <Post.user.email> "${args.email}"

How can this be done?

Thanks,
J

So close,

Just like you mapped the post fields to Post.* the email field would be User.email

1 Like

Duh. Thank you!

Now I have to map the @hasInverse so User → posts also contains the new Post id… (since apparently this is not done automatically with rdf):

Can this be done with one upsert? I need to get the new post id correct? I imagine there is standard practice to deal with this situation…

upsert {
    query {
      q(func: eq(User.email, "${args.email}")) {
        v as uid
      }
    }
    mutation {
      set { 
        _:blank-0 <Post.name> "${args.name}" .
        _:blank-0 <Post.description> "${args.description}" .
        _:blank-0 <Post.user> uid(v) .
        _:blank-0 <Post.published> "${args.published}" .
        _:blank-0 <dgraph.type> "Post" .
        _:uid(v) <User.posts> "GET THIS NEW ID" .
      }
    }
  }

Nothing is done automatically with RDF mutation. That’s a GraphQL thing.

This should be

uid(v) <User.posts> _:blank-0 .

There’s no _: prefix in the uid() func.

The post already exist?

That was easy! Thank you @verneleem and @MichelDiz! I was thinking I had to add the post, get that ID, then add the inverse user post in a separate mutation.

Thanks!