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-resolver] interface resolver should could use type mappers #4121

Open
Tracked by #8296 ...
LucienLee opened this issue May 28, 2020 · 1 comment
Open
Tracked by #8296 ...
Assignees
Labels
core Related to codegen core/cli plugins

Comments

@LucienLee
Copy link

At [email protected], 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.

@lionkeng
Copy link

lionkeng commented May 30, 2020

I am running into a similar issue. mappers correctly maps simple graphQL type to my own declared Typescript type model. But for a graphQL interface, graphql-codegen is generating unioned Typescript types and ignores my configuration in mappers to use my model for graphQL types in my schema that implements an interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to codegen core/cli plugins
Projects
None yet
Development

No branches or pull requests

5 participants