MCPcopy Create free account
hub / github.com/cli/cli / trimGraphQL

Function trimGraphQL

script/ghmock/trim.go:21–54  ·  view source on GitHub ↗

trimGraphQL removes the fields that a query didn't ask for from a stored GraphQL response, because GitHub only sends what's asked for. One stored response can then serve gh versions that ask for different fields. Fields that the query asks for and the response lacks stay missing, and the schema chec

(body []byte, gql *graphQLRequest, schema *ast.Schema)

Source from the content-addressed store, hash-verified

19// interface applies to an object. Without it, such fragments are kept. The
20// body comes back unchanged if the query or the body can't be read.
21func trimGraphQL(body []byte, gql *graphQLRequest, schema *ast.Schema) []byte {
22 doc, err := parser.ParseQuery(&ast.Source{Name: "query", Input: gql.Query})
23 if err != nil || len(doc.Operations) == 0 {
24 return body
25 }
26 op := doc.Operations.ForName(gql.Operation)
27 if op == nil {
28 return body
29 }
30 v, err := decodeBody(body)
31 if err != nil {
32 return body
33 }
34 top, ok := v.(map[string]any)
35 if !ok {
36 return body
37 }
38 data, ok := top["data"].(map[string]any)
39 if !ok {
40 return body
41 }
42
43 t := &trimmer{doc: doc, schema: schema}
44 t.vars, err = operationVariables(op, gql.Variables)
45 if err != nil {
46 return body
47 }
48 top["data"] = t.object(data, op.SelectionSet)
49 trimmed, err := json.Marshal(top)
50 if err != nil {
51 return body
52 }
53 return trimmed
54}
55
56type trimmer struct {
57 doc *ast.QueryDocument

Callers 1

ServeHTTPMethod · 0.85

Calls 3

objectMethod · 0.95
decodeBodyFunction · 0.85
operationVariablesFunction · 0.85

Tested by

no test coverage detected