Skip to content

Commit

Permalink
Remove default typename in Rollup GraphQL plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonmade committed Aug 5, 2024
1 parent d91c56d commit c473811
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-lies-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@quilted/rollup': patch
---

Remove default typename in Rollup GraphQL plugin
4 changes: 3 additions & 1 deletion packages/rollup/source/features/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import {

export interface GraphQLOptions {
manifest?: string | boolean;
addTypename?: boolean;
}

export function graphql({manifest}: GraphQLOptions = {}): Plugin {
export function graphql({manifest, addTypename}: GraphQLOptions = {}): Plugin {
const shouldWriteManifest = Boolean(manifest);
const manifestPath =
typeof manifest === 'string' ? manifest : `manifests/graphql.json`;
Expand Down Expand Up @@ -45,6 +46,7 @@ export function graphql({manifest}: GraphQLOptions = {}): Plugin {
cleanGraphQLDocument(loadedDocument, {
removeUnused: {exclude: topLevelDefinitions},
}),
{addTypename},
);

return {
Expand Down
17 changes: 13 additions & 4 deletions packages/rollup/source/features/graphql/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@ export function cleanGraphQLDocument<
Variables = Record<string, any>,
>(
document: DocumentNode | TypedQueryDocumentNode<Data, Variables>,
{removeUnused = true}: {removeUnused?: boolean | {exclude: Set<string>}} = {},
{
removeUnused = true,
addTypename: shouldAddTypename = false,
}: {
removeUnused?: boolean | {exclude: Set<string>};
addTypename?: boolean;
} = {},
): EnhancedDocumentNode<Data, Variables> {
if (removeUnused) {
removeUnusedDefinitions(document, {
exclude: removeUnused === true ? new Set() : removeUnused.exclude,
});
}

for (const definition of document.definitions) {
addTypename(definition);
if (shouldAddTypename) {
for (const definition of document.definitions) {
addTypename(definition);
}
}

const normalizedSource = minifyGraphQLSource(print(document));
Expand Down Expand Up @@ -82,10 +90,11 @@ export function extractGraphQLImports(rawSource: string) {

export function toGraphQLOperation<Data = unknown, Variables = unknown>(
documentOrSource: EnhancedDocumentNode<Data, Variables> | string,
options?: Parameters<typeof cleanGraphQLDocument>[1],
): GraphQLOperation<Data, Variables> {
const document =
typeof documentOrSource === 'string'
? cleanGraphQLDocument(parse(documentOrSource))
? cleanGraphQLDocument(parse(documentOrSource), options)
: documentOrSource;

return {
Expand Down

0 comments on commit c473811

Please sign in to comment.