ProcessPersistedQuery stores and retrieves persisted queries by following waterfall logic: 1. If sha256Hash is not provided process queries without persisting 2. If sha256Hash is provided try retrieving persisted queries 2a. Persisted Query not found i) If query is not provided then throw "Persisted
(ctx context.Context, gqlReq *schema.Request)
| 32 | // ii) If query is provided then match query retrieved, if identical do nothing else |
| 33 | // throw "query does not match persisted query" |
| 34 | func ProcessPersistedQuery(ctx context.Context, gqlReq *schema.Request) error { |
| 35 | query := gqlReq.Query |
| 36 | sha256Hash := gqlReq.Extensions.PersistedQuery.Sha256Hash |
| 37 | |
| 38 | if sha256Hash == "" { |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | if x.WorkerConfig.AclEnabled { |
| 43 | accessJwt, err := x.ExtractJwt(ctx) |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | if _, err := validateToken(accessJwt); err != nil { |
| 48 | return err |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | join := sha256Hash + query |
| 53 | |
| 54 | queryForSHA := `query Me($join: string){ |
| 55 | me(func: eq(dgraph.graphql.p_query, $join)){ |
| 56 | dgraph.graphql.p_query |
| 57 | } |
| 58 | }` |
| 59 | variables := map[string]string{ |
| 60 | "$join": join, |
| 61 | } |
| 62 | req := &Request{ |
| 63 | req: &api.Request{ |
| 64 | Query: queryForSHA, |
| 65 | Vars: variables, |
| 66 | ReadOnly: true, |
| 67 | }, |
| 68 | doAuth: NoAuthorize, |
| 69 | } |
| 70 | storedQuery, err := (&Server{}).doQuery(ctx, req) |
| 71 | |
| 72 | if err != nil { |
| 73 | glog.Errorf("Error while querying sha %s", sha256Hash) |
| 74 | return err |
| 75 | } |
| 76 | |
| 77 | type shaQueryResponse struct { |
| 78 | Me []struct { |
| 79 | PersistedQuery string `json:"dgraph.graphql.p_query"` |
| 80 | } `json:"me"` |
| 81 | } |
| 82 | |
| 83 | shaQueryRes := &shaQueryResponse{} |
| 84 | if len(storedQuery.Json) > 0 { |
| 85 | if err := json.Unmarshal(storedQuery.Json, shaQueryRes); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if len(shaQueryRes.Me) == 0 { |
| 91 | if query == "" { |
no test coverage detected