如何限制我从 graphql 返回的图像数量?

How to limit the number of images I get back from graphql?

我们正在使用 graphql 和中继,并进行重组。

我正在从 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
      }
  `),
);

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

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

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