-
-
Notifications
You must be signed in to change notification settings - Fork 516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support batched GraphQL queries #510
Comments
the only way I got graphql batch requests to be intercepted, is by using rest with the full request URL, like so:
and then I have to manually analyze the req.body[] and handle/mock the request. |
@antoniosZ Do you have a repro for this? I haven't tried this feature myself, but I imagine it might have to be linked? https://mswjs.io/docs/api/graphql/link#examples |
@msutkowski I tried linking and that didn't help. I'll see if I can quickly create a repro |
@msutkowski Unfortunately, I could not find a public graphql server to use for the repro, that supports query batching. please let me know if you have one available and then I can create the demo project that reproduces it... thank you |
I'm not a graphQL user personally but I can try to take a look at this later and set up a server locally. It's possible that somebody will beat me to this but I'll try to get to it by tomorrow |
Ah, the issue here is that |
@msutkowski this is exactly what is happening. It posts an array, instead of an object... |
@antoniosZ can you test it out against this preview of the build? https://ci.codesandbox.io/status/mswjs/msw/pr/513/builds/83287 - or |
thank you @msutkowski but unfortunately, it does not work. It appears that the problem is here: |
@antoniosZ Sorry about that, there is a little more work to do here for handling the arrays. I'll get it taken care of today :) |
thank you for working on this @msutkowski! Is there a new commit that you would like me to test? |
@msutkowski We're interested in a resolution for this as well in our organisation. Unfortunately we have found errors in TS integration beyond version 0.21.2, so we're fixed to that at the moment. Many thanks! |
Hello, I maintain a GraphQL client for Vue.js and used the following workaround for the batched requests test. As @antoniosZ pointed out, it requires intercepting it as a rest request, but I utilized the handlers you typically define earlier export const handlers = [
// some graphql handlers ...
// add this to the end of GQL handlers
rest.post('https://test.com/graphql', async (req, res) => {
if (!Array.isArray(req.body)) {
throw new Error('Unknown operation');
}
// map the requests to responses using their handlers
const data = await Promise.all(
req.body.map(async op => {
const partReq = { ...req, body: op };
const handler = handlers.find(h => h.test(partReq));
// no handler matched that operation
if (!handler) {
return Promise.reject(new Error(`Cannot handle operation ${op}`));
}
// execute and return the response-like object
return handler.run(partReq);
})
);
return res(res => {
res.headers.set('content-type', 'application/json');
// responses need extraction from the individual requests
// and we stitch them into a single array response
res.body = JSON.stringify(
data.map(d => {
return JSON.parse(d?.response?.body) || {};
})
);
return res;
});
}),
]; I realize this might not cover most cases, but works for me for now. |
@msutkowski did some incredible job on the initial support phase. We've merged a major refactoring to how the request handlers work internally, so that pull request needs to be adjusted. I will try to resolve the conflicts and leave it at the same state for any volunteers to collaborate. Thank you for your interest in this. We'd love to make batched operations support happen. |
I'm experiencing the same issue, any fix coming up ? |
Would also like a fix for this! Spent a good 2h on this before figuring it was a bug! |
bump |
Ok, so its been a year and still no fix? |
Hi everyone! I was wondering if there are any plans to continue the work to support batched queries. Seems like the linked PR is kind of stale now. |
bump |
Hi all. Any progress on this? |
Hey @kettanaito! First of all, thank you for all of the work you've put into this really useful tool! I was wondering if this bug was on your roadmap to resolve, or if it was available for someone to pick up and complete? This thread seems to have stagnated (with a few requests for updates), and #513 seems to have gone stale. If this is something you're still looking for help on, I may be able to dedicate some time to digging in on this -- having this resolved would save me a ton of time in my app! |
@kettanaito any progress on this? |
Sharing my thoughts on the batched GraphQl queries since I had a minute to look into this question. Batched queries are non-standardFirst, I can hardly see support for batched GraphQL queries landing in MSW for the following reason: query batching isn't a part of the GraphQL specification. It's a technique used by GraphQL clients to enhance performance. This means that each GraphQL client is free to implement and interpret batched queries as it wishes. For example, Apollo and // Apollo
[
{ data: { user: value },
{ data: { product: value },
{ data: { user: value }
]
// batch-execute
{
user_0: value,
product_0: value,
user_1: value
} As one would expect from the lack of standard, the clients will dictate all sorts of formats that each client finds the best for them. MSW, however, cannot cater to particular clients, it's against our core philosophy. The library has shipped zero framework/client-specific code for 5 years, and I won't see that changed. In practice, the lack of a standard batched query format means MSW has to guess what GraphQL client you may be using. Inevitably, this will lead to users demanding support for their client of choice and how batched queries are implemented there. This is a slippery slope of failed expectations I'd rather not see us even step a foot at. This issue quickly becomes apparent once you try to apply a batched query resolution to the request/response contract. 1 request handler is responsible for controlling 1 request. Naturally, you want to keep the query/response collocation flat because that's how it is in your actual GraphQL server (batching is an entirely a GraphQL client's feature; the client wraps/unwraps the requests, your server doesn't know anything about those batches). Suddenly, a single request contains multiple GraphQL queries that have to be (1) parsed, (2) matched against the request handlers; (3) resolved. A single GraphQL request handler cannot do it so the next idea is to lift this resolution up to How to handle batched queries then?I suggest you detect and unwrap the batched queries in your network description. I highly suggest you do this on your end because:
I already have both use cases implemented and passing tests in here: #1982. Once this is finalized, I will add this as a recipe to the docs so it's clear how to approach mocking batched GraphQL queries. |
Could |
@kalabro, we can but you have to consider that spec itself is a draft and can change as more and more people contribute and refine it. Batching is also not mentioned in their public draft, and given it's in the The important bit is this: you can achieve support for batched GraphQL queries trivially on your side. You are also in full context and awareness of what you're using so you don't have to guess, unlike MSW. This is a win-win for both sides and this is the direction we are taking on such features. I'm finalizing the tests and the updates to the docs to share more details on how to do that.
|
Mock batched GraphQL Queries |
Released: v2.1.3 🎉This has been released in v2.1.3! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
Environment
Request handlers
Actual request
https://www.apollographql.com/docs/link/links/batch-http/
Using Apollo Client to perform a normal batch query
ie.
Current behavior
batch queries are not getting intercepted but if I switch from
BatchHttpLink
to a regularHttpLink
, the interception works as expected.Expected behavior
batch queries get intercepted
The text was updated successfully, but these errors were encountered: