Skip to content

Commit 4715fda

Browse files
feat(graphql): output translated DQL query when the graphql debug superflag is set (#9280)
**Description** This change adds the translated DQL query to the GraphQL's `extension` attribute set when the GraphQL debug superflag is set to true. In Dgraph, GraphQL queries are translated to DQL before entering the query system. This change allows for an inspection of what the translator is doing behind the scenes. Potential Use Cases: * learning DQL * aiding few shot prompting in LMs * use in Dgraph "dashboards" or other helper apps Note: this is for queries (reads) only. Mutations in DQL are pretty straightforward, but support for them could be added in the future. ![image](https://github.com/user-attachments/assets/a818ef04-5189-4bc1-a3d7-7b7a5d2fbe70) **Checklist** - [x] Code compiles correctly and linting passes locally - [x] For all _code_ changes, an entry added to the `CHANGELOG.md` file describing and linking to this PR - [x] Tests added for new functionality, or regression tests for bug fixes added as applicable - [x] [Docs PR](dgraph-io/dgraph-docs#689)
1 parent 66b6d69 commit 4715fda

File tree

7 files changed

+43
-2
lines changed

7 files changed

+43
-2
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project will
66
adhere to [Semantic Versioning](https://semver.org) starting `v22.0.0`.
77

8+
## [v24.X.X] - YYYY-MM-DD
9+
10+
- **GraphQL**
11+
12+
- feat(graphql): adds translated DQL to extensions response when the "--graphql debug" super flag
13+
is set to true (#9280)
14+
15+
[v24.X.X]: https://github.com/hypermodeinc/dgraph/compare/v24.0.5...v24.X.X
16+
817
## [v24.0.5] - 2024-11-05
918

1019
[v24.0.5]: https://github.com/hypermodeinc/dgraph/compare/v24.0.4...v24.0.5

graphql/e2e/common/common.go

+1
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ func RunAll(t *testing.T) {
848848
t.Run("multiple operations", multipleOperations)
849849
t.Run("query post with author", queryPostWithAuthor)
850850
t.Run("queries have extensions", queriesHaveExtensions)
851+
t.Run("queries with debug flag have dql query in extensions", queriesWithDebugFlagHaveDQLQueryInExtensions)
851852
t.Run("queries have touched_uids even if there are GraphQL errors", erroredQueriesHaveTouchedUids)
852853
t.Run("alias works for queries", queryWithAlias)
853854
t.Run("multiple aliases for same field in query", queryWithMultipleAliasOfSameField)

graphql/e2e/common/query.go

+21
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io"
2525
"math/rand"
2626
"net/http"
27+
"regexp"
2728
"sort"
2829
"strconv"
2930
"strings"
@@ -2097,6 +2098,26 @@ func queriesHaveExtensions(t *testing.T) {
20972098
require.Greater(t, int(gqlResponse.Extensions[touchedUidskey].(float64)), 0)
20982099
}
20992100

2101+
func queriesWithDebugFlagHaveDQLQueryInExtensions(t *testing.T) {
2102+
query := &GraphQLParams{
2103+
Query: `query {
2104+
queryPost {
2105+
title
2106+
}
2107+
}`,
2108+
}
2109+
2110+
gqlResponse := query.ExecuteAsPost(t, GraphqlURL+"?debug=true")
2111+
RequireNoGQLErrors(t, gqlResponse)
2112+
2113+
// Needed for directives tests (Post remapped to myPost)
2114+
pattern := regexp.MustCompile(`query \{\n\s*query(?:Post|myPost)\(func: type\((?:Post|myPost)\)\) \{\n\s*Post\.title : (?:Post|myPost)\.title\n\s*dgraph\.uid : uid\n\s*\}\n\}`)
2115+
2116+
require.Contains(t, gqlResponse.Extensions, "dql_query")
2117+
require.NotEmpty(t, gqlResponse.Extensions["dql_query"])
2118+
require.True(t, pattern.MatchString(gqlResponse.Extensions["dql_query"].(string)))
2119+
}
2120+
21002121
func erroredQueriesHaveTouchedUids(t *testing.T) {
21012122
country1 := addCountry(t, postExecutor)
21022123
country2 := addCountry(t, postExecutor)

graphql/e2e/directives/docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ services:
3737
/gobin/dgraph ${COVERAGE_OUTPUT} alpha --telemetry "reports=false; sentry=false;"
3838
--zero=zero1:5080 --expose_trace --profile_mode block --block_rate 10 --logtostderr -v=2
3939
--my=alpha1:7080 --security "whitelist=10.0.0.0/8,172.16.0.0/12,192.168.0.0/16;" --graphql
40-
"lambda-url=http://lambda:8686/graphql-worker;" --trace "ratio=1.0;"
40+
"lambda-url=http://lambda:8686/graphql-worker; debug=true;" --trace "ratio=1.0;"
4141

4242
lambda:
4343
image: dgraph/dgraph-lambda:latest

graphql/e2e/normal/docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ services:
3737
/gobin/dgraph ${COVERAGE_OUTPUT} alpha --telemetry "reports=false; sentry=false;"
3838
--zero=zero1:5080 --expose_trace --profile_mode block --block_rate 10 --logtostderr -v=2
3939
--my=alpha1:7080 --security "whitelist=10.0.0.0/8,172.16.0.0/12,192.168.0.0/16;" --graphql
40-
"lambda-url=http://lambda:8686/graphql-worker;" --trace "ratio=1.0;"
40+
"lambda-url=http://lambda:8686/graphql-worker; debug=true;" --trace "ratio=1.0;"
4141

4242
lambda:
4343
image: dgraph/dgraph-lambda:latest

graphql/resolve/query.go

+3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ func (qr *queryResolver) rewriteAndExecute(ctx context.Context, query schema.Que
139139
}
140140

141141
ext.TouchedUids = resp.GetMetrics().GetNumUids()[touchedUidsKey]
142+
if x.Config.GraphQL.GetBool("debug") {
143+
ext.DQLQuery = qry
144+
}
142145
resolved := &Resolved{
143146
Data: resp.GetJson(),
144147
Field: query,

graphql/schema/response.go

+7
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ func (r *Response) Output() interface{} {
182182
type Extensions struct {
183183
TouchedUids uint64 `json:"touched_uids,omitempty"`
184184
Tracing *Trace `json:"tracing,omitempty"`
185+
DQLQuery string `json:"dql_query,omitempty"`
185186
}
186187

187188
// GetTouchedUids returns TouchedUids
@@ -205,6 +206,12 @@ func (e *Extensions) Merge(ext *Extensions) {
205206
} else {
206207
e.Tracing.Merge(ext.Tracing)
207208
}
209+
210+
if e.DQLQuery == "" {
211+
e.DQLQuery = ext.DQLQuery
212+
} else {
213+
e.DQLQuery = e.DQLQuery + "\n" + ext.DQLQuery
214+
}
208215
}
209216

210217
// Trace : Apollo Tracing is a GraphQL extension for tracing resolver performance.Response

0 commit comments

Comments
 (0)