Skip to content

Implementing ResolverInterface breaks typings #23

Open
@giacomorebonato

Description

@giacomorebonato

Reporting an example where the usage of ResolverInterface on the resolver break the typings on the property decorated with @FieldResolver.

@Resolver((_of) => Task)
class TaskResolver implements ResolverInterface<Task> {
  async category(@Root() task: Task) {
    return (dataloader: DataLoader<string, Category>) => {
      if (task.categoryId === undefined) return null

      return dataloader.load(task.categoryId)
    }
  }
}

@ObjectType({ description: 'A task is something to do' })
class Task extends TimeStamps {
  @GQLField()
  _id: string = ''

  @MongoField()
  @GQLField()
  name: string = ''

  @MongoField()
  @GQLField()
  completed: boolean = false

  @MongoField()
  categoryId?: string

  @GQLField({ nullable: true })
  category?: Category
}

The error is:

error TS2416: Property 'category' in type 'TaskResolver' is not assignable to the same property in base type 'ResolverInterface<Task>'.
  Type '(task: Task) => Promise<(dataloader: DataLoader<string, Category, string>) => Promise<Category> | null>' is not assignable to type '(root: Task, ...args: any[]) => Category | Promise<Category | undefined> | undefined'.
    Type 'Promise<(dataloader: DataLoader<string, Category, string>) => Promise<Category> | null>' is not assignable to type 'Category | Promise<Category | undefined> | undefined'.
      Type 'Promise<(dataloader: DataLoader<string, Category, string>) => Promise<Category> | null>' is not assignable to type 'Promise<Category | undefined>'.
        Property '_id' is missing in type '(dataloader: DataLoader<string, Category>) => Promise<Category> | null' but required in type 'Category'.

46   async category(@Root() task: Task) {

The solution at the moment is not to implement the interface and the implementation works.
It first look ResolverInterface should be enriched in type-graphql, but I am posting here first for opinion and feedback and I'll close the issue if it doesn't make sense to be here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions