Graphql: Union types aren't working with schema sorting enabled

Created on 23 Aug 2020  路  5Comments  路  Source: nestjs/graphql

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

All 5 comments

Also, I didn't find any similar issues, not in nestjs repository nor the graphql repository. That's why my assumption is that this bug is reproducible only with nestjs.

Just ran into the same issue with a different Nest version:

Nest version: ^7.0.0
Nest Graphql version: ^7.6.0

@mattleff would you like to take a look into this PR? It seems to be related to the feature you've added.

@kamilmysliwiec I'll take a look soon.

This was fixed upstream with https://github.com/graphql/graphql-js/pull/2793. A release hasn't been cut yet.

Was this page helpful?
0 / 5 - 0 ratings