gatsbyjs/gatsby · error

Error executing the GraphQL query inside gatsby-plugin-sitem

Error message

Error executing the GraphQL query inside gatsby-plugin-sitemap:\n

What it means

In gatsby-plugin-sitemap's onPostBuild (gatsby-node.js:29-34), after attempting to resolve the site URL, the plugin checks if the graphql(query) call returned errors. If the GraphQL query has syntax errors, schema errors, or returns partial data with errors, reporter.panic fires with the errors array. The default query selects site URL and all page paths; a custom query option could also introduce errors.

Source

Thrown at packages/gatsby-plugin-sitemap/src/gatsby-node.js:30

    entryLimit,
    query,
    excludes,
    resolveSiteUrl,
    resolvePagePath,
    resolvePages,
    filterPages,
    serialize,
  }
) => {
  const { data: queryRecords, errors } = await graphql(query)

  // resolvePages and resolveSiteUrl are allowed to be sync or async. The Promise.resolve handles each possibility
  const siteUrl = await Promise.resolve(resolveSiteUrl(queryRecords)).catch(
    err => reporter.panic(`${REPORTER_PREFIX} Error resolving Site URL`, err)
  )

  if (errors) {
    reporter.panic(
      `Error executing the GraphQL query inside gatsby-plugin-sitemap:\n`,
      errors
    )
  }

  const allPages = await Promise.resolve(resolvePages(queryRecords)).catch(
    err => reporter.panic(`${REPORTER_PREFIX} Error resolving Pages`, err)
  )

  if (!Array.isArray(allPages)) {
    reporter.panic(
      `${REPORTER_PREFIX} The \`resolvePages\` function did not return an array.`
    )
  }

  reporter.verbose(
    `${REPORTER_PREFIX} Filtering ${allPages.length} pages based on ${excludes.length} excludes`
  )

View on GitHub (pinned to 8b06340921)

Solutions

  1. Read the errors array in the panic output — it contains the specific GraphQL validation/resolution errors
  2. If using a custom query option, test it in the GraphiQL playground at http://localhost:8000/__graphql
  3. Remove the custom query to use the default and see if the error persists
  4. Fix any schema issues reported by other plugins earlier in the build

Example fix

// before: custom query with a non-existent field
{
  resolve: `gatsby-plugin-sitemap`,
  options: {
    query: `
      {
        site {
          siteMetadata {
            siteUrl
            nonExistentField // causes GraphQL error
          }
        }
      }
    `,
  },
},

// after: remove the invalid field or use the default query
{
  resolve: `gatsby-plugin-sitemap`,
  // uses default query automatically
},
Defensive patterns

Strategy: validation

Validate before calling

// Test the sitemap GraphQL query before building
// Run this in the GraphiQL playground or via a script:
async function validateSitemapQuery(graphql) {
  const { errors, data } = await graphql(sitemapQuery)
  if (errors) {
    throw new Error(`Sitemap query failed: ${errors.map(e => e.message).join(', ')}`)
  }
  if (!data) {
    throw new Error('Sitemap query returned no data')
  }
}

Try / catch

// The plugin catches GraphQL errors at the top level.
// As a consumer, validate the query shape beforehand:
const result = await graphql(query)
if (result.errors) {
  console.error('Query errors:', result.errors)
  // fix before building
}

Prevention

When it happens

Trigger: The site GraphQL query fails: schema not built correctly, a custom 'query' option has a syntax error, or a referenced type/field doesn't exist. The Gatsby build has schema issues that cause GraphQL resolution errors.

Common situations: Custom query option referencing fields that don't exist in the schema. Gatsby schema customization conflicts. Plugin ordering issues where schema isn't ready. Broken GraphQL fragments in the custom query.

Related errors


AI-assisted analysis of gatsbyjs/gatsby@8b06340921 (2026-08-13). Data as JSON: /api/errors/1bb6a8bbc7540932. Report an issue: GitHub.