Navigation Menu

Skip to content

Commit

Permalink
enhance(utils/rewireTypes): do not throw duplicate schema type error (#…
Browse files Browse the repository at this point in the history
…4706)

* enhance(utils/rewireTypes): do not throw duplicate schema type error

* Improvements

* Go

* Ok

* Tests

* Warning
  • Loading branch information
ardatan committed Sep 9, 2022
1 parent 13b9af9 commit 43c736b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-crabs-report.md
@@ -0,0 +1,5 @@
---
'@graphql-tools/utils': minor
---

Do not throw duplicate type error name while rewiring types
5 changes: 5 additions & 0 deletions .changeset/nice-moose-compete.md
@@ -0,0 +1,5 @@
---
'@graphql-tools/wrap': minor
---

RenameTypes: do not rename type if the new name already exists in the schema
4 changes: 2 additions & 2 deletions packages/loaders/url/tests/url-loader.spec.ts
Expand Up @@ -584,7 +584,7 @@ describe('Schema URL Loader', () => {
})) as ExecutionResult;
expect(result.data).toBeUndefined();
expect(result.errors).toBeDefined();
expect(result.errors?.[0].message).toBe('fetch failed to http://127.0.0.1:9777/graphql');
expect(result.errors?.[0].originalError?.message).toContain('failed');
expect(result.errors?.[0].message).toContain('failed');
expect(result.errors?.[0].message).toContain('http://127.0.0.1:9777/graphql');
});
});
5 changes: 3 additions & 2 deletions packages/utils/src/heal.ts
Expand Up @@ -75,8 +75,9 @@ export function healTypes(
continue;
}

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

actualNamedTypeMap[actualName] = namedType;
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/src/rewire.ts
Expand Up @@ -54,7 +54,8 @@ export function rewireTypes(
}

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

newTypeMap[newName] = namedType;
Expand Down
8 changes: 8 additions & 0 deletions packages/wrap/src/transforms/RenameTypes.ts
Expand Up @@ -44,6 +44,7 @@ export default class RenameTypes<TContext = Record<string, any>>
originalWrappingSchema: GraphQLSchema,
_subschemaConfig: SubschemaConfig<any, any, any, TContext>
): GraphQLSchema {
const typeNames = new Set<string>(Object.keys(originalWrappingSchema.getTypeMap()));
return mapSchema(originalWrappingSchema, {
[MapperKind.TYPE]: (type: GraphQLNamedType) => {
if (isSpecifiedScalarType(type) && !this.renameBuiltins) {
Expand All @@ -55,9 +56,16 @@ export default class RenameTypes<TContext = Record<string, any>>
const oldName = type.name;
const newName = this.renamer(oldName);
if (newName !== undefined && newName !== oldName) {
if (typeNames.has(newName)) {
console.warn(`New type name ${newName} for ${oldName} already exists in the schema. Skip renaming.`);
return;
}
this.map[oldName] = newName;
this.reverseMap[newName] = oldName;

typeNames.delete(oldName);
typeNames.add(newName);

return renameType(type, newName);
}
},
Expand Down

1 comment on commit 43c736b

@vercel
Copy link

@vercel vercel bot commented on 43c736b Sep 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.