Home > front end >  GraphQL best practice when processing queries with multiple data
GraphQL best practice when processing queries with multiple data

Time:02-03

I'm new to GraphQL, and I'm confused about what the best practice is when a client needs to repeat a query for multiple inputs. For example, let's say I have an API that can tell me which city is nearest a GPS coordinate. (Apologies if this has errors, but you get the idea)

type Query {
  locate(gps: GPS!): City!
}

Now let's say my client has a couple hundred GPS coordinates it wants to locate. It's obviously grossly inefficient to make hundreds of requests. Should I make a query that can locate a bunch of coordinates? Or batch them in some other way?

type Query {
  locate(gps: GPS!): City!
  locateMultiple(gpss: [GPS]!): [City]!
}

This seems antithetical to the idea of GraphQL, but I'm still new here. Also, for context, I'm not planning on using Apollo (Haskell backend with iOS frontend, I'm looking at SwiftGraphQL), but I could probably create a simple batching system (an array of requests to an array of responses) if needed.

What would you do?

CodePudding user response:

This is not an anti-pattern to receive or send a list of inputs to/from a GraphQL API, i wonder what could be antithetical in this. I think you have a misconception here.

When a client and a web service are sharing informations this is super-common, i dont see any reason to have such a huge constraint.

This is by for more efficient that sending a request by object, and by far more simple that coding a batching system.

  •  Tags:  
  • Related