dotansimha / dotansimha/graphql-code-generator

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

Open
#4,121 1 comment 3 reactions 1 assignee Claimed by @kamilkisiela View on GitHub
core plugins
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

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.

```gql
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 }, ... ] |
| ... | ... |

```js
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.

```gql
# 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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.