How to delete an item by uid dgraph-js

I am using cloud stub and the npm package dgraph-js I am trying to delete a item but it is not working

I have tried two different ways and none of them are working. I just want to delete the node and all of its pointers. How can I do this in DQL by dgraph-js

async function DeletePostD(username: string, postId: string): Promise<boolean> {
    const txn = dgraphClient.newTxn();

    try {
        console.log("deleting post", postId);
        const postUid = await GetPostUid(postId);
        if (!postUid) return false;
        const mutation = new dgraph.Mutation();
        const p = {
            uid: postUid,
        };
        mutation.setDeleteJson(p);
        const didDel = await txn.mutate(mutation);
        console.log(didDel.getUidsMap());
        if (didDel.getUidsMap().arr_.length <= 0) return false;
        await txn.commit();
        return true;
    } catch (err) {
        console.error(err);
        return false;
    } finally {
        await txn.discard();
    }
}

The second function uses the nquads

async function DeletePostD(username: string, postId: string): Promise<boolean> {
    const txn = dgraphClient.newTxn();

    try {
        console.log("deleting post", postId);
        const postUid = await GetPostUid(postId);
        if (!postUid) return false;
        const mutation = new dgraph.Mutation();
        const p = {
            uid: postUid,
            
        }
        mutation.setDelNquads(`<${postUid}> * * .`);
        const didDel = await txn.mutate(mutation);
        console.log(didDel.getUidsMap());
        if (didDel.getUidsMap().arr_.length <= 0) return false;
        await txn.commit();
        return true;
    } catch (err) {
        console.error(err);
        return false;
    } finally {
        await txn.discard();
    }
}

these both do not work with no error code. The item exists in the Post type and definently can be seen through a regular query

Helllo @Grandma_Kisses ,

Well, in general, people often forget that the predicate “dgraph.type” must be present in the target node. Additionally, there needs to be an existing Type in the Schema. Once the Type matches all the existing predicates in the target node, it will execute the deletion of the target data.

Could you possibly give me an example because I have the post type which looks like this

Post.clickedOn: [uid] .
Post.comments: [uid] .
Post.hashtag: uid .
Post.likes: [uid] .
Post.postId: string @index(exact) .
Post.username: string .
type Post {
	Post.postId
	Post.username
	Post.likes
	Post.comments
	Post.hashtag
}

Do i have to supply each item and not just the uid? is that what you mean? maybe an example besides the basic would be good. Thank you

Did your mutation look like this?

	<_:new> <Post.postId> "1" .
	<_:new> <Post.username> "user1" .
	<_:new> <Post.likes> <0x1> .
	<_:new> <Post.comments>  <0x2> .
	<_:new> <Post.hashtag>  <0x32> .
	<_:new> <dgraph.type> "Post".

It needs to be “<_:new> <dgraph.type> “Post”.`” exactly as the name of the Type.

My mutation looks something like this

// mutate with variables
        const p = {
            "Profile.username": username,
            "Profile.rname": rname,
            "Profile.profilePicture": profilePicture,
            "Profile.backgroundPicture": backgroundPicture,
            "Profile.accent": accent,
            "Profile.isVerified": false,
            "Profile.isBanned": false,
        };
        const mu = new dgraph.Mutation();
        mu.setSetJson(p);
        await txn.mutate(mu);
        await txn.commit();
        return true;

and relationships using the setSetNquads function

Add this and you are good to go.

"Profile.isBanned": false,
"dgraph.type": Post,
        };

How would I delete then. I have tried using both of the previous method and it does not work

Try this

upsert {
  query {
    v as var(func: has(Post.comments)) 
  }

  mutation {
    set {
      uid(v) <dgraph.type> "Post" .
    }

  }
}

After that you can try to delete the target node.

This is an error of: in dql

"Error parsing JSON at line 1, character 2: invalid character 'u' looking for beginning of value\n"

Run this in Ratel and it will work. Run in the Mutation tab.