@@ -40,26 +40,91 @@ func init() {
40
40
cache = sessions .GetCache ()
41
41
}
42
42
43
+ /* Raw bridges pass the raw query from the request context to Dgraph.
44
+ * @Warning: It looses transformation that eventually happen in the resolvers/directives.
45
+ */
43
46
44
- func (r * queryResolver ) Gqlgen2DgraphQueryResolver (ctx context.Context , data interface {}) error {
45
- return DgraphRawQueryResolver (ctx , data , r .db )
47
+ func (r * queryResolver ) DgraphBridgeRaw (ctx context.Context , data interface {}) error {
48
+ err := DgraphQueryResolverRaw (ctx , r .db , data )
49
+ return postGqlProcess (ctx , r .db , data , err )
46
50
}
47
51
48
- func (r * mutationResolver ) Gqlgen2DgraphQueryResolver (ctx context.Context , data interface {}) error {
49
- return DgraphRawQueryResolver (ctx , data , r .db )
52
+ func (r * mutationResolver ) DgraphBridgeRaw (ctx context.Context , data interface {}) error {
53
+ err := DgraphQueryResolverRaw (ctx , r .db , data )
54
+ return postGqlProcess (ctx , r .db , data , err )
50
55
}
51
56
52
- //func (r *mutationResolver) Gqlgen2DgraphMutationResolver(ctx context.Context, ipts interface{}, data interface{}) error {
53
- // return DgraphQueryResolver(ctx, ipts, data, r.db)
54
- //}
57
+ /* Those bridges rebuild the query from the request context preloads, and uses the input
58
+ * parameters from the gqlgen resolvers whuch reflext the modifications in the resolvers/directives.
59
+ * @Warning: It looses eventual directive in the query graph (@cascade, @skip etc)
60
+ */
61
+
62
+ func (r * queryResolver ) DgraphGetBridge (ctx context.Context , maps map [string ]interface {}, data interface {}) error {
63
+ panic (fmt .Errorf ("not implemented" ))
64
+ }
65
+
66
+ func (r * queryResolver ) DgraphQueryBridge (ctx context.Context , maps map [string ]interface {}, data interface {}) error {
67
+ panic (fmt .Errorf ("not implemented" ))
68
+ }
69
+
70
+ func (r * mutationResolver ) DgraphAddBridge (ctx context.Context , input interface {}, upsert * bool , data interface {}) error {
71
+ err := DgraphAddResolver (ctx , r .db , input , upsert , data )
72
+ return postGqlProcess (ctx , r .db , data , err )
73
+ }
74
+
75
+ func (r * mutationResolver ) DgraphUpdateBridge (ctx context.Context , input interface {}, data interface {}) error {
76
+ err := DgraphUpdateResolver (ctx , r .db , input , data )
77
+ return postGqlProcess (ctx , r .db , data , err )
78
+ }
79
+
80
+ func (r * mutationResolver ) DgraphDeleteBridge (ctx context.Context , filter interface {}, data interface {}) error {
81
+ err := DgraphDeleteResolver (ctx , r .db , filter , data )
82
+ return postGqlProcess (ctx , r .db , data , err )
83
+ }
55
84
56
85
/*
57
86
*
58
87
* Dgraph-Gqlgen bridge logic
59
88
*
60
89
*/
61
90
62
- func DgraphRawQueryResolver (ctx context.Context , data interface {}, db * db.Dgraph ) error {
91
+ func DgraphAddResolver (ctx context.Context , db * db.Dgraph , input interface {}, upsert * bool , data interface {}) error {
92
+ _ , uctx , err := auth .GetUserContext (ctx )
93
+ if err != nil { return tools .LogErr ("Access denied" , err ) }
94
+
95
+ _ , typeName , _ , err := queryTypeFromGraphqlContext (ctx )
96
+ if err != nil { return tools .LogErr ("DgraphQueryResolver" , err ) }
97
+
98
+ err = db .AddExtra (* uctx , typeName , input , upsert , GetQueryGraph (ctx ), data )
99
+ return err
100
+ }
101
+
102
+ func DgraphUpdateResolver (ctx context.Context , db * db.Dgraph , input interface {}, data interface {}) error {
103
+ _ , uctx , err := auth .GetUserContext (ctx )
104
+ if err != nil { return tools .LogErr ("Access denied" , err ) }
105
+
106
+ _ , typeName , _ , err := queryTypeFromGraphqlContext (ctx )
107
+ if err != nil { return tools .LogErr ("DgraphQueryResolver" , err ) }
108
+
109
+ err = db .UpdateExtra (* uctx , typeName , input , GetQueryGraph (ctx ), data )
110
+ return err
111
+ }
112
+
113
+ func DgraphDeleteResolver (ctx context.Context , db * db.Dgraph , input interface {}, data interface {}) error {
114
+ _ , uctx , err := auth .GetUserContext (ctx )
115
+ if err != nil { return tools .LogErr ("Access denied" , err ) }
116
+
117
+ _ , typeName , _ , err := queryTypeFromGraphqlContext (ctx )
118
+ if err != nil { return tools .LogErr ("DgraphQueryResolver" , err ) }
119
+
120
+ err = db .DeleteExtra (* uctx , typeName , input , GetQueryGraph (ctx ), data )
121
+ return err
122
+ }
123
+
124
+ // @deprecated: Follow the Gql request to Dgraph.
125
+ // This use raw query from the request context and thus won't propage change
126
+ // of the input that may happend in the resolvers.
127
+ func DgraphQueryResolverRaw (ctx context.Context , db * db.Dgraph , data interface {}) error {
63
128
// How to get the query args ? https://github.com/99designs/gqlgen/issues/1144
64
129
// for k, a := range rc.Args {
65
130
@@ -86,7 +151,7 @@ func DgraphRawQueryResolver(ctx context.Context, data interface{}, db *db.Dgraph
86
151
rawQuery = reg .ReplaceAllString (rawQuery , "history:[]" )
87
152
88
153
// If Graphql variables are given...
89
- t := strings .ToLower (typeName )
154
+ t := strings .ToLower (typeName ) // @DEBUG: only the first letter must lowered ?!
90
155
if variables [t ] != nil && variables [t ].(map [string ]interface {})["set" ] != nil {
91
156
s := variables [t ].(map [string ]interface {})["set" ].(map [string ]interface {})
92
157
s ["history" ] = nil
@@ -105,10 +170,14 @@ func DgraphRawQueryResolver(ctx context.Context, data interface{}, db *db.Dgraph
105
170
// Send request
106
171
uctx := auth .GetUserContextOrEmpty (ctx )
107
172
err = db .QueryGql (uctx , "rawQuery" , reqInput , data )
108
- if data != nil && err != nil {
173
+ return err
174
+ }
175
+
176
+ func postGqlProcess (ctx context.Context , db * db.Dgraph , data interface {}, errors error ) error {
177
+ if data != nil && errors != nil {
109
178
// Gqlgen ignore the data if there is an error returned
110
179
// see https://github.com/99designs/gqlgen/issues/1191
111
- //graphql.AddErrorf(ctx, err .Error())
180
+ //graphql.AddErrorf(ctx, errors .Error())
112
181
113
182
// Nodes query can return null field if Node are hidden
114
183
// but children are not. The source ends up to be a tension where
@@ -118,14 +187,16 @@ func DgraphRawQueryResolver(ctx context.Context, data interface{}, db *db.Dgraph
118
187
if (string (d ) == "null" ) {
119
188
// If there is really no data, show the graphql error
120
189
// otherwise, fail silently.
121
- return err
190
+ return errors
122
191
}
123
- fmt .Println ("Dgraph Error Ignored: " , err .Error ())
192
+ fmt .Println ("Dgraph Error Ignored: " , errors .Error ())
124
193
return nil
125
- } else if err != nil || data == nil {
126
- return err
194
+ } else if errors != nil || data == nil {
195
+ return errors
127
196
}
128
197
198
+ uctx := auth .GetUserContextOrEmpty (ctx )
199
+ if uctx .Username == "" { return errors }
129
200
// Post processing (@meta_patch) / Post hook operation.
130
201
// If the query go trough the validation stack, execute.
131
202
//if f, _ := cache.Do("GETDEL", uctx.Username + "meta_patch_f"); f != nil {
@@ -144,51 +215,5 @@ func DgraphRawQueryResolver(ctx context.Context, data interface{}, db *db.Dgraph
144
215
//fmt.Println("Redis error: ", err)
145
216
}
146
217
147
- return err
218
+ return errors
148
219
}
149
-
150
- //// Mutation type Enum
151
- //type mutationType string
152
- //const (
153
- // AddMut mutationType = "add"
154
- // UpdateMut mutationType = "update"
155
- // DelMut mutationType = "delete"
156
- //)
157
- //type MutationContext struct {
158
- // type_ mutationType
159
- // argName string
160
- //}
161
-
162
-
163
- //// @Debug: GetPreloads loose subfilter in payload(in QueryGraph)
164
- //func DgraphQueryResolver(ctx context.Context, ipts interface{}, data interface{}, db *db.Dgraph) error {
165
- // mutCtx := ctx.Value("mutation_context").(MutationContext)
166
- //
167
- // /* Rebuild the Graphql inputs request from this context */
168
- // rc := graphql.GetResolverContext(ctx)
169
- // queryName := rc.Field.Name
170
- //
171
- // // Format inputs
172
- // inputs, _ := json.Marshal(ipts)
173
- // // If inputs needs to get modified, see tools.StructToMap() usage
174
- // // in order to to get the struct in the schema.resolver caller.
175
- //
176
- // // Format collected fields
177
- // inputType := strings.Split(fmt.Sprintf("%T", rc.Args[mutCtx.argName]), ".")[1]
178
- // queryGraph := GetQueryGraph(ctx)
179
- //
180
- // // Build the graphql raw request
181
- // reqInput := map[string]string{
182
- // "QueryName": queryName, // function name (e.g addUser)
183
- // "InputType": inputType, // input type name (e.g AddUserInput)
184
- // "QueryGraph": queryGraph, // output data
185
- // "InputPayload": string(inputs), // inputs data
186
- // }
187
- //
188
- // op := string(mutCtx.type_)
189
- //
190
- // // Send request
191
- // uctx := auth.GetUserContextOrEmpty(ctx)
192
- // err = db.QueryGql(uctx, op, reqInput, data)
193
- // return err
194
- //}
0 commit comments