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

Typescript generation does not accurately reflect interface types #1568

Open
mgagliardo91 opened this issue Oct 7, 2019 · 2 comments
Open
Labels
🤖 component - codegen related to the codegen core packages

Comments

@mgagliardo91
Copy link

mgagliardo91 commented Oct 7, 2019

Intended outcome:
With interface types, the codegen fails to produce the expected hierarchy of types.

For example:

GraphQL Schema

interface Auto {
  wheelCount: Int
}

type Car implements Auto {
  wheelCount: Int
  name?: String!
}

type Truck implements Auto {
  wheelCount: Int
  bedSize: Int!
}

Queries

// Top-level query
query GetAutos {
  autos {
    wheelCount
  }
}

// Specific query
query GetCars {
  autos {
    wheelCount
    ... on Car {
      name
    }
  }
}

Generated Code

interface GetAutos_autos {
  __typename: "Car" || "Truck"
  wheelCount: number;
}

interface GetCars_autos_Car {
  __typename: "Car"
  wheelCount: number;
  name: string;
}

interface GetCars_autos_Truck {
  __typename: "Truck"
  wheelCount: number;
  bedSize: number;
}

In the generated code above, I would have expected some interface <> extends <> structure so that we could pass a parent type around in our codebase.

interface Auto {
  __typename: "Car" || "Truck"
  wheelCount: number;
}

interface Car extends Auto {
  __typename: "Car"
  wheelCount: number;
  name: string;
}

interface Truck extends Auto {
  __typename: "Truck"
  wheelCount: number;
  bedSize: number;
}

Actual outcome:

There is no hierarchy and therefore I have to use fixed (non-dynamic) types in order for typescript to compile.

How to reproduce the issue:

See above example.

Versions

@JakeDawkins JakeDawkins added the 🤖 component - codegen related to the codegen core packages label Oct 11, 2019
@HosseinAgha
Copy link

@JakeDawkins When you have an interface in the schema with no Object implementing it, This gets into a major error. Codegen generates wrong field types with empty values. I think it is because it cannot detect the __typename for the interface type.

@KennyHammerlund
Copy link

Kind of a feature request, I didn't want to open a new one as I think it relates to this issue : It would also be nice if this would only generate the car type on autos instead of the union of car | truck as nothing from this query would be anything but a car

query GetCars {
  autos {
    ... on Car {
      wheelCount   <- moved inside interface
      name
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 component - codegen related to the codegen core packages
Projects
None yet
Development

No branches or pull requests

4 participants