DQL to delete nodes + all child nodes with upsert

say i have this simplified schema in graphql:

type Folder {
    id: ID!
    title: String! @search(by: [exact])
    createdby: User!
    folders: [Folder] @hasInverse(field: parent)
    files: [File] @hasInverse(field: parent)
    parent: Folder
}

type File {
  	id: ID!
  	title: String! @search(by: [exact])
  	createdby: User!
  	parent: Folder
}

When I delete a folder I also want to delete all its children folders and files. I’m struggling to think of a recursive DQL query to solve this. I was thinking of an upsert with a recursive query to get all the UIDs of the child nodes and then somehow pass that to the delete mutation. for starters:

upsert {
 query {
   q(func: uid("0x2e357"))  {   // apply recursive query here to 
loop through all children
     Folder.folders {
       uid
     }
   }
 }
 
 mutation {
   delete {
       // triples with queried uids
   }
 }
}

any help to go from there would be great. i guess if too complicated I can generated RDF quads from the client side and go from there.

Are you inserting the data via GraphQL or DQL? if DQL, make sure your dataset has “dgraph.type” plus the value e.g: "{ dgraph.type": "File" },{ "dgraph.type": "Folder" }.

the DQL I used to solve this was:

upsert {
  query {
    q(func: uid("0x2e357")) @recurse @normalize {
      all_ids as uid
      Folder.folders
      Folder.files
    }
  }
  
  mutation {
    delete {
      uid(all_ids) * * .
    }
  }
}
3 Likes