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

graphql-typescript-definitions type collision #2548

Open
Flufd opened this issue Feb 3, 2023 · 0 comments
Open

graphql-typescript-definitions type collision #2548

Flufd opened this issue Feb 3, 2023 · 0 comments

Comments

@Flufd
Copy link
Contributor

Flufd commented Feb 3, 2023

Overview

Typescript type collision is possible with the generated types, in combination with imported schema types. This is because the generated types for a query are based on the key path inside the query, and those key paths could match GQL schema types.

example:

query MarketDetails {
  market(id:123) {
    manager {
      type 
      #...
    }
  }
}

The type on a market.manager is an enum called MarketManager.
The generated query type definitions then becomes:

import { MarketManager } from './where/schema/types/are';

export namespace MarketDetailsQueryData {
  export interface MarketManager {
    type: MarketManager;
  }
}

And then we have a recursive type reference to the containing type, instead of the imported schema type.

If we rename the field to something sensible for consuming code like marketManager, we force a type renaming to MarketMarketManager, which is an okay work around.

query MarketDetails {
  market(id:123) {
    marketManager: manager {
      type 
      #...
    }
  }
}
export namespace MarketDetailsQueryData {
  export interface MarketMarketManager {
    type: MarketManager;
  }
}

But note, if we define this market query part as a fragment, the types for the fragment are generated slightly different, so we get:

initially, there's no collision because the names don't have the Market prefix:

export namespace MarketDetailsFragmentData {
  export interface Manager {
    __typename: "MarketMarketManager";
    type: MarketManager;
  }
}

after the rename we get a collision:

export namespace MarketDetailsFragmentData {
 export interface MarketManager {
    __typename: "MarketMarketManager";
    type: MarketManager;
  }
}

I'm not consuming the fragment types directly, so this is not an issue.

A fix?

Maybe we could detect this and rename the imported schema types as they are imported.

import { MarketManager as _MarketManager } from './where/schema/types/are';

export namespace MarketDetailsFragmentData {
 export interface MarketManager {
    __typename: "MarketMarketManager";
    type: _MarketManager;
  }
}

Consuming repo

What repo were you working in when this issue occurred?
web

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

No branches or pull requests

1 participant