deleteJson to delete a Node does not delete it

deleteJson mutation usage

Dgraph Version ``` v20.11.1 ```

What I want to do

I’m doing a small todo app in Vue using Dgraph to test the tech, and while I can add or edit my nodes using the setJson mutation, I can’t delete my nodes with deleteJson

What I did

Here’s my add and my delete methods:

AddTodo() (working :white_check_mark:)

async addTodo(title = this.title) {
  try {
    const res = await this.dgraph.newTxn().mutate({
      setJson: {
        uid: "_:newTodo",
        is_todo: true,
        title,
        completed: false
      },
      commitNow: true
    });
  } catch (error) {
    alert("Database write failed!");
    console.error("Network error", error);
  } finally {
    this.title = "";
    this.fetchData();
  }
},

DeleteTodo(todo) (not working: :x:)

async DeleteTodo(todo) {
  try {
    await this.dgraph.newTxn().mutate({
      deleteJson: {
        uid: todo.uid
      },
      commitNow: true
    });
  } catch (error) {
    alert("Database write failed!");
    console.error("Network error", error);
  } finally {
    this.fetchData();
  }
},

I also tried directly in Ratel but without success.
The thing is if I add a console.log() in the finally state of the promise it actually shows up, but my todos are still complete, the node I asked to delete is here too.

What am I doing wrong ? I’m following the tutorial I found here : Building a To-Do List React App with Dgraph - Dgraph Blog

And both in the page and the github the author is using deleteJson , I know I’m using vue instead of react but this not supposed to change anything in dgraph transactions

This is the identical function from that tutorial and I see no error in it at a glance.

When you call this function, is it returning the alert and the “Network error”, or what?

Where are your todos still complete at? If you query the database again are they still there or are they just still in your application?


As this appears to be your first post, I am going to assume that you are new to Dgraph. So this might be a help to you.

The tutorial you are following is using Dgraph and the DQL endpoints and clients. This is not a bad thing, but sometimes it can make things more difficult than they need to be. Dgraph offers both the DQL endpoints and a GraphQL endpoint. For starters check out: New to Dgraph? DQL vs. GraphQL Your application may require functionality that is only in DQL such as being able to run without a predefined schema or using transactions. More than likely though the GraphQL endpoint will better serve a frontend application.

These different endpoints can be quite confusing to new comers to Dgraph, If you are committed to the DQL endpoint, then your OP still needs to be resolved and the aforementioned questions will help lead to a resolution. If you are not committed to DQL and want to have the full ecosystem tools of GraphQL, can I recommend you to follow this instead:

(You don’t have to use Slash, you can run Dgraph locally for testing purposes and still use the GraphQL endpoint, you just won’t have the Slash GUI for updating the schema, using lambdas, or the API Explorer. But Slash does have a free tier at 1Mb/day transfer rate that makes getting started really easy without needing to setup and manage your own Dgraph instance)

Thank you for your answer, yes I am an absolute beginner with Dgraph.

No, the Network alert is not triggered since there is no error, and my todos are still complete both in my application after fetching my todos again and when I query all my items in ratel. Even if I say precisely
deleteJson: { uid: "0x2" }
(assuming the todo with this uid exists) it won’t be deleted.

I’ll look at the video you gave me because this is my case, I didn’t really understand the difference between GraphQL and DQL.

Thank you

I think I see the underlying issue of the OP if you decide to continue with DQL and the dgraph-js-http client.

From the docs:

If no predicates are specified, then all of the node’s known outbound edges (to other nodes and to literal values) are deleted (corresponding to deleting S * * ). The predicates to delete are derived using the type system. Refer to the RDF format documentation and the section on the type system for more information

(Emphasis Added)

In this tutorial, Dgraph is ran in a “schemaless” manner. The schema is not predefined but is assumed based on the received data. Also there are no types set for the nodes that are created.

Ideally should have been:

setJson: {
  uid: "_:newTodo",
  "dgraph.type": "Todo",
  is_todo: true,
  title,
  completed: false
},

But I think the types would also need to be defined with their fields, I may be wrong about this part. Without defining the Dgraph.type on the nodes deletes in the manner of S * * will not be deleted. So instead the deletes would have to be in the form of S P * or S P O to work:

deleteJson: {
  uid: todo.uid
  is_todo: null,
  completed: null
},

Overall, if you are new to Dgraph and are working with React/Next/Vue frontends I would HIGHLY recommend to use GraphQL instead. That would have solved 100% of this OP because with GraphQL the type system is enforced and a predefined schema is required.

1 Like