Hello community!
I’ve been using Dgraph at my company to prototype our product, but we’re looking to keep in production at launch. The Dgraph database is an absolutely remarkable piece of technology. This is my first post to the community so I apologize if this topic is malformed in any way.
Our question is: using the generated “updateType” mutations, can we reorder the links arbitrarily without adding additional properties? And if not, can that be a feature of Dgraph in future?
Experience Report for Feature Request
For (IP related) reasons, I’ve translated the problem we faced into the Todo App example found in the docs.
As such, consider the following Todo App data for this topic:
{
"data": {
"addUser": {
"user": [
{
"username": "[email protected]",
"name": "Alice",
"tasks": [
{
"id": "0x2",
"title": "Avoid crowd"
},
{
"id": "0x3",
"title": "Wash your hands often"
},
{
"id": "0x5",
"title": "Avoid touching your face"
},
{
"id": "0x6",
"title": "Stay safe"
}
]
}
]
}
},
"extensions": {
"touched_uids": 41
}
}
What you wanted to do
We want to reorder the tasks using the generated updateUser
mutation like so:
mutation {
updateUser(input: {
filter: { username: { eq: "[email protected]" }}
set: {
tasks: [
{
id: "0x6"
},
{
id: "0x2"
},
{
id: "0x3"
},
{
id: "0x5"
}
]
}
}) {
user {
tasks {
id
title
}
}
}
}
so that “Stay Safe” was at the top of Alice’s list of tasks. We want her to stay safe.
The mutation, however, returns the result:
{
"data": {
"updateUser": {
"user": [
{
"tasks": [
{
"id": "0x2",
"title": "Avoid crowd"
},
{
"id": "0x3",
"title": "Wash your hands often"
},
{
"id": "0x5",
"title": "Avoid touching your face"
},
{
"id": "0x6",
"title": "Stay safe"
}
]
}
]
}
},
"extensions": {
"touched_uids": 51
}
}
…preserving the original order of the links, meaning we could not reorder Alice’s tasks.
What you actually did
We added a “priority” field to the Task type, mutated accordingly, and ordered the result in our query like so:
mutation {
updateUser(input: {
filter: { username: { eq: "[email protected]" }}
set: {
tasks: [
{
id: "0x6",
priority: 2
},
{
id: "0x2",
priority: 1
},
{
id: "0x3",
priority: 1
},
{
id: "0x5",
priority: 1
}
]
}
}) {
user {
tasks(order: { desc: priority}) {
id
title
}
}
}
}
which does return “Stay Safe” as the first task item.
Why that wasn’t great, with examples
Adding the extra field complicated the API, and thus, complicated our queries. We also need to ensure that each “priority” value is unique so that the ordering remains strict. For us, this means we must use an initial query to return the current highest priority in order to move tasks to the top of the list.
Any external references to support your case
The GraphQL spec defines the List type as an ordered collection; stating “GraphQL servers must return an ordered list as the result of a list type” https://spec.graphql.org/June2018/#sec-Type-System.List
Obviously, Dgraph does indeed do this. However, one could argue that the links themselves under the GraphQL interface should also be ordered accordingly.