question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for property resolvers for interface types

See original GitHub issue

I’m submitting a…

[x] Feature request

Current behavior

I would like to create a resolver for an interface type which has a property resolver

Expected behavior

I should be able to define a resolver for an interface type and add property resolvers for virtual fields on that resolver. It should have access to services via constructor injection, etc.

Minimal reproduction of the problem with instructions

@InterfaceType()
export class IExample {
}

@Resolver(of => IExample)
export class ExampleResolver {
  constructor(
    private readonly example: ExampleService
  ) { }

  @ResolveProperty(type => Boolean)
  public async foo(@Root() item: IExample) {
    // this.example is available for this function
    return false
  }
}

What is the motivation / use case for changing the behavior?

If you have an interface type with multiple concrete types you want to be able to access it in gql like:

examples {
  foo
}

Not

examples {
  ... on ConcreteType {
    foo
  }
}

Which it seems like right now the only way to do it is to promote the field to resolvers for each concrete type.

Environment

Nest version: ^6.7.2 For Tooling issues:

  • Node version: v12.8.0
  • Platform: linux

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
justinmchasecommented, Oct 5, 2019

Looking at this more, adding the field to the interface doesn’t help. I am getting a crash in type-graphql which I’m filing a bug over there for but I do think that this is a legitimate issue in this library and think this ticket should be re-opened.

I think its legitimate because I am able to do this with graphql without using this library and it is documented in their documentation and it is a limitation of this library that this isn’t supported.

I think the example from the gql documentation for characters that @marcus-sa linked to is a good one, so this feature request / bug becomes how can I implement Character.name as a property resolver?

@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.charcters.resolveName(character)
  }
}
1reaction
marcus-sacommented, Oct 4, 2019

You can’t query interfaces, you can only implement them.

EDIT: It seems like I’ve misunderstood your question. This is out of the scope (as it’s not supported in the GraphQL spec)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolvers - Apollo GraphQL Docs
This object is called the resolver map. The resolver map has top-level fields that correspond to your schema's types (such as Query above)....
Read more >
graphql - combining resolvers for interface and concrete types
For some reason I'm having a hard time figuring out how to combine resolvers for a GraphQL interface and a type that implements...
Read more >
Resolvers – GraphQL Tools
Writing resolvers with graphql-tools. ... Unions and interfaces are great when you have fields that are in common between two types.
Read more >
Resolvers | NestJS - A progressive Node.js framework
Recommended: define an interface instead of using any . Type is imported from the @nestjs/common package; The isAbstract: true property indicates that SDL...
Read more >
Interfaces and inheritance - TypeGraphQL
The main idea of TypeGraphQL is to create GraphQL types based on TypeScript classes. In object-oriented programming it is common to create interfaces...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found