Skip to content

[typescript-resolver] interface resolver should could use type mappers  #4121

Open
@LucienLee

Description

@LucienLee

At graphql-codegen@1.13.0, we could map interface type to our own type. But at v.1.13.1, in the #3538 #3618, the behavior is changed. The interface will be the union type of its implements types, even I pass the mapper type in the config. I hope that we can bring interface type mapper back when we have set it in the config.

That's said in the same case of books.

interface Book {
  title: String
}

type TextBook implements Book {
  title: String
  cost: Float
}

type ColoringBook implements Book {
  title: String
  colors: [String]
}

type Shell {
  id: ID!
  books: [Book!]!
}

If we store the shell data in the nosql database in the following format:

id books
1 [ { type: 'TextBook', id: 111 }, { type: 'ColoringBook', id: 222 }, ... ]
... ...
const resolver = {
  Query: {
    shell() {
       return shellCollections.get();
    }
  },
  Book {
   __resolveType(parent, _context, _info) {
      // i would like to map the parent type as my model type rather the union of TextBook and ColoringBook
    }
}

That's quite useful if you work in the Apollo Federation.
For example, that's said there are 3 services: Book, User, Organization. In the book service, a book could be published by a user and an organization and I store the type and id in the book database. I would like to resolve the more publisher info from User, Organization services.

# book service
interface Publisher {
  id: ID!
  name: String!
}

extend type User implements Publisher {}
extend type Organization implements Publisher {}

In that kind of case, we can not get all of data in the book service and it's interface type resolvers can not determine the type by their unique property. We might use our model type to determine which typename it is. Hence, please consider that:
if an interface is assigned the type mapper, use the mapped type instead of union type.

Metadata

Metadata

Assignees

Labels

coreRelated to codegen core/cliplugins

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions