# How to query for arbitrary predicate and get the uids along with the predicate

**URL:** <https://discuss.dgraph.io/t/how-to-query-for-arbitrary-predicate-and-get-the-uids-along-with-the-predicate/18741>\
**Category:** Dgraph\
**Tags:** kind:question\
**Created:** [June 23, 2023, 1:17pm UTC](https://discuss.dgraph.io/t/how-to-query-for-arbitrary-predicate-and-get-the-uids-along-with-the-predicate/18741 "2023-06-23T13:17:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Orneyfish](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/orneyfish/32/10558_2.png) [@Orneyfish](https://discuss.dgraph.io/u/Orneyfish)\
**Post date:** [June 23, 2023, 1:17pm UTC](https://discuss.dgraph.io/t/how-to-query-for-arbitrary-predicate-and-get-the-uids-along-with-the-predicate/18741/1 "2023-06-23T13:17:30Z")

</div>

Hi I am porting over from SPARQL to DQL:  
Now I have a query that fetches uid from graph. Now I want to get the predicates and the node uid that are connected to the node.  
Like in sprarql:

```auto
select ?s ?p ?o where {
?s a <s_type> .
?s ?p ?o .
}

```

Now how can I port over to DQL? Can anyone help?  
I have tried this query:

```auto
{
  result (func: type("Section")) {
    uid
    expand(_all_) {
      seconduid: uid
    }
  }
}

```

But it was giving the following result:

```auto
{
  "data": {
    "result": [
      {
        "uid": "0x25e6261"
      },
      {
        "uid": "0x299e4c1"
      },
      {
        "uid": "0x3465161"
      },
      {
        "uid": "0x37ec681"
      },
      {
        "uid": "0x3b8c241"
      },
      {
        "uid": "0x3f444a1"
      },
      {
        "uid": "0x42e4062"
      },
      {
        "uid": "0x469c2c1"
      },
      {
        "uid": "0x4a54521"
      },
      {
        "uid": "0x4d79fc1"
      },
      {
        "uid": "0x51014e1"
      },
      {
        "uid": "0x54b9741"
      },
      {
        "uid": "0x58719a1"
      },
      {
        "uid": "0x80e12a4"
      },
      {
        "uid": "0x8142d23"
      },
      {
        "uid": "0x81d54e2"
      },
      {
        "uid": "0x81d54e4"
      },
      {
        "uid": "0x81d54e7"
      },
      {
        "uid": "0x8236f62"
      },
      {
        "uid": "0x8280342"
      },
      {
        "uid": "0x8312b01"
      },
      {
        "uid": "0x835bee3"
      },
      {
        "uid": "0x8374583"
      },
      {
        "uid": "0x84687c3"
      },
      {
        "uid": "0x8499506"
      },
      {
        "uid": "0x84b1ba4"
      },
      {
        "uid": "0x84e28e4"
      },
      {
        "uid": "0x852bcc2"
      },
      {
        "uid": "0x858d741"
      },
      {
        "uid": "0x85a5de2"
      },
      {
        "uid": "0x85d6b22"
      },
      {
        "uid": "0x86385a1"
      },
      {
        "uid": "0x86692e1"
      },
      {
        "uid": "0x89f0802"
      },
      {
        "uid": "0x8a08ea4"
      },
      {
        "uid": "0x8a52286"
      },
      {
        "uid": "0x8b15781"
      },
      {
        "uid": "0x8b15783"
      },
      {
        "uid": "0x8b8f8a1"
      },
      {
        "uid": "0x8b8f8a3"
      },
      {
        "uid": "0x8ba7f44"
      },
      {
        "uid": "0x8bc05e2"
      },
      {
        "uid": "0x8c3a701"
      },
      {
        "uid": "0x8fc1c22"
      },
      {
        "uid": "0x8fda2c4"
      },
      {
        "uid": "0x900b004"
      },
      {
        "uid": "0x90543e4"
      },
      {
        "uid": "0x90b5e63"
      },
      {
        "uid": "0x90ff242"
      },
      {
        "uid": "0x912ff81"
      },
      {
        "uid": "0x9191a02"
      },
      {
        "uid": "0x920bb21"
      },
      {
        "uid": "0x923c862"
      },
      {
        "uid": "0xa1af9a5"
      },
      {
        "uid": "0xa1c8042"
      },
      {
        "uid": "0xa1e06e2"
      },
      {
        "uid": "0xa229ac4"
      },
      {
        "uid": "0xa272ea2"
      },
      {
        "uid": "0xa305661"
      },
      {
        "uid": "0xa37f785"
      },
      {
        "uid": "0xa397e22"
      },
      {
        "uid": "0xa3e1202"
      },
      {
        "uid": "0xa3e1203"
      },
      {
        "uid": "0xa7b1b03"
      },
      {
        "uid": "0xa82bc22"
      },
      {
        "uid": "0xa8ef121"
      },
      {
        "uid": "0xa969241"
      },
      {
        "uid": "0xa999f83"
      },
      {
        "uid": "0xa9cacc1"
      },
      {
        "uid": "0xa9fba01"
      },
      {
        "uid": "0xa9fba02"
      },
      {
        "uid": "0xaa5d481"
      },
      {
        "uid": "0xaa5d482"
      },
      {
        "uid": "0xae46423"
      },
      {
        "uid": "0xae8f803"
      },
      {
        "uid": "0xaf21fc3"
      },
      {
        "uid": "0xaf3a664"
      },
      {
        "uid": "0xaf6b3a1"
      },
      {
        "uid": "0xaf6b3a2"
      },
      {
        "uid": "0xaf83a42"
      },
      {
        "uid": "0xaffdb62"
      },
      {
        "uid": "0xb02e8a2"
      },
      {
        "uid": "0xb077c81"
      },
      {
        "uid": "0xb3ff1a3"
      },
      {
        "uid": "0xb417844"
      },
      {
        "uid": "0xb42fee6"
      },
      {
        "uid": "0xb524121"
      },
      {
        "uid": "0xb53c7c4"
      },
      {
        "uid": "0xb554e61"
      },
      {
        "uid": "0xb585ba2"
      },
      {
        "uid": "0xb5cef81"
      },
      {
        "uid": "0xb618361"
      },
      {
        "uid": "0xb661741"
      },
      {
        "uid": "0xb9b7f26"
      },
      {
        "uid": "0xb9e8c62"
      },
      {
        "uid": "0xba01303"
      },
      {
        "uid": "0xba32042"
      },
      {
        "uid": "0xbb26282"
      },
      {
        "uid": "0xbb87d02"
      },
      {
        "uid": "0xbba03a2"
      },
      {
        "uid": "0xbbb8a44"
      },
      {
        "uid": "0xbc4b201"
      },
      {
        "uid": "0xbc7bf42"
      },
      {
        "uid": "0xbe7ca63"
      },
      {
        "uid": "0xbe7ca64"
      },
      {
        "uid": "0xbead7a4"
      },
      {
        "uid": "0xbef6b82"
      },
      {
        "uid": "0xbf278c7"
      },
      {
        "uid": "0xbf3ff62"
      },
      {
        "uid": "0xbf3ff64"
      },
      {
        "uid": "0xc095c21"
      },
      {
        "uid": "0xc0c6961"
      },
      {
        "uid": "0xc0f76a1"
      },
      {
        "uid": "0xc47ebc2"
      },
      {
        "uid": "0xc511383"
      },
      {
        "uid": "0xc529a23"
      },
      {
        "uid": "0xc572e02"
      },
      {
        "uid": "0xc58b4a1"
      },
      {
        "uid": "0xc58b4a3"
      },
      {
        "uid": "0xc5a3b42"
      },
      {
        "uid": "0xc61dc61"
      },
      {
        "uid": "0xc697d81"
      },
      {
        "uid": "0xc6c8ac1"
      },
      {
        "uid": "0xca1f2a4"
      },
      {
        "uid": "0xca1f2a5"
      },
      {
        "uid": "0xca4ffe2"
      },
      {
        "uid": "0xcb134e2"
      },
      {
        "uid": "0xcb44223"
      },
      {
        "uid": "0xcb74f62"
      },
      {
        "uid": "0xcb8d601"
      },
      {
        "uid": "0xcba5ca4"
      },
      {
        "uid": "0xcc07721"
      },
      {
        "uid": "0xcc07723"
      },
      {
        "uid": "0xd021403"
      },
      {
        "uid": "0xd052146"
      },
      {
        "uid": "0xd06a7e2"
      },
      {
        "uid": "0xd0e4902"
      },
      {
        "uid": "0xd1a7e01"
      },
      {
        "uid": "0xd1a7e02"
      },
      {
        "uid": "0xd1c04a4"
      },
      {
        "uid": "0xd209882"
      },
      {
        "uid": "0xd23a5c1"
      },
      {
        "uid": "0xd29c041"
      },
      {
        "uid": "0xd6e6a62"
      },
      {
        "uid": "0xd6ff104"
      },
      {
        "uid": "0xd7177a3"
      },
      {
        "uid": "0xd72fe43"
      },
      {
        "uid": "0xd779221"
      },
      {
        "uid": "0xd7a9f62"
      },
      {
        "uid": "0xd80b9e3"
      },
      {
        "uid": "0xd854dc2"
      },
      {
        "uid": "0xd885b01"
      },
      {
        "uid": "0xd89e1a1"
      },
      {
        "uid": "0xdc256c2"
      },
      {
        "uid": "0xdc9f7e4"
      },
      {
        "uid": "0xdcb7e85"
      },
      {
        "uid": "0xdce8bc2"
      },
      {
        "uid": "0xdd01263"
      },
      {
        "uid": "0xdd19903"
      },
      {
        "uid": "0xdd31fa2"
      },
      {
        "uid": "0xdd4a641"
      },
      {
        "uid": "0xdddce01"
      },
      {
        "uid": "0xde261e3"
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 107306,
      "processing_ns": 2227158,
      "encoding_ns": 180105,
      "assign_timestamp_ns": 934799,
      "total_ns": 3565435
    },
    "txn": {
      "start_ts": 100168,
      "hash": "3b35d887a1b49dc2067fc57a46822f4c74f36140991b26e56740291c29a2636e"
    },
    "metrics": {
      "num_uids": {
        "_total": 163,
        "dgraph.type": 0,
        "uid": 163
      }
    }
  }
}

```

Thanks in advance

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [June 23, 2023, 6:04pm UTC](https://discuss.dgraph.io/t/how-to-query-for-arbitrary-predicate-and-get-the-uids-along-with-the-predicate/18741/2 "2023-06-23T18:04:16Z")

</div>

Have you added types and did your data have the dgraph.type assigned?

---

<div class="post-metadata">

**Author:** ![Orneyfish](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/orneyfish/32/10558_2.png) [@Orneyfish](https://discuss.dgraph.io/u/Orneyfish)\
**Post date:** [June 26, 2023, 7:00am UTC](https://discuss.dgraph.io/t/how-to-query-for-arbitrary-predicate-and-get-the-uids-along-with-the-predicate/18741/3 "2023-06-26T07:00:47Z")

</div>

Hi I have added the dgraph.type for the classes.

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [June 26, 2023, 1:27pm UTC](https://discuss.dgraph.io/t/how-to-query-for-arbitrary-predicate-and-get-the-uids-along-with-the-predicate/18741/4 "2023-06-26T13:27:53Z")

</div>

Do you want to take this discussion to Discord for better back-and-forth discussions?

> **[Join the Dgraph Users Community (unofficial) Discord Server!](https://discord.com/invite/5vsjb8ZBZp)**
>
> Check out the Dgraph Users Community (unofficial) community on Discord - hang out with 491 other members and enjoy free voice and text chat.

I’m not sure what you meant by `classes`? Did you mean you added it to the data nodes or your schema? Can you provide your schema? Can you query a node by uid and select all of the fields to show what data actually exists?

---

<div class="post-metadata">

**Author:** ![Orneyfish](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/orneyfish/32/10558_2.png) [@Orneyfish](https://discuss.dgraph.io/u/Orneyfish)\
**Post date:** [June 26, 2023, 2:11pm UTC](https://discuss.dgraph.io/t/how-to-query-for-arbitrary-predicate-and-get-the-uids-along-with-the-predicate/18741/5 "2023-06-26T14:11:08Z")

</div>

See for my case the tree kind of look like this:  
Document – → Section – → subSection .  
I want to get one single query to get all the uids that are in this chain. I will only specify document uid.  
Now the issue is I want to make the predicate arbitrary while writing this query.

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [June 26, 2023, 3:06pm UTC](https://discuss.dgraph.io/t/how-to-query-for-arbitrary-predicate-and-get-the-uids-along-with-the-predicate/18741/6 "2023-06-26T15:06:44Z")

</div>

> Now the issue is I want to make the predicate arbitrary while writing this query

You can make it arbitrary to a limited extent. And that extent is that it must be defined in your schema types.

Let’s say you have this schema:

```auto
sections: [uid] .
paragraphs: [uid] .
name: string .
title: string .
text: string .

type Document {
  name
  sections
}

type Section {
  sections
  paragraphs
}

type Note {
  text
}

type Paragraph {
  text
}

```

With some data:

```plaintext
  _:doc1 <dgraph.type> "Document" .
  _:doc1 <name> "My First Document" .
  _:doc1 <sections> _:section1 .
  _:section1 <dgraph.type> "Section" .
  _:section1 <title> "foo" .
  _:section1 <sections> _:section2 .
  _:section2 <dgraph.type> "Section" .
  _:section2 <title> "bar" .
  _:section1 <sections> _:section3 .
  _:section3 <dgraph.type> "Section" .
  _:section3 <title> "baz" .
  _:doc1 <notes> _:note1 .
  _:note1 <dgraph.type> "Note" .
  _:note1 <text> "Lorem Ipsum" .

```

Now you can arbitrary query for sections and paragraphs of the document, but not the notes, because the note predicate is not part of the type schema for the `Document` not the `Section`

```auto
query {
  docs(func: type(Document)) {
    uid
    expand(_all_) {
      uid
      expand(_all_) {
        uid
      }
    }
  }
}

```

If you want the `notes` that may exist but are not included within the types, then you would have to add them imperatively:

```auto
query {
  docs(func: type(Document)) {
    uid
    notes {
      uid
      text
    }
    expand(_all_) {
      uid
      expand(_all_) {
        uid
      }
    }
  }
}

```

* * *

This may be confusing because in an older version of Dgraph, the `expand(_all_)` use to include every predicate that existed for the nodes selected. But in recent versions the algorithm was changed to only included the typed predicates.

And FWIW, this is also the case for deletes as well. Older Dgraph versions use to delete all predicates that were attached to a node with the `S**` delete methods, but now, only predicates specifically typed to the node are deleted.

And if for whatever reason your nodes don’t have a `dgraph.type` predicate, then you can not use the `expand(_all_)` nor the `S**` syntax effectively. This almost forces you to use Dgraph in a typed manner instead of the older envision of a schema-less database. The predicates do get generated if you just provide the data, but the types and predicates those types include are not generated for you.

[https://dgraph.io/docs/dql/dql-schema/#when-to-use-node-types](https://dgraph.io/docs/dql/dql-schema/#when-to-use-node-types)

> Node types are optional, but there are two use cases where actually knowing the list of potential predicates of a node is necessary:
> 
> - deleting all the information about a node: this is the `delete { <uid> * * . }` mutation.
> - retrieving all the predicates of a given node: this is done using the [`expand(_all_)`](https://dgraph.io/docs/query-language/expand-predicates/) feature of DQL.
> 
> The Dgraph node types are used in those 2 use cases: when executing the `delete all predicates` mutation or the `expand all` query, Dgraph will check if the node has a `dgraph.type` predicate. If so, the engine is using the declared type to find the list of predicates and apply the delete or the expand on all of them.
> 
> When nodes have a type (i.e have a `dgraph.type` predicate), then you can use the function [`type()`](https://dgraph.io/docs/dql/dql-syntax/dql-query/#node-criteria-used-by-root-function-or-by-filter) in queries.
> 
> > Warning `delete { <uid> * * . }` will only delete the predicates declared in the type. You may have added other predicates by running DQL mutation on this node: the node may still exist after the operation if it holds predicates not declared in the node type. `<>`
