gpt4 book ai didi

graphql - 将参数传递给 graphQL 查询

转载 作者:行者123 更新时间:2023-12-02 17:03:46 28 4
gpt4 key购买 nike

我正在努力在节点中设置 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
}
}

将此查询定义为采用类型国家/地区货币可选参数的正确方法是否正确?

最佳答案

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

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

另一个选择可能是将所有这些参数包装在带有可选字段的 filter 对象中。此外,typecurrencycountry 可能比字符串更好地表示为枚举:)

关于graphql - 将参数传递给 graphQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43690400/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com