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

Have gql-tag-operations generate the type for document registry #10192

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-geckos-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-codegen/gql-tag-operations": patch
---

Have gql-tag-operations generate the type for document registry
39 changes: 27 additions & 12 deletions packages/plugins/typescript/gql-tag-operations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,42 @@ export const plugin: PluginFunction<{
};

function getDocumentRegistryChunk(sourcesWithOperations: Array<SourceWithOperations> = []) {
const lines = new Set<string>();
lines.add(
const lines = new Array<string>();
// It's possible for there to be duplicate sourceOperations, this set will ensure we have unique records for our document registry
const linesDupCheck = new Set<string>();
lines.push(
`/**\n * Map of all GraphQL operations in the project.\n *\n * This map has several performance disadvantages:\n`
);
lines.add(` * 1. It is not tree-shakeable, so it will include all operations in the project.\n`);
lines.add(` * 2. It is not minifiable, so the string of a GraphQL query will be multiple times inside the bundle.\n`);
lines.add(` * 3. It does not support dead code elimination, so it will add unused operations.\n *\n`);
lines.add(` * Therefore it is highly recommended to use the babel or swc plugin for production.\n`);
lines.add(
lines.push(` * 1. It is not tree-shakeable, so it will include all operations in the project.\n`);
lines.push(` * 2. It is not minifiable, so the string of a GraphQL query will be multiple times inside the bundle.\n`);
lines.push(` * 3. It does not support dead code elimination, so it will add unused operations.\n *\n`);
lines.push(` * Therefore it is highly recommended to use the babel or swc plugin for production.\n`);
lines.push(
` * Learn more about it here: https://the-guild.dev/graphql/codegen/plugins/presets/preset-client#reducing-bundle-size\n */\n`
);
lines.add(`const documents = {\n`);

lines.push(`type Documents = {\n`);
for (const { operations, ...rest } of sourcesWithOperations) {
const originalString = rest.source.rawSDL;
const operation = operations[0];
const aboutToPushLine = ` ${JSON.stringify(originalString)}: typeof types.${operation.initialName},\n`;
if (!linesDupCheck.has(aboutToPushLine)) {
lines.push(aboutToPushLine);
linesDupCheck.add(aboutToPushLine);
}
}
lines.push(`};\n`);
lines.push(`const documents: Documents = {\n`);
for (const { operations, ...rest } of sourcesWithOperations) {
const originalString = rest.source.rawSDL!;
const operation = operations[0];

lines.add(` ${JSON.stringify(originalString)}: types.${operation.initialName},\n`);
const aboutToPushLine = ` ${JSON.stringify(originalString)}: types.${operation.initialName},\n`;
if (!linesDupCheck.has(aboutToPushLine)) {
lines.push(aboutToPushLine);
linesDupCheck.add(aboutToPushLine);
}
}

lines.add(`};\n`);
lines.push(`};\n`);

return lines;
}
Expand Down