简体   繁体   English

将参数传递给graphQL查询

[英]Passing arguments to graphQL query

I am working on setting up a graphQL schema in node. 我正在努力在节点中设置graphQL架构。 The /graphql endpoint will typically be hit with argument(s) passed. /graphql端点通常会被传递的参数命中。 For example, one API call may be looking for "checking" accounts: 例如,一个API调用可能正在寻找“检查”帐户:

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

Another may be looking for all accounts in the "US": 另一个可能正在寻找“美国”中的所有帐户:

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

...and yet another may be looking for "savings" accounts in the "UK" denominated in "GBP": ......而另一个人可能正在寻找以“GBP”计价的“英国”中的“储蓄”账户:

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

Is the proper approach defining this query as taking optional parameters for type , country , and currency ? 定义此查询的正确方法是为typecountrycurrency可选参数吗?

This really depends on how you want to design your API, there is no right or wrong to that answer and I'm not sure if there generally is a proper approach. 这实际上取决于你想要如何设计你的API,这个答案没有对错,我不确定是否通常有一个正确的方法。

However, if you are certain about the scenario you're describing, then yes, type , country and currency should all be optional since you're leaving them out in some queries . 但是,如果您确定要描述的场景,那么是的, typecountrycurrency都应该是可选的,因为您在某些查询中将它们排除在外。 If you didn't make these optional then your GraphQL server would throw errors if they're not passed in. 如果你没有使这些可选,那么你的GraphQL服务器如果没有传入则会抛出错误。

Another option could be that you wrap all of these arguments in a filter object with optional fields. 另一种选择可能是将所有这些参数包装在带有可选字段的filter对象中。 Also, type , currency and country are probably better represented as enums than as strings :) 此外, typecurrencycountry可能更好地表示为枚举而不是字符串:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM