tron
October 16, 2020, 5:20pm
1
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?
MichelDiz
(Michel Diz)
October 16, 2020, 5:28pm
2
It have to be
dgraph.type: 'User'
tron
October 16, 2020, 6:18pm
3
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.
MichelDiz
(Michel Diz)
October 16, 2020, 6:24pm
4
Can you try something like this?
mutation.setSetJson({
email: 'foo@example.com',
name: 'Foo',
['dgraph.type']: 'User',
uid: '_:user'
})
Should be fine
tron
October 16, 2020, 6:57pm
5
['dgraph.type']: 'User'
is actually the same as 'dgraph.type': 'User'
MichelDiz
(Michel Diz)
October 16, 2020, 6:58pm
6
yep, that worked for you?
tron
October 16, 2020, 6:59pm
7
Unfortunately no, that doesn’t work.
MichelDiz
(Michel Diz)
October 16, 2020, 7:28pm
8
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
}
}
tron
October 16, 2020, 7:49pm
9
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