MCPcopy
hub / github.com/dgraph-io/dgraph / rewriteAndExecute

Method rewriteAndExecute

graphql/resolve/query.go:90–145  ·  view source on GitHub ↗
(ctx context.Context, query schema.Query)

Source from the content-addressed store, hash-verified

88}
89
90func (qr *queryResolver) rewriteAndExecute(ctx context.Context, query schema.Query) *Resolved {
91 dgraphQueryDuration := &schema.LabeledOffsetDuration{Label: "query"}
92 ext := &schema.Extensions{
93 Tracing: &schema.Trace{
94 Execution: &schema.ExecutionTrace{
95 Resolvers: []*schema.ResolverTrace{
96 {Dgraph: []*schema.LabeledOffsetDuration{dgraphQueryDuration}},
97 },
98 },
99 },
100 }
101
102 emptyResult := func(err error) *Resolved {
103 return &Resolved{
104 // all the auto-generated queries are nullable, but users may define queries with
105 // @custom(dql: ...) which may be non-nullable. So, we need to set the Data field
106 // only if the query was nullable and keep it nil if it was non-nullable.
107 // query.NullResponse() method handles that.
108 Data: query.NullResponse(),
109 Field: query,
110 Err: schema.SetPathIfEmpty(err, query.ResponseName()),
111 Extensions: ext,
112 }
113 }
114
115 dgQuery, vars, err := qr.queryRewriter.Rewrite(ctx, query)
116 if err != nil {
117 return emptyResult(schema.GQLWrapf(err, "couldn't rewrite query %s",
118 query.ResponseName()))
119 }
120 qry := dgraph.AsString(dgQuery)
121
122 queryTimer := newtimer(ctx, &dgraphQueryDuration.OffsetDuration)
123 queryTimer.Start()
124 resp, err := qr.executor.Execute(ctx,
125 &dgoapi.Request{Query: qry, Vars: vars, ReadOnly: true}, query)
126 queryTimer.Stop()
127
128 if err != nil && !x.IsGqlErrorList(err) {
129 err = schema.GQLWrapf(err, "Dgraph query failed")
130 glog.Infof("Dgraph query execution failed : %s", err)
131 }
132
133 ext.TouchedUids = resp.GetMetrics().GetNumUids()[touchedUidsKey]
134 if x.Config.GraphQL.GetBool("debug") {
135 ext.DQLQuery = qry
136 }
137 resolved := &Resolved{
138 Data: resp.GetJson(),
139 Field: query,
140 Err: schema.SetPathIfEmpty(err, query.ResponseName()),
141 Extensions: ext,
142 }
143
144 return resolved
145}
146
147func NewCustomDQLQueryResolver(ex DgraphExecutor) QueryResolver {

Callers 1

ResolveMethod · 0.95

Calls 13

SetPathIfEmptyFunction · 0.92
GQLWrapfFunction · 0.92
AsStringFunction · 0.92
IsGqlErrorListFunction · 0.92
newtimerFunction · 0.85
InfofMethod · 0.80
GetNumUidsMethod · 0.80
NullResponseMethod · 0.65
ResponseNameMethod · 0.65
RewriteMethod · 0.65
StartMethod · 0.65
ExecuteMethod · 0.65

Tested by

no test coverage detected