【问题标题】:Get the image formats in a Strapi GraphQL query获取 Strapi GraphQL 查询中的图像格式
【发布时间】:2020-10-21 01:57:48
【问题描述】:

我有一个 Post 内容类型,它有一个 FeaturedImage 字段。由于该字段是图片上传,Strapi 会生成方便的附加格式:thumbnailsmallmediumlarge

当我以 JSON 格式查询所有帖子时,我会在响应中得到以下格式:

{
  "id": 1,
  "Title": "Hello World",
  "Content": "Nullam id dolor id nibh ultricies vehicula ut id elit..",
  "FeaturedImage": {
    "id": 1,
    "name": "angel-jimenez-168185",
    "alternativeText": "",
    "caption": "",
    "width": 2768,
    "height": 1560,
    "formats": {
      "thumbnail": {
        "hash": "thumbnail_angel_jimenez_168185_aaceac7acf",
        "ext": ".jpeg",
        "mime": "image/jpeg",
        "width": 245,
        "height": 138,
        "size": 6.22,
        "path": null,
        "url": "/uploads/thumbnail_angel_jimenez_168185_aaceac7acf.jpeg"
      },
      "large": {
        "hash": "large_angel_jimenez_168185_aaceac7acf",
        "ext": ".jpeg",
        "mime": "image/jpeg",
        "width": 1000,
        "height": 564,
        "size": 75.71,
        "path": null,
        "url": "/uploads/large_angel_jimenez_168185_aaceac7acf.jpeg"
      },
      "medium": {
        "hash": "medium_angel_jimenez_168185_aaceac7acf",
        "ext": ".jpeg",
        "mime": "image/jpeg",
        "width": 750,
        "height": 423,
        "size": 41.8,
        "path": null,
        "url": "/uploads/medium_angel_jimenez_168185_aaceac7acf.jpeg"
      },
      "small": {
        "hash": "small_angel_jimenez_168185_aaceac7acf",
        "ext": ".jpeg",
        "mime": "image/jpeg",
        "width": 500,
        "height": 282,
        "size": 19.69,
        "path": null,
        "url": "/uploads/small_angel_jimenez_168185_aaceac7acf.jpeg"
      }
    }
  }
}

但是,如果我在 GraphQL 中查询,则只有原始(全尺寸)图像的 publicURL 可用:

如何使用 GraphQL 访问这些图像格式?

【问题讨论】:

    标签: graphql gatsby strapi


    【解决方案1】:

    这个查询向我展示了所有预期的格式对象:

    query MyQuery {
      strapi {
        home {
          content {
            image {
              formats
            }
          }
        }
      }
    }
    

    我确实在strapi上明确安装了graphql插件 https://strapi.io/documentation/3.0.0-beta.x/plugins/graphql.html#usage

    我在我的 api url 中使用 /graphql(例如 http://localhost:1337/graphql)。

    希望对你有帮助,@jgthms

    仅供参考 斯特拉皮 v.3.1.4 Strapi-plugin-graphql v.3.1.4

    【讨论】:

    • 有没有办法获取特定的图片格式?
    【解决方案2】:

    我今天遇到了同样的问题,并且尝试解决这个问题的时间确实太久了。似乎问题在于,当查询“格式”是 JSON 类型时,它不会让它被查询。我不确定为什么会发生这种情况或如何解决它。任何建议都会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-29
      • 2020-10-25
      • 2021-04-13
      • 2020-09-21
      • 2020-06-07
      • 2022-01-16
      • 1970-01-01
      • 2021-06-20
      相关资源
      最近更新 更多