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

Unable to create a resolver for an interface type #436

Closed
justinmchase opened this issue Oct 5, 2019 · 1 comment
Closed

Unable to create a resolver for an interface type #436

justinmchase opened this issue Oct 5, 2019 · 1 comment
Labels
Duplicate 🔑 This issue or pull request already exists

Comments

@justinmchase
Copy link

Describe the bug
I am unable to create a resolver for an interface type. I want to create a resolver with a field resolver for an interface type but I am getting a crash at this line.

The reason why is that the resolver is defined in this.interfaceTypes instead of this.classTypes and so the find on the line before is null.

It doesn't crash if I change the previous line to be:

const objectType
  = this.objectTypes.find(o=> o.target === t)
  || this.interfaceTypes.find(o=> o.target === t); // <-- I added this

Then it successfully finds the objectType. I'm not sure if more is needed beyond this point or if that's all. Also, this should probably throw a helpful error if objectType is undefined after this rather than crashing with a null reference exception.

To Reproduce

@InterfaceType()
export class Character {
  @Field()
   public name: string
}

@Resolver(of => Character ) // <-- this crashes my app
export class CharacterResolver {
  constructor(
    private readonly characters: CharacterService
  ) { }

  @ResolveProperty(type => String)
  public async name(@Root() character: Character) {
    return this.charcaters.resolveName(character)
  }
}

Expected behavior

  • I expect there to be no crashing.
  • I expect the resolver class to resolve fields for types of that interface.

For example if I am returning an interface Character { name } and I query without fragments it should resolve to this function unless there is a more specific concrete type resolver declared:

query CharacterNames {
  characters {
    name # <-- should resolve to my CharacterResolver.name method
  }
}

Logs

TypeError: Cannot read property 'fields' of undefined
    at /d/code/livemenu-api/apps/api/node_modules/type-graphql/dist/metadata/metadata-storage.js:134:52
    at Array.forEach (<anonymous>)
    at MetadataStorage.buildFieldResolverMetadata (/d/code/livemenu-api/apps/api/node_modules/type-graphql/dist/metadata/metadata-storage.js:122:21)
    at MetadataStorage.build (/d/code/livemenu-api/apps/api/node_modules/type-graphql/dist/metadata/metadata-storage.js:77:14)
    at Function.generateFromMetadataSync (/d/code/livemenu-api/apps/api/node_modules/type-graphql/dist/schema/schema-generator.js:29:51)
    at Function.<anonymous> (/d/code/livemenu-api/apps/api/node_modules/type-graphql/dist/schema/schema-generator.js:16:33)
    at Generator.next (<anonymous>)
    at /d/code/livemenu-api/apps/api/node_modules/type-graphql/node_modules/tslib/tslib.js:110:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/d/code/livemenu-api/apps/api/node_modules/type-graphql/node_modules/tslib/tslib.js:106:16)
    at Function.generateFromMetadata (/d/code/livemenu-api/apps/api/node_modules/type-graphql/dist/schema/schema-generator.js:15:24)
    at /d/code/livemenu-api/apps/api/node_modules/type-graphql/dist/utils/buildSchema.js:11:65
    at Generator.next (<anonymous>)
    at /d/code/livemenu-api/apps/api/node_modules/type-graphql/node_modules/tslib/tslib.js:110:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/d/code/livemenu-api/apps/api/node_modules/type-graphql/node_modules/tslib/tslib.js:106:16)

Enviorment (please complete the following information):

  • OS: Ubuntu (windows wsl)
  • Node 12.8.0
  • Package version 0.17.5
  • TypeScript version 3.5.2

Additional context
I am using this via @nestjs

@MichalLytek
Copy link
Owner

MichalLytek commented Oct 5, 2019

Kinda duplicate of #260, related to #261 - @InterfaceType in TypeGraphQL was not designed as an abstract object type an has no arguments and resolvers support 😞

I'm not sure if more is needed beyond this point or if that's all.

It's not enough, resolvers are not created at all in graphql-js.

@MichalLytek MichalLytek added the Duplicate 🔑 This issue or pull request already exists label Oct 5, 2019
EndyKaufman pushed a commit to EndyKaufman/typegraphql-prisma-nestjs that referenced this issue Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🔑 This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants