Why the error `Failed to execute ‘text’ on ‘Response’: body stream already read` if I’m re-assigning res.json?

Why the error `Failed to execute ‘text’ on ‘Response’: body stream already read` if I’m re-assigning res.json?


1

I’m trying to use the code I found here for APQ (automatic persisted queries) and graphql-request.

But if I use the code like described:

export const graphQLClient = new GraphQLClient("https://localhost:3000", {
    fetch: createPersistedQueryFetch(fetch),
});

I’m getting this error in browser’s console:

TypeError: Failed to execute 'text' on 'Response': body stream already read
    at getResult (index.ts:639:48)
    at makeRequest (index.ts:394:24)
    at async Object.queryFn (utils.ts:84:25)

I think this is the issue:

export const createPersistedQueryFetch =
  (fetchImpl: Fetch): Fetch =>
  async (info, init) => {
    // ...

    // send a request without the query
    const res = await fetchImpl(
      requestWithoutQuery.info,
      requestWithoutQuery.init
    );
    const body = await res.json();

    // if the query was not found in the server, send another request with the query
    if (isPersistedQueryNotFoundError(body)) {
      return fetchImpl(requestWithQueryHash.info, requestWithQueryHash.init);
    } else {
      res.json = () => Promise.resolve(body);
      return res;
    }
  };

graphql-request is trying to re-read the body already read in createPersistedQueryFetch function.

Why is the re-assignment to res.json (with res.json = () => Promise.resolve(body)) not working?


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *