{"record":{"id":"c44aa4ae9ca80cf0","repo":"SonarSource/sonarqube","slug":"the-graphql-answer-contains-errors","errorCode":null,"errorMessage":"The GraphQl answer contains errors: ","messagePattern":"The GraphQl answer contains errors: ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"server/sonar-server-common/src/main/java/org/sonar/server/common/graphql/GraphQlClient.java","lineNumber":157,"sourceCode":"\n  private static <T> GsonGraphQlAnswer<T> deserializeValidAnswerOrThrow(Type answerDataType, Response response) {\n    GsonGraphQlAnswer<T> graphQlAnswer;\n    Function<String, GsonGraphQlAnswer<T>> toObject = stringPayload -> GSON.fromJson(stringPayload, answerDataType);\n    Optional<String> bodyString = Optional.empty();\n    try {\n      bodyString = Optional.ofNullable(response.body()).map(body -> {\n        try {\n          return body.string();\n        } catch (IOException e) {\n          throw new IllegalStateException(e);\n        }\n      });\n      graphQlAnswer = bodyString.map(toObject).orElseThrow();\n    } catch (Exception exception) {\n      throw new IllegalStateException(format(\"Error while deserializing GraphQL payload: %s. %s\", exception.getMessage(), bodyString), exception);\n    }\n    if (!graphQlAnswer.isValid()) {\n      throw new IllegalStateException(\"The GraphQl answer contains errors: \" + graphQlAnswer.errors());\n    }\n    return graphQlAnswer;\n  }\n\n}\n","sourceCodeStart":139,"sourceCodeEnd":163,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-server-common/src/main/java/org/sonar/server/common/graphql/GraphQlClient.java#L139-L163","documentation":"If the payload deserializes but the GsonGraphQlAnswer carries a non-empty `errors` array (isValid() false), the client throws an IllegalStateException listing the GraphQL errors. This means the HTTP transport succeeded but the server rejected the query itself — bad field names, missing arguments, permission issues on the queried nodes.","triggerScenarios":"Executing a GraphQL query whose response includes a top-level `errors` array: unknown fields after a server schema change, insufficient permissions on requested entities, invalid arguments, or a partially failing query.","commonSituations":"Client not upgraded after a server upgrade (schema drift); querying components/projects the token's user cannot see; hard-coded queries referencing removed fields; nullability issues surfacing as errors.","solutions":["Read the `errors` array from the exception message to see the exact server complaints.","Update the query to match the current server schema (use introspection or GraphiQL).","Check that the authenticated user/token has permission for the requested resources.","Validate query arguments (ids, filters) actually exist and are correctly formatted.","Upgrade the client/API types alongside the server to avoid schema drift."],"exampleFix":"// before\nquery { projects { data { id name removedField } } }\n// after\nquery { projects { data { id name } } } // removedField no longer in schema","handlingStrategy":"try-catch","validationCode":"// Validate the query against the current schema before shipping:\n// run it in GraphiQL / introspection diff in CI","typeGuard":null,"tryCatchPattern":"try {\n  return graphQlClient.fetchValidDataOrThrow(type, request);\n} catch (IllegalStateException e) {\n  if (e.getMessage().startsWith(\"The GraphQl answer contains errors\")) {\n    log.warn(\"GraphQL query rejected: {}\", e.getMessage());\n    throw new QueryValidationException(e);\n  }\n  throw e;\n}","preventionTips":["Diff client queries against the server schema on every upgrade.","Grant the token's user access to the queried resources.","Validate arguments (ids, filters) before sending.","Keep query definitions in versioned, tested files."],"tags":["graphql","query-errors","api"],"backgroundTag":"api-error-response","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}