Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix TS errors related to FieldSets #1030

Merged
merged 3 commits into from Sep 28, 2021

Conversation

trevor-scheer
Copy link
Member

@trevor-scheer trevor-scheer commented Sep 27, 2021

We have some overlapping types like FederationType and FederationTypeMetadata
where properties like keys and requires should always be a FieldSet.

This commit simplifies the code a bit and adds assertions + casts where
necessary to ensure these types match as we intend them to.

How these types have been out of sync for so long is surprising. I've
noticed them in the past but it's never been a blocking issue until
I was working on some code with @o0Ignition0o and we ran into these
TS errors.

In any case I consider this to be a low-lift improvement worth landing,
even though I suppose there are some types we could combine to
simplify this a bit further.

The long-existing errors I'm referring to:

federation-js/src/composition/DirectiveMetadata.ts:187:13 - error TS2322: Type 'FederationField' is not assignable to type 'FederationFieldMetadata'.

187             federation: fieldFederationMetadata,
                ~~~~~~~~~~

federation-js/src/composition/compose.ts:547:13 - error TS2322: Type 'FederationField' is not assignable to type 'FederationFieldMetadata'.
  Types of property 'requires' are incompatible.
    Type 'readonly SelectionNode[] | undefined' is not assignable to type 'FieldSet | undefined'.
      Type 'readonly SelectionNode[]' is not assignable to type 'readonly (FieldNode | InlineFragmentNode)[]'.
        Type 'SelectionNode' is not assignable to type 'FieldNode | InlineFragmentNode'.
          Type 'FragmentSpreadNode' is not assignable to type 'FieldNode | InlineFragmentNode'.
            Type 'FragmentSpreadNode' is not assignable to type 'FieldNode'.
              Types of property 'kind' are incompatible.
                Type '"FragmentSpread"' is not assignable to type '"Field"'.

547             federation: fieldFederationMetadata,
                ~~~~~~~~~~

federation-js/src/composition/compose.ts:573:11 - error TS2322: Type 'FederationField' is not assignable to type 'FederationFieldMetadata'.

573           federation: fieldFederationMetadata,
              ~~~~~~~~~~

federation-js/src/composition/compose.ts:595:13 - error TS2322: Type 'FederationField' is not assignable to type 'FederationFieldMetadata'.

595             federation: fieldFederationMetadata,

We have some overlapping types like FederationType and FederationTypeMetadata
where properties like keys and requires should always be a FieldSet.

This commit simplifies the code a bit and adds assertions+casts where
necessary to ensure these types match as we intend them to.

How these types have been out of sync for so long is surprising. I've
noticed them in the past but it's never been a blocking issue until
I was working on some code with @o0Ignition0o and we ran into these
TS errors.
@trevor-scheer trevor-scheer changed the title Fix TS errors related to FieldSets chore: Fix TS errors related to FieldSets Sep 27, 2021
@trevor-scheer trevor-scheer added 2021-09 🧬 typings Updates to type system types labels Sep 27, 2021
Copy link
Contributor

@martijnwalraven martijnwalraven left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this definitely seems like a sensible fix.

I think the reason these differences haven't been a problem before is that the only time these packages are compiled together is during testing, because ts-jest doesn't support per-project TypeScript configuration. In other cases, these types exist in isolated projects and shouldn't affect each other.

That's why compilation with tsc (as we do in npm run compile or before tests) works fine, but running tests spits out these errors. (And it only does that the first time you run tests it seems, because ts-jest caches the results).

Somewhat related, parseFieldSet has always been a bit of a hack, because we're forced to parse a document and extract the selections. Recent versions of graphql-js expose Parser, so we should be able to start parsing with the right parsing rule directly (as is done for existing utility methods like parseValue). Not sure when we're ready to drop support for older versions of graphql-js though.

@abernix abernix removed their request for review September 28, 2021 10:00
@trevor-scheer
Copy link
Member Author

@martijnwalraven good tip about the Parser class! We already require ^15.5.3 so I went ahead and made that change.

@glasser
Copy link
Member

glasser commented Sep 28, 2021

@martijnwalraven does that mean that after this change we may be able to simplify/improve something about the Jest/TS setup in this repo?

@trevor-scheer trevor-scheer merged commit 3523aba into main Sep 28, 2021
@trevor-scheer trevor-scheer deleted the trevor/fix-fieldset-type-errors branch September 28, 2021 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧬 typings Updates to type system types
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants