【问题标题】:How to pass nested variables to the GraphQL query in Apollo?如何将嵌套变量传递给 Apollo 中的 GraphQL 查询?
【发布时间】:2020-11-12 10:35:18
【问题描述】:

尝试将嵌套变量传递给 GraphQL 查询,但我的服务器只获取顶级变量 (shopId),其他所有变量均为空。

我试过了:

#1

const CALCULATE_PACKAGE_PRICE = gql`
  query CalculatePackagePrice(
    $shopId: String!
    $address1: String
    $zip: String
    $city: String
    $countryCode: String
    ) {
    calculatePackagePrice(
      where: {
        shopId: $shopId
        destination: {
          address1: $address1
          zip: $zip
          city: $city
          countryCode: $countryCode
        }
      }
    ) {
      name
      price
      userErrors {
        field
        message
      }
    }
  }
`

const [calculatePackagePrice, { loading, data }] = useLazyQuery(
  CALCULATE_PACKAGE_PRICE,
  {
    variables: {
      shopId: shopId,
      destination: {
        address1: "Example 123",
        zip: "123",
        city: "Test",
        countryCode: "US",
      },
    },
  }
)

还有#2:

export function CALCULATE_PACKAGE_PRICE({ shopId, destination }) {
  return gql`
    query CalculatePackagePrice {
        calculatePackagePrice(
          where: {
            shopId: "${shopId}"
            destination: {
              address1: "${destination.address1}"
              zip: "${destination.zip}
              city: "${destination.city}"
              countryCode: "${destination.countryCode}"
            }
          }
        ) {
          name
          price
          userErrors {
            field
            message
          }
        }
      }
  `
}


const [calculatePackagePrice, { loading, data }] = useLazyQuery(
  CALCULATE_PACKAGE_PRICE({
    shopId: shopId,
    destination: {
      address1: "Example 123",
      zip: "123",
      city: "Test",
      countryCode: "US",
    },
  })
)

当我将变量内容硬编码到查询中时,它工作得很好。我做错了什么?

【问题讨论】:

    标签: graphql apollo react-apollo apollo-client


    【解决方案1】:

    这是来自 graphql 文档的一个有用的 sn-p,

    所有声明的变量必须是标量、枚举或输入对象类型。所以如果你想将一个复杂的对象传递给一个字段,你需要知道服务器上匹配的输入类型。

    您正确地将变量作为字符串传递,但随后尝试(也许成功,但我以前从未见过语法)在 gql 模板字符串中创建对象。相反,为目的地和地点创建一个输入类型。

    input WhereInput {
      shopId: String!
      destination: DestinationInput!
    }
    
    input DestinationInput {
      address1: String!
      zip: String!
      city: String!
      countryCode: String!
    }
    

    然后在客户端更改查询(并更新服务器定义),

    const CALCULATE_PACKAGE_PRICE = gql`
      query CalculatePackagePrice($where: WhereInput!) {
        calculatePackagePrice(where: $where) {
          name
          price
          userErrors {
            field
            message
          }
        }
      }
    `
    

    然后传递变量,如,

    const [calculatePackagePrice, { loading, data }] = useLazyQuery(
      CALCULATE_PACKAGE_PRICE,
      {
        variables: {
          where: {
            shopId,
            destination: {
              address1: "Example 123",
              zip: "123",
              city: "Test",
              countryCode: "US",
            },
          },
        }
      }
    )
    

    【讨论】:

    • 非常感谢。 IDK 我在做什么。像魅力一样工作!
    猜你喜欢
    • 2018-01-27
    • 2019-04-02
    • 2019-01-28
    • 2017-05-15
    • 2022-01-24
    • 2019-01-26
    • 2018-08-16
    • 2020-12-31
    • 1970-01-01
    相关资源
    最近更新 更多