Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Memoizing fetch error: Body is unusable #801

Closed
MangoMarcus opened this issue Apr 25, 2024 · 0 comments
Closed

Memoizing fetch error: Body is unusable #801

MangoMarcus opened this issue Apr 25, 2024 · 0 comments
Labels

Comments

@MangoMarcus
Copy link

Description

I've tried to memoize the fetch function for graphql-request using lodash.memoize, but it gives "Error: Body is unusable". Is this a bug or am I missing something?

Thanks

Reproduction Steps/Repo Link

import { GraphQLClient } from 'graphql-request';
import memoize from 'lodash/memoize';

export const gqlClient = new GraphQLClient(GRAPHQL_URL, {
  fetch: memoize(
    fetch,
    (...args) => JSON.stringify(args)
  ),
});

Screenshot

Screenshot 2024-04-25 at 13 02 03

For reference the gqlSdk in the screenshot is generated by graphql-codegen using import { getSdk, type SdkFunctionWrapper } from '@/generated/gql/sdk'; export const gqlSdk = getSdk(gqlClient);, it's just a simple wrapper around gqlClient.request though so the effect should be the same:

Screenshot 2024-04-25 at 13 08 28

Workaround

I've worked around the issue for now by memoizing the individual methods in that sdk instead of the underlying fetch

const unmemoizedGqlSdk = getSdk(gqlClient);
export const gqlSdk = Object.fromEntries(
  Object.entries(unmemoizedGqlSdk).map(([key, value]) => [
    key,
    memoize(value, (...args) => JSON.stringify(args)),
  ]),
);
Repository owner locked and limited conversation to collaborators Apr 25, 2024
@jasonkuhrt jasonkuhrt converted this issue into discussion #802 Apr 25, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

1 participant