Dgraph Query Language (DQL) includes support for custom logic. Specify the DQL query you want to execute and the Dgraph GraphQL API will execute it
We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
Dgraph Query Language (DQL) lets you build custom resolvers logic that goes beyond what’s possible with the GraphQL CRUD API.
To define a DQL custom query, use the notation:
Since v21.03, you can also subscribe to custom DQL queries.
For example, lets say you had following schema:
and you wanted to query tweets containing some particular text sorted by the number of followers their author has. Then, this isn’t possible with the automatically generated CRUD API. Similarly, let’s say you have a table sort of UI component in your app which displays only a user’s name and the number of tweets done by that user. Doing this with the auto-generated CRUD API would require you to fetch unnecessary data at client side, and then employ client side logic to find the count. Instead, all this could simply be achieved by specifying a DQL query for such custom use-cases.
So, you would need to modify your schema like this:
Now, if you run following query, it would fetch you the tweets containing “GraphQL” in their text, sorted by the number of followers their author has:
There are following points to note while specifying the DQL query for such custom resolvers:
Boolean
, Int
, Float
,
etc are allowed. Lists and Object types aren’t allowed to be used as variables
with DQL queries.__typename
in GraphQL query, then you should add dgraph.type
as a field in
DQL query without any alias. This isn’t required for types, only for
interfaces.@withSubscription
directive. See
the Subscriptions article for more
information.Dgraph Query Language (DQL) includes support for custom logic. Specify the DQL query you want to execute and the Dgraph GraphQL API will execute it
We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
Dgraph Query Language (DQL) lets you build custom resolvers logic that goes beyond what’s possible with the GraphQL CRUD API.
To define a DQL custom query, use the notation:
Since v21.03, you can also subscribe to custom DQL queries.
For example, lets say you had following schema:
and you wanted to query tweets containing some particular text sorted by the number of followers their author has. Then, this isn’t possible with the automatically generated CRUD API. Similarly, let’s say you have a table sort of UI component in your app which displays only a user’s name and the number of tweets done by that user. Doing this with the auto-generated CRUD API would require you to fetch unnecessary data at client side, and then employ client side logic to find the count. Instead, all this could simply be achieved by specifying a DQL query for such custom use-cases.
So, you would need to modify your schema like this:
Now, if you run following query, it would fetch you the tweets containing “GraphQL” in their text, sorted by the number of followers their author has:
There are following points to note while specifying the DQL query for such custom resolvers:
Boolean
, Int
, Float
,
etc are allowed. Lists and Object types aren’t allowed to be used as variables
with DQL queries.__typename
in GraphQL query, then you should add dgraph.type
as a field in
DQL query without any alias. This isn’t required for types, only for
interfaces.@withSubscription
directive. See
the Subscriptions article for more
information.