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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With