graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

Aliased fields always null when `mergeSchemas` and `stitchSchemas` combined.

Open jacobkatz-tamg opened this issue 2 years ago • 0 comments
trafficstars

Issue workflow progress

Progress of the issue based on the Contributor Workflow

  • [ ] 1. The issue provides a reproduction available on Github, Stackblitz or CodeSandbox

    Make sure to fork this template and run yarn generate in the terminal.

    Please make sure the GraphQL Tools package versions under package.json matches yours.

  • [ ] 2. A failing test has been provided
  • [ ] 3. A local solution has been provided
  • [ ] 4. A pull request is pending review

Describe the bug

Queries with aliases fail when @graphql-tools/schema's mergeSchemas is used to merge a schema produced by @graphql-tools/stitch's stitchSchemas

To Reproduce Steps to reproduce the behavior:

For example, here is a test that fails today:

import { graphql } from 'graphql';

import { makeExecutableSchema } from '@graphql-tools/schema';
import { mergeSchemas } from '@graphql-tools/schema';
import { stitchSchemas } from '@graphql-tools/stitch';
import { assertSome } from '@graphql-tools/utils';

describe('aliasing', () => {
  test('works', async () => {
    let chirpSchema = makeExecutableSchema({
      typeDefs: /* GraphQL */ `
        type Chirp {
          id: ID!
          text: String
        }

        type Query {
          chirpById(id: ID!): Chirp
        }
      `,
      resolvers: {
        Query: {
          chirpById: (_obj, args ) =>
            args.id % 3 === 0 ? null : {
              id: args.id,
              text: 'chirp ' + args.id
            }
          },
        },
    })
    let dummySchema = makeExecutableSchema({
      typeDefs: /* GraphQL */ `
        type Query {
          dummy: String
        }
      `,
      resolvers: {
        Query: {
          dummy: () => null
        },
      }
    })

    const mergedSchema = mergeSchemas({
      schemas: [stitchSchemas({subschemas: [chirpSchema, dummySchema]})],
    });

    const query = /* GraphQL */ `
      query {
        thisBreaks: chirpById(id: 2) {
          id
          textAlias: text
        }
        working: chirpById(id: 2) {
          id
          text
        }
        alsoWorking: chirpById(id: 2) {
          id
          text
          textAlias: text
        }
      }
    `;

    const result = await graphql({ schema: mergedSchema, source: query });

    expect(result.errors).toBeUndefined();
    assertSome(result.data);
    const working: any = result.data['working'];
    expect(working.id).not.toBe(null);
    expect(working.id).toBe('2');
    expect(working.text).toBe('chirp 2');

    const alsoWorking: any = result.data['alsoWorking'];
    expect(alsoWorking.id).not.toBe(null);
    expect(alsoWorking.id).toBe('2');
    expect(alsoWorking.text).toBe('chirp 2');
    expect(alsoWorking.textAlias).toBe('chirp 2');

    const thisBreaks: any = result.data['thisBreaks'];
    expect(thisBreaks.id).not.toBe(null);
    expect(thisBreaks.id).toBe('2');
    // it breaks right here - this field is null and should not be.
    expect(thisBreaks.textAlias).toBe('chirp 2');
  });
});

Expected behavior

One would expect that if

query {
  chirpById(id: 2) {
    text
  }
}

resolves to { chirpById: { text: "chirp 2" } },

query {
  chirpById(id: 2) {
    textAlias: text
  }
}

would resolve to { chirpById: { textAlias: "chirp 2" } }, but instead it resolves to { chirpById: { textAlias: null } }

Environment:

  • OS: amazon linux, osx, etc
  • @graphql-tools/...:
  • NodeJS:

Additional context

I have not been able to reproduce with just mergeSchemas or just stitchSchemas.

jacobkatz-tamg avatar May 09 '23 00:05 jacobkatz-tamg