简体   繁体   中英

Passing arguments to graphQL query

I am working on setting up a graphQL schema in node. The /graphql endpoint will typically be hit with argument(s) passed. For example, one API call may be looking for "checking" accounts:

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":

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 ?

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.

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 . If you didn't make these optional then your GraphQL server would throw errors if they're not passed in.

Another option could be that you wrap all of these arguments in a filter object with optional fields. Also, type , currency and country are probably better represented as enums than as strings :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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