0

我正在尝试使用createPaginationContainer一些变量,这是我的代码示例:

export const GiftRecipientsPaginationContainer = createPaginationContainer(
  GiftRecipients,
  {
    giftRecipientsMe: graphql`
      fragment GiftRecipients_giftRecipientsMe on Me
      @argumentDefinitions(
        filter: {
          type: "GiftRecipientsFilterInput!"
          defaultValue: { metaMaskEthAddress: null, name: null }
        }
        products: { type: "[ProductGiftInput!]!" }
        first: { type: "Int", defaultValue: 6 }
        after: { type: "String", defaultValue: null }
      ) {
        giftRecipientsConnection(
          products: $products
          filter: $filter
          first: $first
          after: $after
        )
          @connection(
            key: "GiftRecipientsMe_giftRecipientsConnection"
            filters: ["filter", "products"]
          ) {
          pageInfo {
            endCursor
            hasNextPage
            hasPreviousPage
            startCursor
          }
          edges {
            node {
              vipXP
            }
          }
        }
      }
    `,
  },
  {
    direction: "forward",
    getConnectionFromProps(props) {
      return props.giftRecipientsMe.giftRecipientsConnection
    },
    getFragmentVariables(prevVars, totalCount) {
      console.log("prevVars", prevVars)
      return {
        ...prevVars,
        count: totalCount,
      }
    },
    getVariables(props, { count, cursor }, fragmentVariables) {
      console.log(props, fragmentVariables)
      return {
        filter: fragmentVariables.filter,
        products: fragmentVariables.products,
        first: fragmentVariables.first,
        after: cursor,
        count,
      }
    },
    query: graphql`
      query GiftRecipientsPaginationQuery(
        $filter: GiftRecipientsFilterInput!
        $products: [ProductGiftInput!]!
        $first: Int!
        $after: String
      ) {
        me {
          ...GiftRecipients_giftRecipientsMe
            @arguments(
              filter: $filter
              products: $products
              first: $first
              after: $after
            )
        }
      }
    `,
  }
)

在父组件中,我像这样传递我的变量:

<GiftRecipients
    metaMaskEthAddress={metaMaskEthAddress}
    giftRecipientsMe={me}
/>

现在,在我重新获取之前,我在 getFragmentVariables 或 getVariables 中的日志都没有显示。如何将变量传递给createPaginationContainer初始请求?

4

1 回答 1

0

嗯,我的错误是它们必须来自父片段并传递到 graphql 查询中

像这样的东西:

const CheckoutPromptQueryRenderer = (
  props: CheckoutPromptQueryRendererProps
) => {
  if (!props.checkoutPrompt) return null

  const { checkoutPrompt, relay, metaMaskEthAddress } = props

  return (
    <QueryRenderer
      environment={relay.environment}
      query={graphql`
        query CheckoutPromptQueryRendererQuery(
          $filter: GiftRecipientsFilterInput!
          $products: [ProductGiftInput!]!
        ) {
          me {
            ...GiftRecipients_giftRecipientsMe
              @arguments(products: $products, filter: $filter)
          }
        }
      `}
      variables={{
        products: checkoutPrompt.productIDs.map(id => ({ productID: id })),
        filter: metaMaskEthAddress
          ? { metaMaskEthAddress: metaMaskEthAddress, name: null }
          : { metaMaskEthAddress: null, name: null },
      }}
      render={(
        response: QueryRendererReadyState<CheckoutPromptQueryRendererQueryResponse>
      ) => {
        if (response.error) {
          console.log(response.error)
          throw "Error querying checkout prompt"
        }
        if (!response.props) return null

        return <CheckoutPrompt {...response.props} />
      }}
    />
  )
}

于 2020-12-26T13:05:12.503 回答