You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gatsby中正常的Strapi GraphQL查询在NextJS(Apollo)中失效如何解决?

问题根因

你在Gatsby GraphiQL中运行的查询是Gatsby侧适配后的专属GraphQL结构,和Strapi原生的GraphQL接口结构不兼容,这是查询在Strapi Playground和Next.js Apollo中报错的核心原因。

gatsby-source-strapi 插件会将拉取到的Strapi数据统一转换为Gatsby规范的GraphQL格式:比如给集合类型加allStrapiXXX前缀、用edges.node结构包裹数据、过滤语法统一为Gatsby的filter:{字段:{操作符:值}}格式,这些语法在原生Strapi GraphQL中是不识别的。

适配方案

单查询快速适配

你原来的Gatsby查询可直接修改为Strapi原生支持的格式,即可在Strapi Playground、Next.js Apollo中正常运行:

query MyQuery {
  products(filters: {featured: {eq: true}}) {
    data {
      id # 对应Gatsby查询中的strapiId
      attributes {
        name
        variants {
          data {
            attributes {
              price
              qty
              product
            }
          }
        }
      }
    }
  }
}

调整规则对应如下:

  • 集合查询名从allStrapiProduct改为Strapi原生的复数资源名products(具体名称以你Strapi后台定义的内容类型API ID为准)
  • 过滤参数从filter改为Strapi v4+ 要求的filters
  • 层级结构从edges.node改为Strapi标准的data.attributes结构
  • 原Gatsby查询中的strapiId对应Strapi原生返回的data.id字段

如果你使用的是Strapi v3版本,不需要attributes层,字段直接从data下获取即可。

全量查询批量迁移规则

如果要把所有Gatsby查询批量迁移到Next.js项目中,可按以下规则统一转换:

  1. 所有集合查询前缀allStrapiXXX替换为Strapi内容类型对应的复数API ID,单条查询前缀strapiXXX替换为单数API ID
  2. 所有过滤参数的filter字段改为filters
  3. 遍历列表的edges.node路径替换为data.attributes
  4. 所有strapiId字段替换为顶层id字段

内容的提问来源于stack exchange,提问作者ThisIsShayan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 00:57:01