Hi Michael, putting up this example. Please review if it makes sense so that we can take it forward.
Schema:
<customer>: uid .
<customerid>: string @index(exact) .
<orderid>: string @index(exact) .
<product>: uid .
<productid>: string @index(exact) .
Let’s create some customers and products.
{
set{
_:c1 <customerid> "c1" .
_:c2 <customerid> "c2" .
_:p1 <productid> "p1" .
_:p2 <productid> "p2" .
}
}
Now let’s say that we have an order, and we want to hook up to customer and product via a conditional upsert, like the one below.
upsert{
query{
order as var(func: eq(orderid,"o2")){
customerLink as customer @filter( eq(customerid,"c1"))
productLink as product @filter(eq(productid, "p1"))
}
customer as var(func: eq(customerid,"c1"))
product as var(func: eq(productid, "p1"))
}
mutation @if( eq(len(customerLink), 0) OR eq(len(productLink), 0)) {
set{
uid(order) <customer> uid(customer) .
uid(order) <product> uid(product) .
uid(order) <orderid> "o2" .
}
}
}
The first time we write, both conditions in the “OR” clause are unsatisfied, and we will get a response as below. A new order was created and predicates were linked.
{
"data": {
"code": "Success",
"message": "Done",
"queries": {},
"uids": {
"uid(order)": "0x2d"
}
},
"extensions": {
"server_latency": {
"parsing_ns": 63000,
"processing_ns": 12374300,
"encoding_ns": 23300,
"assign_timestamp_ns": 1396300,
"total_ns": 14631600
},
"txn": {
"start_ts": 450,
"commit_ts": 451,
"preds": [
"1-customer",
"1-orderid",
"1-product"
]
}
}
}
Now, let’s say that we want to switch the product from p1 to p2. The resulting mutation is as below.
upsert{
query{
order as var(func: eq(orderid,"o2")){
customerLink as customer @filter( eq(customerid,"c1"))
productLink as product @filter(eq(productid, "p2"))
}
customer as var(func: eq(customerid,"c1"))
product as var(func: eq(productid, "p2"))
}
mutation @if( eq(len(customerLink), 0) OR eq(len(productLink), 0)) {
set{
uid(order) <customer> uid(customer) .
uid(order) <product> uid(product) .
uid(order) <orderid> "o2" .
}
}
}
This will return all three predicates touched. But we don’t know which condition caused it by just looking at the response.
{
"data": {
"code": "Success",
"message": "Done",
"queries": {},
"uids": {}
},
"extensions": {
"server_latency": {
"parsing_ns": 54400,
"processing_ns": 9710000,
"encoding_ns": 56500,
"assign_timestamp_ns": 1521200,
"total_ns": 11833000
},
"txn": {
"start_ts": 526,
"commit_ts": 527,
"preds": [
"1-customer",
"1-orderid",
"1-product"
]
}
}
Thus, the ask is that we should return individual condition result (as specified in the @if
clause) to provide better visibility of the processing in the upsert block.
@mbn18, Please let us know if you have any comments.