Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

FilterTypes Transform can break an UnionType #16

Closed
AndKiel opened this issue Aug 19, 2019 · 3 comments
Closed

FilterTypes Transform can break an UnionType #16

AndKiel opened this issue Aug 19, 2019 · 3 comments

Comments

@AndKiel
Copy link

AndKiel commented Aug 19, 2019

Given:

  • ObjectTypes A, B, and C
  • UnionType with types A, B, and C

When:

  • FilterType transform is used to hide one of the ObjectTypes included in the UnionType

Then:

  • ObjectType which was hidden becomes null inside UnionType, making schema invalid

My current workaround is removing null members and recreating UnionTypes without them:

import { GraphQLNamedType, GraphQLSchema, GraphQLUnionType } from "graphql";
import { Transform } from "graphql-tools-fork";
import { visitSchema, VisitSchemaKind } from "graphql-tools-fork/dist/transforms/visitSchema";

export class CleanUpUnionTypes implements Transform {
  public transformSchema(originalSchema: GraphQLSchema): GraphQLSchema {
    return visitSchema(originalSchema, {
      [VisitSchemaKind.UNION_TYPE](type): GraphQLNamedType {
        const config = (<GraphQLUnionType>type).toConfig();
        config.types = config.types.filter(Boolean);
        return new GraphQLUnionType(config);
      }
    });
  }
}

The question is, should this be responsibility of FilterType transform to clean up unions as an additional step?

@yaacovCR
Copy link
Owner

I think so! My hastily put together filterSchema function should do this, you can possibly try that instead of the FilterTypes transform.

@AndKiel
Copy link
Author

AndKiel commented Aug 19, 2019

There is one more edge-case related to types filtering.
When you filter a type and another type has a field using that type, and it has no other fields, it becomes an "empty" type, also making schema invalid.

EDIT: (that can be opened as another issue, I guess, would be blocked by #14 because it's impossible to write custom transform returning null/undefined at the moment)

@yaacovCR
Copy link
Owner

Should be fixed in v6.4 thanks to the use of healing/pruning within the new consolidated visitSchema. Have to work on docs.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants