(database, query string, vars json.RawMessage, role string)
| 2223 | } |
| 2224 | |
| 2225 | func (gj *graphjinEngine) explainQueryForDatabase(database, query string, vars json.RawMessage, role string) (*QueryExplanation, error) { |
| 2226 | if !gj.anyDatabaseReady() { |
| 2227 | return nil, fmt.Errorf("schema not initialized") |
| 2228 | } |
| 2229 | |
| 2230 | dbCtx, ok := gj.GetDatabase(database) |
| 2231 | if !ok { |
| 2232 | return nil, fmt.Errorf("database not found: %s", database) |
| 2233 | } |
| 2234 | |
| 2235 | queryBytes := []byte(query) |
| 2236 | h, err := graph.FastParseBytes(queryBytes) |
| 2237 | if err != nil { |
| 2238 | return &QueryExplanation{ |
| 2239 | Database: database, |
| 2240 | Errors: []string{fmt.Sprintf("parse error: %s", err.Error())}, |
| 2241 | }, nil |
| 2242 | } |
| 2243 | |
| 2244 | r := gj.newGraphqlReq(nil, h.Operation, h.Name, queryBytes, vars) |
| 2245 | s, err := newGState(context.Background(), gj, r) |
| 2246 | if err != nil { |
| 2247 | return &QueryExplanation{ |
| 2248 | Database: database, |
| 2249 | Errors: []string{fmt.Sprintf("state error: %s", err.Error())}, |
| 2250 | }, nil |
| 2251 | } |
| 2252 | if role != "" { |
| 2253 | s.role = role |
| 2254 | } |
| 2255 | |
| 2256 | st := stmt{role: s.role} |
| 2257 | var found bool |
| 2258 | if st.roc, found = gj.roles[s.role]; !found { |
| 2259 | return &QueryExplanation{ |
| 2260 | Operation: h.Operation, |
| 2261 | Name: h.Name, |
| 2262 | Role: s.role, |
| 2263 | Database: database, |
| 2264 | Errors: []string{fmt.Sprintf(`roles '%s' not defined in c.gj.config`, s.role)}, |
| 2265 | }, nil |
| 2266 | } |
| 2267 | |
| 2268 | var compileVars map[string]json.RawMessage |
| 2269 | if len(s.r.aschema) != 0 { |
| 2270 | compileVars = s.r.aschema |
| 2271 | } else { |
| 2272 | compileVars = s.vmap |
| 2273 | } |
| 2274 | |
| 2275 | if err := s.compileForDatabase(st, compileVars, dbCtx); err != nil { |
| 2276 | return &QueryExplanation{ |
| 2277 | Operation: h.Operation, |
| 2278 | Name: h.Name, |
| 2279 | Role: s.role, |
| 2280 | Database: database, |
| 2281 | Errors: []string{err.Error()}, |
| 2282 | }, nil |
no test coverage detected