简体   繁体   English

在 Strapi GraphQL 查询中获取图像格式

[英]Get the image formats in a Strapi GraphQL query

I have a Post content type, which has a FeaturedImage field.我有一个Post内容类型,它有一个FeaturedImage字段。 Because this field is an image upload, Strapi generates handy additional formats: thumbnail , small , medium and large .因为这个字段是图片上传,Strapi 会生成方便的附加格式: thumbnailsmallmediumlarge

When I query all posts in JSON, I get these formats in the response:当我查询 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"
      }
    }
  }
}

However, if I query in GraphQL, only the publicURL for the original (full size) image is available:但是,如果我在 GraphQL 中查询,则只有原始(全尺寸)图像的publicURL可用:

GraphQL 图像格式树

How I access these image formats with GraphQL?如何使用 GraphQL 访问这些图像格式?

This query is showing me all of the expected formats objects:此查询向我展示了所有预期的格式对象:

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

I did explicitly install the graphql plugin on strapi https://strapi.io/documentation/3.0.0-beta.x/plugins/graphql.html#usage我确实在strapi https://strapi.io/documentation/3.0.0-beta.x/plugins/graphql.html#usage上明确安装了 graphql 插件

and I am using /graphql in my api url (eg http://localhost:1337/graphql).我在我的 api url 中使用 /graphql(例如 http://localhost:1337/graphql)。

I hope that helps, @jgthms我希望这会有所帮助,@jgthms

FYI strapi v.3.1.4 strapi-plugin-graphql v.3.1.4仅供参考strapi v.3.1.4strapi-plugin-graphql v.3.1.4

I ran into the same issue today and have tried to solve this for admittedly too long.我今天遇到了同样的问题,并且已经尝试解决这个问题太久了。 It seems that the issue is that when querying is that "formats" is of type JSON and it won't let it be queried.似乎问题在于,当查询“格式”是 JSON 类型时,它不会被查询。 I'm not really sure why this is happening or how to resolve it though.我不确定为什么会发生这种情况或如何解决它。 Any advice would be helpful.任何意见将是有益的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM