ardatan / graphql-tools

:wrench: Utility library for GraphQL to build, stitch and mock GraphQL schemas in the SDL-first approach
https://www.graphql-tools.com
MIT License
5.35k stars 810 forks source link

Interference between `RenameInputObjectFields` transforms #5091

Open cgcoss opened 1 year ago

cgcoss commented 1 year ago

Issue workflow progress

Progress of the issue based on the Contributor Workflow


Describe the bug

RenameInputObjectFields transforms seem to be interfering with each other resulting in field names not transforming as expected. The bug has not been identified specifically, but a failing test is provided below which is a modification of the first test in packages/wrap/tests/transformRenameInputObjectFields.test.ts. The test below passes when static args are provided in the document.

test('renaming with arguments works', async () => {
    const schema = makeExecutableSchema({
      typeDefs: /* GraphQL */ `
        input InputObject {
          field1: String
          field2: String
        }

        type OutputObject {
          field1: String
          field2: String
        }

        type Query {
          test(argument: InputObject): OutputObject
        }
      `,
      resolvers: {
        Query: {
          test: (_root, args) => {
            return args.argument;
          },
        },
      },
    });

    const transformedSchema = wrapSchema({
      schema,
      transforms: [
        new RenameInputObjectFields((typeName, fieldName) => {
          if (typeName === 'InputObject' && fieldName === 'field2') {
            return 'field3';
          }
        }),
        new RenameInputObjectFields((typeName, fieldName) => {
          if (typeName === 'InputObject' && fieldName === 'field1') {
            return 'field0';
          }
        }),
      ],
    });

    const query = /* GraphQL */ `
      query ($argument: InputObject) {
        test(argument: $argument) {
          field1
          field2
        }
      }
    `;

    const result = await execute({
      schema: transformedSchema,
      document: parse(query),
      variableValues: { argument: { field0: "field1", field3: "field2" } },
    });
    if (isIncrementalResult(result)) throw Error('result is incremental');
    assertSome(result.data);
    const testData: any = result.data['test'];
    expect(testData.field1).toBe('field1');
    expect(testData.field2).toBe('field2');
  })`

To Reproduce Steps to reproduce the behavior:

Run the failing test provided above.

Expected behavior

The fields should be renamed and the test should pass.

Environment:

ardatan commented 1 year ago

Could you create a PR with this test? Maybe you can try to fix that as well?

cgcoss commented 1 year ago

Thanks for the quick response. I opened a PR with two tests, one that passes and a similar one that does not. I spent some time trying to debug, but was not able to identify or fix the issue.