remorses / genql

Type safe TypeScript client for any GraphQL API
https://genql.dev
MIT License
868 stars 36 forks source link

inferred type from a query fetching specific fields from an interface returns a type with all fields instead #180

Open arie-finaloop opened 2 months ago

arie-finaloop commented 2 months ago

Happens in 6.3.3.

type schema

interface Foo {
  id: ID!
  fooType: String!
....
  createdAt: Date!
  updatedAt: Date!
  deletedAt: Date
  publishedAt: Date
...
}

type Bar implements Foo @key(fields: "id") {
....
} 

type Baz implements Foo @key(fields: "id") {
....
} 

query

extend type Query {
...
  getFoos(input: GetFoosInput!): GetFoosResult!
...
}
type GetFoosResult {
  foos: [Foo!]!
}

type resolvers

export const typeResolvers: Resolvers = {
  Foo: {
    __resolveType: (_parent, _context) => {
      return 'Bar';
    },
  },
};

typescript function with bad return type

export const getFoosByIds = async (params: GetFoosByIds) => {
  const data = await gqlClient.query({
    getFoos: {
      __args: {
        input: {
        ...
        },
      },
      foos: {
        id: true,
        __typename: true,
      },
    },
  });
  return data.getFoos.foos[0];
}

the returning type has all fields and not only id and __typename

linear[bot] commented 2 months ago

GEN-33 inferred type from a query fetching specific fields from an interface returns a type with all fields instead

arie-finaloop commented 2 months ago

looks like there is a similar issue when Foo is a union of Bar and Baz (where Bar and Baz are concrete types that share the fields of the foo "interface")