Comments (4)
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.
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.
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.
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)
- Errors with Strawberry Generated Schema including `JSON` Scalar HOT 1
- Tests failing on Go 1.22+
- Lint failing on 1.21+
- Using `# @genqlient(typename: "Webhook")` adds `json:"-"` to generated struct (?) HOT 7
- Support `optional: generic` inline
- Support, new release HOT 1
- How can I get response header HOT 1
- Allow globs for schema/queries to be optional, if at least one file is found HOT 1
- Inconsistent JSON Tag Generation for Required Boolean Fields in Structs HOT 3
- Optional input with required output HOT 1
- Support for @authenticated directive HOT 2
- Standardise the error return from http request HOT 3
- Add option to not generate the client, only the types HOT 2
- Option to generate multiple files HOT 2
- Using an Interface inline fragment inside of a Union is awkward to unpack HOT 3
- Optional parameter made required HOT 2
- schema.md has some formatting issues
- Running genqlient on large schemas fails HOT 4
- use_struct_references not compatible with new omitempty validation logic HOT 9
- The Generated GraphQl query with mutation throws error for LINEAR GraphQL Query endpoint HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from genqlient.