From baf3c28f43dcfafffd15386daeb153bc2895c1b3 Mon Sep 17 00:00:00 2001 From: "Henry Q. Dineen" Date: Mon, 11 Mar 2024 06:09:43 -0400 Subject: [PATCH] fix(filterSchema): provide actual type name for root field (#5931) * fix(filterSchema): provide actual type name for root field * add changeset --- .changeset/warm-ducks-smash.md | 5 ++++ packages/utils/src/filterSchema.ts | 2 +- packages/utils/tests/filterSchema.test.ts | 29 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 .changeset/warm-ducks-smash.md diff --git a/.changeset/warm-ducks-smash.md b/.changeset/warm-ducks-smash.md new file mode 100644 index 00000000000..24e30d6073b --- /dev/null +++ b/.changeset/warm-ducks-smash.md @@ -0,0 +1,5 @@ +--- +"@graphql-tools/utils": patch +--- + +fix filterSchema argument filter for schema with non-default root types diff --git a/packages/utils/src/filterSchema.ts b/packages/utils/src/filterSchema.ts index 4360a6c0cca..1ab54f714cf 100644 --- a/packages/utils/src/filterSchema.ts +++ b/packages/utils/src/filterSchema.ts @@ -94,7 +94,7 @@ function filterRootFields( delete config.fields[fieldName]; } else if (argumentFilter && field.args) { for (const argName in field.args) { - if (!argumentFilter(operation, fieldName, argName, field.args[argName])) { + if (!argumentFilter(type.name, fieldName, argName, field.args[argName])) { delete field.args[argName]; } } diff --git a/packages/utils/tests/filterSchema.test.ts b/packages/utils/tests/filterSchema.test.ts index 73436defa05..617ebffdf86 100644 --- a/packages/utils/tests/filterSchema.test.ts +++ b/packages/utils/tests/filterSchema.test.ts @@ -236,4 +236,33 @@ describe('filterSchema', () => { ['field'].args.map(arg => arg.name), ).toEqual(['keep']); }); + + it('filters root field arguments for non-default query type', () => { + const schema = makeExecutableSchema({ + typeDefs: /* GraphQL */ ` + schema { + query: Root + } + type Root { + field(keep: String, omit: String): String + } + `, + }); + + const filtered = filterSchema({ + schema, + argumentFilter(typeName, fieldName, argName) { + if (typeName === 'Root' && fieldName === 'field' && argName === 'omit') { + return false; + } + return true; + }, + }); + + expect( + (filtered.getType('Root') as GraphQLObjectType) + .getFields() + ['field'].args.map(arg => arg.name), + ).toEqual(['keep']); + }); });