Skip to content
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

Allow HTTP client to expose a ReadableStream #4086

Open
andrewmcgivery opened this issue Dec 3, 2024 · 0 comments
Open

Allow HTTP client to expose a ReadableStream #4086

andrewmcgivery opened this issue Dec 3, 2024 · 0 comments
Assignees

Comments

@andrewmcgivery
Copy link

andrewmcgivery commented Dec 3, 2024

👋 Hello! ApolloGraphQL solutions architect here! :)

Feature Description

It is currently not possible to incrementally stream chunks over the Http client as part of a multipart request.

This prevents using K6 to test APIs which use HTTP Multipart and streaming responses in incremental chunks, such as Apollo Router. Apollo Router uses HTTP Multipart with a --graphql boundary instead of websockets for GraphQL Subscriptions (Example accept header: multipart/mixed; boundary="graphql"; subscriptionSpec=1.0, application/json)

Suggested Solution (optional)

With k6/experimental/streams in experimental, it would be great if it could be used with the HTTP client.

In Node, using Fetch, it looks something like this:

fetch(actualUrl, {
  method: "POST",
  headers: {
    accept: 'multipart/mixed; boundary="graphql"; subscriptionSpec=1.0, application/json',
    "content-type": "application/json",
  },
  body: JSON.stringify(body),
}).then((response) => {
  const reader = response.body.getReader(); // I can get a reader here!

  // Process each chunk as it comes in
  const processChunk = ({ done, value }) => {
    // ... code here ...

    // Read the next chunk
    return reader.read().then(processChunk);
  }

  return reader.read().then(processChunk);
});

As each chunk comes in, it is processed by the reader and then the fetch can be ended directly using an AbortSignal.

With K6, this isn't possible because it awaits the entire response and because it doesn't expose a getReader interface.

I'd love to be able to do something like this: (not married to this exact solution or function names)

let response = http.asyncRequest("POST", BASE_URL, JSON.stringify({ query: query }), {
    headers: {
      accept: 'multipart/mixed; boundary="graphql"; subscriptionSpec=1.0, application/json',
      "content-type": "application/json",
    },
  });

const reader = response.getReader();

This may not be the right solution. Open to whatever would be the "k6-way" to accomplish this as long as the end result is being able to stream a response incrementally from an API. :)

Already existing or connected issues / PRs (optional)

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants