{"record":{"id":"c57b9f80128c4124","repo":"gatsbyjs/gatsby","slug":"gatsbysortandaggrcodemod-graphql-syntax-error-in-c57b9f","errorCode":null,"errorMessage":"GatsbySortAndAggrCodemod: GraphQL syntax error in query:\n\n${query}\n\nmessage:\n\n${err}","messagePattern":"GatsbySortAndAggrCodemod: GraphQL syntax error in query:\n\n(.+?)\n\nmessage:\n\n(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/gatsby/src/query/transform-document.ts","lineNumber":135,"sourceCode":"                  kind: graphql.Kind.LIST,\n                  values: newObjects,\n                }\n              : newObjects[0]\n          hasChanged = true\n        } else if (node.name.value === `field`) {\n          if (node.value.kind !== graphql.Kind.ENUM) {\n            return\n          }\n\n          // @ts-ignore read-only ...\n          node.value = pathSegmentsToAst(node.value.value, `SELECT`)\n          hasChanged = true\n        }\n      },\n    })\n    return { ast, hasChanged }\n  } catch (err) {\n    throw new Error(\n      `GatsbySortAndAggrCodemod: GraphQL syntax error in query:\\n\\n${query}\\n\\nmessage:\\n\\n${err}`\n    )\n  }\n}\n\nexport function tranformDocument(ast: graphql.DocumentNode): {\n  ast: graphql.DocumentNode\n  hasChanged: boolean\n  error?: Error\n} {\n  if (_CFLAGS_.GATSBY_MAJOR === `5`) {\n    try {\n      return processGraphQLQuery(ast)\n    } catch (error) {\n      return { ast, hasChanged: false, error }\n    }\n  }\n  return { ast, hasChanged: false }","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/gatsbyjs/gatsby/blob/8b06340921ffdf23125a365b9c9923690cb62ce6/packages/gatsby/src/query/transform-document.ts#L117-L153","documentation":"Gatsby v5 runs the `GatsbySortAndAggrCodemod` over every query to rewrite legacy `sort: { fields, order }` and aggregation `field:` syntax into the new path-object form. It wraps `graphql.visit` in try/catch; any parse/visit failure is rethrown with the offending query and the underlying error message.","triggerScenarios":"A page/static query containing malformed GraphQL, or valid-but-unsupported legacy syntax the codemod's visitor cannot transform, thrown during `visit`.","commonSituations":"Upgrading to Gatsby v5 with queries that were hand-written or generated by an older plugin; fragments concatenated incorrectly producing invalid AST; queries produced by source plugins that emit deprecated aggregation syntax.","solutions":["Locate the offending query (the message includes it) and fix the syntax error.","Manually migrate legacy `sort`/aggregation syntax to the v5 form.","Update the source plugin that generated the query if it is not v5-compatible.","Re-run `gatsby build`/`develop` to confirm the codemod succeeds."],"exampleFix":"// before (legacy sort)\nallX(sort: { fields: [title], order: [DESC] })\n// after (v5)\nallX(sort: { title: DESC })","handlingStrategy":"try-catch","validationCode":"// Pre-validate page queries parse with graphql  before build.\nconst { parse, visit } = require(\"graphql\")\nfunction validateQuery(query) {\n  try { visit(parse(query), {}) } catch (e) {\n  throw new Error(`Invalid GraphQL query: ${e.message}\\n${query}`)\n  }\n}\n","typeGuard":null,"tryCatchPattern":"// Wrap the codemod call so one bad query is reported, not fatal.\ntry {\n  transform(document)\n} catch (e) {\n  if (/GatsbySortAndAggrCodemod/.test(String(e))) {\n  reportQueryError(e)\n  }\n  throw e\n}\n","preventionTips":["Run `gatsby build` locally after upgrading to v5 to surface codemod failures early.","Migrate legacy `sort`/aggregation syntax ahead of the upgrade.","Keep source plugins current with the Gatsby major version."],"tags":["graphql","query","migration","codemod","gatsby-v5"],"backgroundTag":null,"analyzedSha":"8b06340921ffdf23125a365b9c9923690cb62ce6","analyzedAt":"2026-08-13T02:36:21.405Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}