【问题标题】:When querying for a GraphQL float, what does the second argument represent?查询 GraphQL 浮点数时,第二个参数代表什么?
【发布时间】:2018-11-11 02:27:30
【问题描述】:

我在 GraphQL 中进行以下查询:

{
  metal(silver_bid_usd_toz: 1) {
    silver_bid_usd_toz
  }
}

返回

{
  "data": {
    "metal": {
      "silver_bid_usd_toz": 16.45
    }
  }
}

API 返回的 JSON 对象是平面的:

{
  silver_bid_usd_toz: 123,
  gold_bid_usd_toz: 123,
  copper_bid_usd_toz: 123
}

我不明白我的 graphql 查询中的 int 1 是什么意思 metal(silver_bid_usd_toz: 1)

不管我把它改成什么,它可以是 1 或 355,但它是查询工作所必需的。为什么我不能这样做

{
  metal(silver_bid_usd_toz) {
    silver_bid_usd_toz
  }
}

我的架构如下所示:

 module.exports = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    description: '...',
    fields: () => ({
      metal: {
        type: MetalType,
        args: {
          gold_bid_usd_toz: { type: GraphQLFloat },
          silver_bid_usd_toz: { type: GraphQLFloat }
        },
        resolve: (root, args) => fetch(
          `api_url`
        )
        .then(response => response.json())
      }
    })
  })
});

【问题讨论】:

  • 当您发送没有任何参数的查询时究竟会发生什么?

标签: graphql express-graphql


【解决方案1】:

您将 silver_bid_usd_toz 作为该字段的参数传递,但显然您没有在解析函数中使用它,因此它被忽略了。

这似乎是更改参数值时结果始终相同的原因。

但是当你说它是查询工作所必需的时很奇怪,因为它没有定义为GraphQLNonNull 类型。 根据您传递给我们的 Schema,应该可以在不传递任何参数的情况下查询该字段。

【讨论】:

  • 是的,你是对的。它失败了,因为我将它包裹在括号中,这按预期工作:metal { silver_bid_usd_toz }
  • 哦,有道理。只是看着它,我没有注意到错误。很高兴知道您发现了问题。 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 2010-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
相关资源
最近更新 更多