Skip to content

Union types aren't working with schema sorting enabled #1107

@Namielusi

Description

@Namielusi

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Union types combined with schema sorting stop working. Everything works fine until I try to query some data using inline fragment - server responds with empty object regardless of properties I've requested in the fragment.

I should say that the bug can be reproduced not only with sortSchema option, but also using lexicographicSortSchema.

Example

Example of not working code on the current package version.

import { GraphQLModule } from '@nestjs/graphql'

GraphQLModule.forRoot({
  playground: true,
  autoSchemaFile: 'schema.gql',

  sortSchema: true,
})
import { GraphQLModule } from '@nestjs/graphql'
import { lexicographicSortSchema } from 'graphql'

GraphQLModule.forRoot({
  playground: true,
  autoSchemaFile: 'schema.gql',

  transformSchema: lexicographicSortSchema,
})
Query Response
query {
  items {
    id
    settings {
      __typename
      ... on ItemFirstSettings {
        foo
      }
      ... on ItemSecondSettings {
        bar
      }
    }
  }
}
{
  "data": {
    "items": [
      {
        "id": "hello-world-first-id",
        "settings": {
          "__typename": "ItemFirstSettings"
        }
      },
      {
        "id": "hello-world-second-id",
        "settings": {
          "__typename": "ItemSecondSettings"
        }
      }
    ]
  }
}

Expected behavior

I should get the data I've requested in the query.

Example

Example of working code on the current package version. This behavior is also expected when using sorting.

GraphQLModule.forRoot({
  playground: true,
  autoSchemaFile: 'schema.gql',

  sortSchema: false,
})
Query Response
query {
  items {
    id
    settings {
      __typename
      ... on ItemFirstSettings {
        foo
      }
      ... on ItemSecondSettings {
        bar
      }
    }
  }
}
{
  "data": {
    "items": [
      {
        "id": "hello-world-first-id",
        "settings": {
          "__typename": "ItemFirstSettings",
          "foo": "This SHOULD be displayed"
        }
      },
      {
        "id": "hello-world-second-id",
        "settings": {
          "__typename": "ItemSecondSettings",
          "bar": "This SHOULD be displayed"
        }
      }
    ]
  }
}

Minimal reproduction of the problem with instructions

I've prepared the repository. Steps to reproduce:

  1. Clone repository
  2. npm install
  3. Open src/app.module.ts and tweak options according to the comments
  4. npm run dev
  5. Go to localhost:3000
  6. Use the query from the "Current behavior" section

What is the motivation / use case for changing the behavior?

For me personally, it doesn't make much difference to have schema sorting enabled or not, but it was not obvious for me that the issue was caused by the sorting option.

Actually I'm not sure if it's an issue with nestjs and should we fix it at all. But it took me a lot of time to found this behavior and at least I hope to help someone that may face the same issue.

If it's not the issue with nestjs or it won't be fixed, I think this behavior should be mentioned in the nestjs docs.

Environment


Nest version: ^7.4.2
Nest Graphql version: ^7.6.0

 
For Tooling issues:
- Node version: v14.8.0  
- Platform: Linux 

Others:
Ubuntu 20.04.1 LTS
NPM 6.14.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions