checkGraphQLResponse compares a response body with the query gh sent. Paths in problems use the same dotted form as ghmock set --at.
(schema *ast.Schema, op *ast.OperationDefinition, body []byte, variables map[string]any)
| 132 | // checkGraphQLResponse compares a response body with the query gh sent. Paths |
| 133 | // in problems use the same dotted form as ghmock set --at. |
| 134 | func checkGraphQLResponse(schema *ast.Schema, op *ast.OperationDefinition, body []byte, variables map[string]any) []string { |
| 135 | if len(bytes.TrimSpace(body)) == 0 { |
| 136 | return []string{"body: missing, but a GraphQL response needs data or errors"} |
| 137 | } |
| 138 | v, err := decodeBody(body) |
| 139 | if err != nil { |
| 140 | return []string{fmt.Sprintf("body: not valid JSON: %v", err)} |
| 141 | } |
| 142 | top, ok := v.(map[string]any) |
| 143 | if !ok { |
| 144 | return []string{fmt.Sprintf("body: should be an object, but is %s", describeJSON(v))} |
| 145 | } |
| 146 | |
| 147 | w := &gqlWalker{schema: schema, variables: variables} |
| 148 | errs, hasErrors := top["errors"] |
| 149 | if hasErrors { |
| 150 | w.checkErrors(errs) |
| 151 | } |
| 152 | data := top["data"] |
| 153 | if data == nil { |
| 154 | if !hasErrors { |
| 155 | w.addf("body", "has neither data nor errors") |
| 156 | } |
| 157 | return dedupe(w.problems) |
| 158 | } |
| 159 | |
| 160 | root := rootType(schema, op.Operation) |
| 161 | if root == nil { |
| 162 | w.addf("data", "GitHub's schema has no %s type", op.Operation) |
| 163 | return dedupe(w.problems) |
| 164 | } |
| 165 | obj, ok := data.(map[string]any) |
| 166 | if !ok { |
| 167 | w.addf("data", "should be an object, but is %s", describeJSON(data)) |
| 168 | return dedupe(w.problems) |
| 169 | } |
| 170 | w.checkObject("data", obj, root, op.SelectionSet, true) |
| 171 | return dedupe(w.problems) |
| 172 | } |
| 173 | |
| 174 | func rootType(schema *ast.Schema, op ast.Operation) *ast.Definition { |
| 175 | switch op { |
no test coverage detected