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("http://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?

Fred Hors
  • 3,258
  • 3
  • 25
  • 71

0 Answers0