Skip to content

Commit 43c736b

Browse files
authored
enhance(utils/rewireTypes): do not throw duplicate schema type error (#4706)
* enhance(utils/rewireTypes): do not throw duplicate schema type error * Improvements * Go * Ok * Tests * Warning
1 parent 13b9af9 commit 43c736b

File tree

6 files changed

+25
-5
lines changed

6 files changed

+25
-5
lines changed

.changeset/good-crabs-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/utils': minor
3+
---
4+
5+
Do not throw duplicate type error name while rewiring types

.changeset/nice-moose-compete.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/wrap': minor
3+
---
4+
5+
RenameTypes: do not rename type if the new name already exists in the schema

packages/loaders/url/tests/url-loader.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ describe('Schema URL Loader', () => {
584584
})) as ExecutionResult;
585585
expect(result.data).toBeUndefined();
586586
expect(result.errors).toBeDefined();
587-
expect(result.errors?.[0].message).toBe('fetch failed to http://127.0.0.1:9777/graphql');
588-
expect(result.errors?.[0].originalError?.message).toContain('failed');
587+
expect(result.errors?.[0].message).toContain('failed');
588+
expect(result.errors?.[0].message).toContain('http://127.0.0.1:9777/graphql');
589589
});
590590
});

packages/utils/src/heal.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ export function healTypes(
7575
continue;
7676
}
7777

78-
if (actualName in actualNamedTypeMap) {
79-
throw new Error(`Duplicate schema type name ${actualName}`);
78+
if (actualNamedTypeMap[actualName] != null) {
79+
console.warn(`Duplicate schema type name ${actualName} found; keeping the existing one found in the schema`);
80+
continue;
8081
}
8182

8283
actualNamedTypeMap[actualName] = namedType;

packages/utils/src/rewire.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export function rewireTypes(
5454
}
5555

5656
if (newTypeMap[newName] != null) {
57-
throw new Error(`Duplicate schema type name ${newName}`);
57+
console.warn(`Duplicate schema type name ${newName} found; keeping the existing one found in the schema`);
58+
continue;
5859
}
5960

6061
newTypeMap[newName] = namedType;

packages/wrap/src/transforms/RenameTypes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export default class RenameTypes<TContext = Record<string, any>>
4444
originalWrappingSchema: GraphQLSchema,
4545
_subschemaConfig: SubschemaConfig<any, any, any, TContext>
4646
): GraphQLSchema {
47+
const typeNames = new Set<string>(Object.keys(originalWrappingSchema.getTypeMap()));
4748
return mapSchema(originalWrappingSchema, {
4849
[MapperKind.TYPE]: (type: GraphQLNamedType) => {
4950
if (isSpecifiedScalarType(type) && !this.renameBuiltins) {
@@ -55,9 +56,16 @@ export default class RenameTypes<TContext = Record<string, any>>
5556
const oldName = type.name;
5657
const newName = this.renamer(oldName);
5758
if (newName !== undefined && newName !== oldName) {
59+
if (typeNames.has(newName)) {
60+
console.warn(`New type name ${newName} for ${oldName} already exists in the schema. Skip renaming.`);
61+
return;
62+
}
5863
this.map[oldName] = newName;
5964
this.reverseMap[newName] = oldName;
6065

66+
typeNames.delete(oldName);
67+
typeNames.add(newName);
68+
6169
return renameType(type, newName);
6270
}
6371
},

0 commit comments

Comments
 (0)