How to set type during mutation with JavaScript gRPC client?

Hi,

I’m totally new to Dgraph, so I apologize if this question has already been answered.

Suppose I have the following very simple schema:

type User {
  email
  name
}

email: string @index(exact) .
name: string .

I’m trying to add a node with the User type via the JavaScript gRPC client:

import {
  DgraphClient,
  DgraphClientStub,
  Mutation,
  Txn
} from 'dgraph-js'

const stub = new DgraphClientStub('localhost:9080')
const client = new DgraphClient(stub)
const txn = client.newTxn()
const mutation = new Mutation()

mutation.setSetJson({
  email: 'foo@example.com',
  name: 'Foo',
  type: 'User', // I also tried 'dgraph.type': 'User' here.
  uid: '_:user'
})

const response = await txn.mutate(mutation)
await txn.commit()
console.log(response.getUidsMap().get('user')) // "0x1"

I can find the node in Ratel by uid:

{
  users(func: uid("0x1")) {
    email
    name
    uid
  }
}

But when I query by the User type, I get a different node with no email or name:

Any idea what I’m doing wrong?

It have to be
dgraph.type: 'User'

Thanks for the reply, @MichelDiz.

Literally doing dgraph.type: 'User' is a JavaScript syntax error (a period is not allowed in the key of an object, without quoting the key).

This is why I commented that I had tried 'dgraph.type': 'User', which is valid JavaScript syntax for an object key that has a period in it.

I must be missing something really simple.

Can you try something like this?

mutation.setSetJson({
  email: 'foo@example.com',
  name: 'Foo',
  ['dgraph.type']: 'User',
  uid: '_:user'
})

Should be fine

['dgraph.type']: 'User' is actually the same as 'dgraph.type': 'User'

console

yep, that worked for you?

Unfortunately no, that doesn’t work.

I have tested this and work just fine

const dgraph = require("dgraph-js");

const stub = new dgraph.DgraphClientStub('localhost:9080')
const client = new dgraph.DgraphClient(stub)
const txn = client.newTxn()
const mutation = new dgraph.Mutation()

const mut = {
    email: 'foo@example.com',
    name: 'Foo',
    'dgraph.type': 'User',
    uid: '_:user'
  }


const run = async () => {
    mutation.setSetJson(mut)
    
    const response = await txn.mutate(mutation)
    await txn.commit()
    console.log(response.getUidsMap().get('user')) // "0x1"
  };

  run()

Query

{
  users(func: uid("0x2726")) {
    email
    name
    uid
    dgraph.type
  }
}
{
  "data": {
    "users": [
      {
        "email": "foo@example.com",
        "name": "Foo",
        "uid": "0x2726",
        "dgraph.type": [
          "User"
        ]
      }
    ]
  }
}

This also works

{
  users(func: type(User)) {
    email
    name
    uid
    dgraph.type
  }
}

Thank you, @MichelDiz! My problem turned out to be completely unrelated. It turns out I wasn’t properly destructuring the object I was passing to mutation.setSetJson(). In fact, the simplified example in my first post is wrong. I was actually doing this and not realizing it:

mutation.setSetJson({
  user: {
    email: 'foo@example.com',
    name: 'Foo',
    'dgraph.type': 'User',
    uid: '_:user'
  }
})

At any rate, thanks again for your help. Doing 'dgraph.type': 'User' definitely works!

1 Like