I want to write a Custom DQL mutation function that can apply a facet to an existing edge. In my schema I am experimenting with this syntax as an example.
type Mutation {
addMaskFacet(cityId: ID!, placeId: ID!): bool @custom(dql: """
set {
$cityId <place> $placeId (mask_needed=true) .
}
""")
}
I just can’t seem to find the right documentation and this is all very new to me.
Pay attention that you have to add the prefix that you are used to in the GraphQL feature. There it is automatically added, here you have to add manually.
Yes, as you are using Blank Nodes. Every time you send this mutation it will create a new entity. You have two options. Use the actual UID instead of Blank Nodes, or use upsert mutation instead of pure RDF.
No, cuz the UID has to follow the RDF syntax. Check this text Mutation Introduction | Intro | Dgraph Tour
The RDF syntax doesn’t have any magic trick as GraphQL does. You have to do everything manually.
BTW, I don’t understand why you have added a rating value to the place name. You should add in the relationship if your intention is to collect ratings.
Facet value holds a single value. But Faced on edge can have infinity facet values. Contextualized with the relation itself.
No, facets can be set and reset all the time. The issue is that every time you send a new mutation with no facet or a new facet, the old one is overwritten.
What is the point of the rating? what do you wanna achieve? Users rating places? So the rating should come from the users, not from the city to the place. Or is this rating something fix? some “global rating”?
If you know the UID of both nodes. A simple RDF mutation works.
e.g:
{
set {
<0x1> <City.places> <0x3> (rating=4) .
}
}
In the example above 0x1 is the city, and the 0x3 is the place. This query can be called an “update mutation”. Cuz this relation already exists, you are adding only the facet “rating”.
If you don’t know the UIDs but know how to find them via query. You can use Upsert.
e.g:
upsert {
query {
var(func: eq(City.name, "Miami")) {
v as uid
}
}
mutation {
set {
uid(v) <City.places> <0x3> (rating=4) .
}
}
}
“City.places” is the relation (edge). In that edge that you should add the facet. Facets have two scopes, on edge between two nodes, and on the value edge which belongs to that single node. I see that you confuse the scopes. Never give a facet to a value edge if your intention is to add an information about two related nodes.
After working a bit more on this and with @dmai I found a working solution
# This is add a facet between uid(2) and uid(5)
curl -H "Content-Type: application/rdf" http://0.0.0.0:8080/mutate?commitNow=true -XPOST -d $'
{
set {
<0x02> <City.places> <0x05> (rating=4) .
}
}'
# This does the same thing but with a query first
curl -H "Content-Type: application/rdf" http://0.0.0.0:8080/mutate?commitNow=true -XPOST -d $'
upsert {
query {
var(func: eq(City.name, "miami")) {
c as uid
}
var(func: eq(Place.place_id, "ChIJyezI5rC32YgRaD5pUTeSYCI")) {
p as uid
}
}
mutation {
set {
uid(c) <City.places> uid(p) (rating=4) .
}
}
}'
# This performs a query to see the facet value and an alias is used for the rating called rating.
curl -s -H "Content-Type: application/dql" http://0.0.0.0:8080/query -XPOST -d $'
{
data(func: eq(City.name, "miami")) {
City.id
City.name
City.places @facets(eq(rating, 4)) @facets(rating:rating) {
Place.place_id
}
}
}' | jq
# Here is some of the output
{
"data": {
"data": [
{
"City.name": "miami",
"City.places": [
{
"Place.place_id": "ChIJT2-gAgC32YgRTTrFGl9JoAo",
"rating": 4
},
{
"Place.place_id": "ChIJyezI5rC32YgRaD5pUTeSYCI",
"rating": 4
}
]
}
]
},