Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 828
MattDionis Avatar asked Oct 17 '22 13:10

MattDionis


1 Answers

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

like image 80
nburk Avatar answered Oct 21 '22 08:10

nburk