Giter VIP home page Giter VIP logo

Comments (4)

benjaminjkraft avatar benjaminjkraft commented on August 23, 2024 1

It looks like the way Shopify does it is a bit different. The Apollo way is a bit simpler for the application code, it's basically just you stick a bunch of queries in a single HTTP request. You can read more in their documentation here: https://www.apollographql.com/docs/react/api/link/apollo-link-batch-http/. That's what I would plan to follow, although I think they do auto-batching and, at least to start, I would probably make it explicit.

from genqlient.

benjaminjkraft avatar benjaminjkraft commented on August 23, 2024

Interesting idea. This is definitely something we've run into as well. We usually solve it by making a field something like productsByHandles(handles: [String!]!): [Product], but of course that requires server changes.

There are actually two ways to do this in GraphQL: one is what you describe, but you can also do "batch queries" which just send all the queries separately in the same HTTP request. I think the way Apollo does it is the de-facto spec, which some other libraries support (I'm not sure about specific ones). genqlient doesn't support this yet, but perhaps it could (added #46).

Then the question is whether genqlient can do something for you here. One thing you can do without special support (once we support fragments at all, see #8) is

fragment productFields on Product {
  variants(...) { ... }
}

query {
  product1: productByHandle(handle:"ABC") { ...productFields }
  product2: productByHandle(handle:"DEF") { ...productFields }
  product3: productByHandle(handle:"GHI") { ...productFields }
  ...
}

So then the question is: should genqlient be able to generate that for you? (Another way you could do it today is to autogenerate that file -- you could just write a little shell script or something.) In general I'm pretty hesitant to have genqlient manipulate the given query, or add special query syntax, if we can avoid it (see also #27), but this may point to the need for an extension API where one of the things it could do is to modify the queries before generating code.

In any case I'll leave this open to think about as we go -- thanks for the suggestion!

from genqlient.

benjaminjkraft avatar benjaminjkraft commented on August 23, 2024

I was thinking about this a bit more, because it is a problem we have as well at Khan. We have mostly solved it by the productsByHandles approach, but I've never felt it's ideal.

On the one hand, I think batch queries are philosophically a much better solution; and I think that homogeneous batch queries should be easy for geqnlient to support (that's not a promise, just a guess; see #46).

On the other hand, I think the way Apollo did them is inefficient in several ways: first of all you have to pass the operation-document several times (so the backend has to parse it several times, etc.); and then additionally the backend's resolvers are called several times instead of a get-many, which is especially bad if you use federation -- I believe the gateway will in that case not batch the requests to the backends. So it's fine for where you want to avoid several HTTP roundtrips, but doesn't really help if you want to make your backends more efficient.

That said, I think probably the right solution to this is to fix the problems of batching on the Apollo side, then use that. I'll start a discussion somewhere when I get a chance to write up the context.

from genqlient.

adrianocr avatar adrianocr commented on August 23, 2024

On the topic of batch queries I think they could potentially be a solution but I think they present an extra bit of learning curve (if that even makes sense grammatically) and adds complexity. I'm not an expert on the subject so I'm not sure if there are other ways of doing batch queries but here's how Shopify does it: https://shopify.dev/tutorials/perform-bulk-operations-with-admin-api.

The query isn't even a query, it's a mutation. But on top of that you also need to then write code into your handlers to keep polling a specific address to check on the status of the operation. Only when the endpoint gives you a completed status can you actually access the data you were interested in to begin with.

So when you say genqlient may be able to add batch query functionality do you mean a similar approach to Shopify's and would genqlient also do the heavy lifting of polling the required endpoint for a completed status?

from genqlient.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.