Skip to content

Commit

Permalink
feat(named-operations-object): add option to throw on duplicates (#775)
Browse files Browse the repository at this point in the history
* feat(named-operations-object): add option to throw on duplicate operation name

* add changeset
  • Loading branch information
JonathanWbn committed Jul 11, 2024
1 parent 90f3c00 commit c33e9ac
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-roses-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/named-operations-object': minor
---

add option to throw on duplicate operation names
15 changes: 15 additions & 0 deletions packages/plugins/typescript/named-operations-object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export interface NamedOperationsObjectPluginConfig {
* @default false
*/
useConsts?: boolean;
/**
* @description Throws an error if a duplicate operation name is found.
* @default false
*/
throwOnDuplicate?: boolean;
}

export const plugin: PluginFunction<NamedOperationsObjectPluginConfig, string> = (
Expand All @@ -43,10 +48,16 @@ export const plugin: PluginFunction<NamedOperationsObjectPluginConfig, string> =
fragment: new Set(),
};

const duplicateOperationNames = [];

oldVisit(allAst, {
enter: {
OperationDefinition: node => {
if (node.name?.value) {
if (allOperationsNames[node.operation].has(node.name.value)) {
duplicateOperationNames.push(node.name.value);
return;
}
allOperationsNames[node.operation].add(node.name.value);
}
},
Expand All @@ -56,6 +67,10 @@ export const plugin: PluginFunction<NamedOperationsObjectPluginConfig, string> =
},
});

if (config.throwOnDuplicate && duplicateOperationNames.length > 0) {
throw new Error(`Duplicated operation name(s): ${duplicateOperationNames.join(', ')}`);
}

const objectItems = Object.keys(allOperationsNames)
.map(operationType => {
const relevantOperations: Set<string> = allOperationsNames[operationType];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,60 @@ describe('named-operations-object', () => {
}
}`);
});

it('Should not throw on duplicate operations', async () => {
const result = await plugin(
null,
[
{
document: parse(/* GraphQL */ `
query myQuery {
id
}
`),
},
{
document: parse(/* GraphQL */ `
query myQuery {
id
}
`),
},
],
{},
);

expect(result).toBeSimilarStringTo(`export const namedOperations = {
Query: {
myQuery: 'myQuery'
}
}`);
});

it('Should throw on duplicate operations when config.throwOnDuplicate is set', async () => {
expect(() =>
plugin(
null,
[
{
document: parse(/* GraphQL */ `
query myQuery {
id
}
`),
},
{
document: parse(/* GraphQL */ `
query myQuery {
id
}
`),
},
],
{
throwOnDuplicate: true,
},
),
).toThrowErrorMatchingInlineSnapshot(`"Duplicated operation name(s): myQuery"`);
});
});

0 comments on commit c33e9ac

Please sign in to comment.