| 158 | } |
| 159 | |
| 160 | func (c *checker) check(e loggedRequest, resp *response, validateVariables bool) checkResult { |
| 161 | var res checkResult |
| 162 | if e.GraphQL != nil { |
| 163 | schema, err := c.graphQL() |
| 164 | if err != nil { |
| 165 | return res |
| 166 | } |
| 167 | operation := e.GraphQL.Operation |
| 168 | if !validateVariables && operation == "" && strings.HasPrefix(e.Name, "graphql ") { |
| 169 | operation = strings.TrimPrefix(e.Name, "graphql ") |
| 170 | if operation == "<anonymous>" { |
| 171 | operation = "" |
| 172 | } |
| 173 | } |
| 174 | op, problems := checkGraphQLQuery(schema, e.GraphQL.Query, operation) |
| 175 | res.queryProblems = problems |
| 176 | var variables map[string]any |
| 177 | if validateVariables { |
| 178 | variables = map[string]any{} |
| 179 | var variablesErr error |
| 180 | if len(e.GraphQL.Variables) > 0 { |
| 181 | variablesErr = json.Unmarshal(e.GraphQL.Variables, &variables) |
| 182 | } |
| 183 | if variablesErr == nil && op != nil && len(problems) == 0 { |
| 184 | var coerced map[string]any |
| 185 | coerced, variablesErr = validator.VariableValues(schema, op, variables) |
| 186 | if variablesErr == nil { |
| 187 | variables = coerced |
| 188 | } |
| 189 | } |
| 190 | if variablesErr != nil { |
| 191 | res.queryProblems = append(res.queryProblems, "variables: "+variablesErr.Error()) |
| 192 | } |
| 193 | } |
| 194 | if resp == nil || resp.OffSchema { |
| 195 | return res |
| 196 | } |
| 197 | switch { |
| 198 | case op == nil: |
| 199 | res.note = "gh's query couldn't be read" |
| 200 | case resp.status() != 200: |
| 201 | res.note = fmt.Sprintf("status %d has no GraphQL result to check", resp.status()) |
| 202 | default: |
| 203 | res.responseProblems = checkGraphQLResponse(schema, op, resp.bodyBytes(), variables) |
| 204 | } |
| 205 | return res |
| 206 | } |
| 207 | |
| 208 | if resp == nil || resp.OffSchema { |
| 209 | return res |
| 210 | } |
| 211 | if !isAPIHost(e.Host) { |
| 212 | res.note = "not a REST API host" |
| 213 | return res |
| 214 | } |
| 215 | idx, err := c.rest() |
| 216 | if err != nil { |
| 217 | return res |