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

graphql-modules-preset is not adding interface and union resolvers to Module.Resolver #7123

Open
Tracked by #8296 ...
ghost opened this issue Dec 1, 2021 · 4 comments
Open
Tracked by #8296 ...
Labels
core Related to codegen core/cli

Comments

@ghost
Copy link

ghost commented Dec 1, 2021

Describe the bug

When using graphql-module framework, the generated resolver is missing definition for interfaces and unions.
To Reproduce
Steps to reproduce the behavior:
Repo that reproduce the issue with readme for explanantion
https://github.com/laurent-lubino-roam/graphql-modules-codegen-resolver

Expected behavior

I would expect to be able to resolve types directly in the resolver using resolveType instead of isTypeOf

Environment:

  • OS: macOS
  • @graphql-codegen/graphql-module-preset:
  • NodeJS: 16

Additional context

@ghost ghost changed the title graphql-modules-preset is not adding interface resolver to Module.Resolver graphql-modules-preset is not adding interface and union resolvers to Module.Resolver Dec 1, 2021
@the-ult
Copy link

the-ult commented Mar 10, 2022

Is there a way to fix/work around this?
Having the same problems with an union based on interfaces.

union MyUnion = InterfaceImpl1 | InterfaceImpl2 | InterfaceImpl3


interface MyInterface{
  id: String!
}

type InterfaceImpl1 implements MyInterface{
  id: String!
  ...
}

type InterfaceImpl2 implements MyInterface{
  id: String!
  ...
}

type InterfaceImpl3 implements MyInterface{
  id: String!
  ...
}

InterfaceImpl1, InterfaceImpl2 and InterfaceImpl3 are defined in the generated export interface Resolvers {}
But MyUnion is not

Same when only using Unions.
They are defined as types in the generated file but not in the resolver.

@ghost
Copy link
Author

ghost commented Mar 10, 2022

HI @the-ult, the workaround we've used was to define each type (InterfaceImpl1, InterfaceImpl2 and InterfaceImpl3) that implements the base interface (for you MyInterface) in the resolver and define a __isTypeOf like so:

export const resolvers: MyInterfaceModule.Resolvers = {
   InterfaceImpl3: {
       __isTypeOf: (obj) => {
           return obj.type === 'InterfaceImpl3' // or whatever helps you to identify your object type
      }
   }
}

instead of having one __resolveType in the base interface (MyInterface) resolver.

Also, we've stopped using union and the result types are using the base interface (eg. extend type Query { getMyInterface: MyInterface }), then in your queries, you can do something like:

query GetMyInterface {
    getMyInterface {
       id
       ... on InterfaceImpl1 { .... }
       ... on InterfaceImpl2 { .... }
       ... on InterfaceImpl3 { .... }
    }
}

Hope it helps.

@the-ult
Copy link

the-ult commented Mar 10, 2022

Thanks gonna try it out tomorrow.

Would be great though, if this could be fixed/added to the graphql-modules-preset

@the-ult
Copy link

the-ult commented Mar 11, 2022

@dotansimha is this issue a bug or a feature => intended?


I used to fix 🤔 it like this:

export const PersonResolvers: PersonModule.Resolvers | unknown = {}

But that way you'll lose all Typings.. 😳

Was trying it now like this:

export const PersonResolvers: PersonModule.Resolvers & { PersonUnion } = {}

Not much better, but at least this way we keep the types of all other PersonResolver properties.

Gonna try @laurent-lubino-roam way today.

@charlypoly charlypoly added the core Related to codegen core/cli label Nov 3, 2022
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
Projects
None yet
Development

No branches or pull requests

2 participants