【问题标题】:Passing arguments to graphQL query将参数传递给 graphQL 查询
【发布时间】:2017-09-27 04:10:48
【问题描述】:

我正在尝试在 node.js 中设置 graphQL 模式。 /graphql 端点通常会被传递的参数命中。 例如,一个 API 调用可能正在寻找“检查”帐户:

query {
  accounts(type: "checking") {
    id
    type
    country
    currency
  }
}

另一个可能正在寻找“美国”的所有帐户:

query {
  accounts(country: "US") {
    id
    type
    country
    currency
  }
}

...还有一个可能正在“英国”寻找以“英镑”计价的“储蓄”账户:

query {
  accounts(type: "savings", country: "UK", currency: "GBP") {
    id
    type
    country
    currency
  }
}

将此查询定义为采用type、country 和currency 的可选参数的正确方法吗?

【问题讨论】:

    标签: graphql graphql-js


    【解决方案1】:

    这实际上取决于您希望如何设计您的 API,该答案没有对错之分,我不确定通常是否存在 正确 方法。

    但是,如果您确定所描述的场景,那么是的,type、country 和 currency 都应该是可选的,因为您在某些查询中将它们排除在外。如果您没有将这些设置为可选,那么您的 GraphQL 服务器会在未传入时抛出错误。

    另一种选择是将所有这些参数包装在带有可选字段的filter 对象中。此外,type、currency 和 country 可能更好地表示为枚举而不是字符串:)

    【讨论】:

      猜你喜欢
      • 2023-01-26
      • 2018-03-08
      • 2017-05-28
      • 2020-12-31
      • 2018-02-16
      • 1970-01-01
      • 2019-04-02
      • 2017-01-02
      • 1970-01-01
      相关资源
      最近更新 更多