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

Method rewriteAndExecute

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

Source from the content-addressed store, hash-verified

174}
175
176func (qr *customDQLQueryResolver) rewriteAndExecute(ctx context.Context,
177 query schema.Query) *Resolved {
178 dgraphQueryDuration := &schema.LabeledOffsetDuration{Label: "query"}
179 ext := &schema.Extensions{
180 Tracing: &schema.Trace{
181 Execution: &schema.ExecutionTrace{
182 Resolvers: []*schema.ResolverTrace{
183 {Dgraph: []*schema.LabeledOffsetDuration{dgraphQueryDuration}},
184 },
185 },
186 },
187 }
188
189 emptyResult := func(err error) *Resolved {
190 resolved := EmptyResult(query, err)
191 resolved.Extensions = ext
192 return resolved
193 }
194
195 dgQuery := query.DQLQuery()
196 args := query.Arguments()
197 vars := make(map[string]string)
198 for k, v := range args {
199 // dgoapi.Request{}.Vars accepts only string values for variables,
200 // so need to convert all variable values to string
201 vStr, err := convertScalarToString(v)
202 if err != nil {
203 return emptyResult(schema.GQLWrapf(err, "couldn't convert argument %s to string", k))
204 }
205 // the keys in dgoapi.Request{}.Vars are assumed to be prefixed with $
206 vars["$"+k] = vStr
207 }
208
209 queryTimer := newtimer(ctx, &dgraphQueryDuration.OffsetDuration)
210 queryTimer.Start()
211 resp, err := qr.executor.Execute(ctx, &dgoapi.Request{Query: dgQuery, Vars: vars,
212 ReadOnly: true}, nil)
213 queryTimer.Stop()
214
215 if err != nil {
216 return emptyResult(schema.GQLWrapf(err, "Dgraph query failed"))
217 }
218 ext.TouchedUids = resp.GetMetrics().GetNumUids()[touchedUidsKey]
219
220 var respJson map[string]interface{}
221 if err = schema.Unmarshal(resp.Json, &respJson); err != nil {
222 return emptyResult(schema.GQLWrapf(err, "couldn't unmarshal Dgraph result"))
223 }
224
225 resolved := DataResult(query, respJson, nil)
226 resolved.Extensions = ext
227 return resolved
228}
229
230func resolveIntrospection(ctx context.Context, q schema.Query) *Resolved {
231 data, err := schema.Introspect(q)

Callers 1

ResolveMethod · 0.95

Calls 12

GQLWrapfFunction · 0.92
UnmarshalFunction · 0.92
EmptyResultFunction · 0.85
convertScalarToStringFunction · 0.85
newtimerFunction · 0.85
DataResultFunction · 0.85
GetNumUidsMethod · 0.80
DQLQueryMethod · 0.65
ArgumentsMethod · 0.65
StartMethod · 0.65
ExecuteMethod · 0.65
StopMethod · 0.65

Tested by

no test coverage detected