【问题标题】:How to limit the number of images I get back from graphql?如何限制我从 graphql 返回的图像数量?
【发布时间】:2019-08-13 07:34:43
【问题描述】:

我们正在使用graphql 和relay,并重新组合。

我正在从Unit 获取一些数据,但我还需要从它的Project 获取一些数据。(一个单元属于一个项目)

我已经想出了如何从项目中获取我需要的一些图像,如下所示:

project {
  images {
    fileKey
    aspectRatio
  }

但我得到了很多图像.. 像 20.. 我只需要第一个.. 我如何告诉 relay 或 graphQl 只给我一个?

谢谢,这是我的完整片段:

const enhance = compose(
  fragment(graphql`
      fragment DetailsSectionContainer_unit on Unit {
        tourIdArchilogic
        tourIdMatterport
        images {
          fileKey
          aspectRatio
        }
        ...TitleRowContainer_unit
        ...UnitSummaryContainer_unit
        ...ContactFormColumnContainer_unit
        project {
          images {
            fileKey
            aspectRatio
          }
          ...ContactFormColumnContainer_project
        }
        company {
          ...ContactFormColumnContainer_company
        }
        ...TourContainer_unit
      }
      fragment DetailsSectionContainer_rentedUnit on RentedUnit {
        ...UnitSummaryContainer_rentedUnit
      }
      fragment DetailsSectionContainer_vacantUnit on VacantUnit {
        ...UnitSummaryContainer_vacantUnit
      }
  `),
);

【问题讨论】:

    标签: graphql relay


    【解决方案1】:

    您应该将图像的返回类型更改为连接,然后告诉服务器您要获取多少图像,如下所示:

    project {
       images(first: 1) {
            edges {
              node {
                  fileKey
                  aspectRatio
              }
           }
       }
    }
    

    更多关于连接的信息 -> Grahpql connections

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 1970-01-01
      • 2020-01-18
      • 2010-12-07
      • 2011-06-28
      • 2018-06-06
      • 2022-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多