Skip to content

Commit 4d6221b

Browse files
committed
feat: allow @cascade trick + dgraph query bridge refactor
1 parent 440fdad commit 4d6221b

File tree

9 files changed

+189
-51
lines changed

9 files changed

+189
-51
lines changed

db/gql.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,16 @@ func (dg Dgraph) UpdateValue(uctx model.UserCtx, vertex string, id, k, v string)
344344

345345
// GetDirectives return the list of directives to apply to the given query
346346
// by looking for the pressence of special attributes in the payload graph.
347-
func GetDirectives(pg string) string {
347+
func GetDirectives(pg string) (string, string) {
348348
directives := []string{}
349349
words := strings.Fields(pg)
350350
for _, word := range words {
351351
if word == "cascade_directive" {
352352
directives = append(directives, "@cascade")
353+
pg = strings.ReplaceAll(pg, "cascade_directive", "")
353354
}
354355
}
355-
return strings.Join(directives, " ")
356+
return pg, strings.Join(directives, " ")
356357
}
357358

358359
// Query codec, to be used in the resolver functions
@@ -375,6 +376,8 @@ func (dg Dgraph) QueryExtra(uctx model.UserCtx, vertex string, filter any, order
375376
}{filter, order, first, offset}
376377
varmap, _ := MarshalWithoutNil(filter_)
377378

379+
qg, directives := GetDirectives(qg)
380+
378381
// Build the request template map
379382
reqInput := map[string]string{
380383
"QueryName": queryName, // Query name (e.g addUser)
@@ -383,7 +386,7 @@ func (dg Dgraph) QueryExtra(uctx model.UserCtx, vertex string, filter any, order
383386
"QueryInput": QuoteString(queryInput), // inputs data
384387
"QueryGraph": CleanString(qg, true), // output data
385388
"VarMap": string(varmap), // inputs data
386-
"Directives": GetDirectives(qg),
389+
"Directives": directives,
387390
}
388391

389392
// Send request

graph/dgraph_resolver.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,6 @@ func (r *mutationResolver) DgraphDeleteBridge(ctx context.Context, filter interf
9393
return postGqlProcess(ctx, r.db, data, err)
9494
}
9595

96-
/* Raw bridges pass the raw query from the request context to Dgraph.
97-
* @warning: It looses transformation that eventually happen in the resolvers/directives.
98-
* @warning: It is hard to modify the query with this approache
99-
* @deprecated
100-
*/
101-
102-
func (r *queryResolver) DgraphBridgeRaw(ctx context.Context, data interface{}) error {
103-
err := DgraphQueryResolverRaw(ctx, r.db, data)
104-
return postGqlProcess(ctx, r.db, data, err)
105-
}
106-
107-
func (r *mutationResolver) DgraphBridgeRaw(ctx context.Context, data interface{}) error {
108-
err := DgraphQueryResolverRaw(ctx, r.db, data)
109-
return postGqlProcess(ctx, r.db, data, err)
110-
}
111-
11296
func getUserQueryType(ctx context.Context) (*model.UserCtx, string, error) {
11397
_, uctx, err := auth.GetUserContext(ctx)
11498
if err != nil {
@@ -123,6 +107,9 @@ func getUserQueryType(ctx context.Context) (*model.UserCtx, string, error) {
123107
return uctx, typeName, err
124108
}
125109

110+
// postGqlProcess postprocess output data and error:
111+
// - ignore error if data are returned (cause by @auth rules filtering).
112+
// - handle meta query passed with redis.
126113
func postGqlProcess(ctx context.Context, db *db.Dgraph, data interface{}, errors error) error {
127114
if data != nil && errors != nil {
128115
// Gqlgen ignore the data if there is an error returned
@@ -170,6 +157,22 @@ func postGqlProcess(ctx context.Context, db *db.Dgraph, data interface{}, errors
170157
return errors
171158
}
172159

160+
/* Raw bridges pass the raw query from the request context to Dgraph.
161+
* @warning: It looses transformation that eventually happen in the resolvers/directives.
162+
* @warning: It is hard to modify the query with this approache
163+
* @deprecated
164+
*/
165+
166+
func (r *queryResolver) DgraphBridgeRaw(ctx context.Context, data interface{}) error {
167+
err := DgraphQueryResolverRaw(ctx, r.db, data)
168+
return postGqlProcess(ctx, r.db, data, err)
169+
}
170+
171+
func (r *mutationResolver) DgraphBridgeRaw(ctx context.Context, data interface{}) error {
172+
err := DgraphQueryResolverRaw(ctx, r.db, data)
173+
return postGqlProcess(ctx, r.db, data, err)
174+
}
175+
173176
// @deprecated: Follow the Gql request to Dgraph.
174177
// This use raw query from the request context and thus won't propagate change
175178
// of the input that may happend in the resolvers.

graph/generated/root_.generated.go

Lines changed: 24 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)