dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.88k stars 1.34k forks source link

Smaller graphql client bundles without babel/swc plugins #9780

Open glenjamin opened 12 months ago

glenjamin commented 12 months ago

Is your feature request related to a problem? Please describe.

Currently the client-preset docs' reducing bundle size advice offers three approaches:

I'm currently looking at adopting Rspack for our builds, and we use @apollo/client for queries, so none of these options are viable.

Describe the solution you'd like

I think an alternative option would be to:

Currently the latter is not configurable, and the graphql function always expects to be able to look up queries in a map.

export function graphql(source: string) {
  return (documents as any)[source] ?? {};
}

However, an alternative implementation which would still work and produce much smaller bundles could be

import { gql } from "@apollo/client";
export function graphql(source: string) {
  return gql(source);
}

This does move the parsing work to the client, but at least parsing is done on-demand per-query rather than doing them all up-front.

Have I overlooked something which would mean this is a bad idea? I'd be happy to contribute an implementation if it's something you'd be open to.

Describe alternatives you've considered

No response

Is your feature request related to a problem? Please describe.

No response

glenjamin commented 12 months ago

After writing this I've realised it won't work as proposed if fragment masking is enabled.

Although I think fragment masking could be handled with a runtime helper that only needs a map of fragments, not the whole queries