type-graphql icon indicating copy to clipboard operation
type-graphql copied to clipboard

Resolver Inheritance with dynamic field resolvers name not working properly

Open YassinEldeeb opened this issue 3 years ago • 2 comments

Problem:

So I made a base resolver to produce two field resolvers for user type which are -> followers and following to overwrite them from the parent resolver and give them pagination and sorting.

They're so similar followers and following resolvers both have the exact same code but call a different data loader.

I made two instances from that base resolver and just provide the resolver name to it, But the Problem is that only the first instance of the two works, and I've emitted the graphql schema to look it up, but only I can find one of my two field resolvers, The interesting part is now I've two instances -> followers -> following, and as I said the first one only works but when I flip the order of them like this two instances -> following -> followers now following is working and followers doesn't work, so the first instance I make is the only one targeted and the other below it is just ignored.

Note: when copying the exact same base resolver code and split it to two seperate identical resolver besides calling just different data loader it works and resolver Inheritance in general works for me but with field resolvers with dynamic name it doesn't work.

Screenshot 2021-07-25 223314

Screenshot 2021-07-25 223527

Screenshot 2021-07-25 223535

To Reproduce:

Screenshot 2021-07-25 221930

Expected Behavior

I should be getting two field resolvers for the user type and both should have pagination and sorting arguments applied to it besides calling the right data loader

Logs

Everything is working fine, no errors or crashes.

Screenshot 2021-07-25 225000

Environment (please complete the following information):

  • OS: Windows
  • Node v16.4.0
  • Package version v1.1.1
  • TypeScript version v4.3.2

Additional Context:

my ts.config

Screenshot 2021-07-25 225418

YassinEldeeb avatar Jul 25 '21 20:07 YassinEldeeb

Please create a minimal reproducible code example. I need to run the code to debug and see what's happening.

Minimal means minimal - do not paste your whole project, just try to recreate your issue from scratch, with as less classes, function and properties as possible. The best would be to take simple-usage example and modify it with your dynamic field resolvers inheritance case. https://github.com/MichalLytek/type-graphql/tree/master/examples/simple-usage

MichalLytek avatar Aug 05 '21 12:08 MichalLytek

Here is a very minimal example to reproduce the issue,

import 'reflect-metadata'
import { GraphQLServer } from 'graphql-yoga'
import {
  buildSchema,
  Field,
  FieldResolver,
  ObjectType,
  Query,
  Resolver,
} from 'type-graphql'

@ObjectType()
class User {
  @Field()
  name: string

  @Field(() => [Follower])
  followers: Follower[]

  @Field(() => [Follower])
  following: Follower[]
}

@ObjectType()
class Follower {
  @Field()
  follower_name: string
}

@Resolver()
class UserResolver {
  @Query(() => User)
  user(): User {
    return {
      name: 'Yassin',
      followers: [{ follower_name: 'Amin' }],
      following: [{ follower_name: 'Michal' }],
    }
  }
}

function createBaseResolver(name: 'following' | 'followers') {
  @Resolver((_of) => User)
  class BaseResolver {
    @FieldResolver((_type) => [Follower], { name })
    async getFollowers(): Promise<Follower[]> {
      return [
        {
          follower_name: 'OverWritten!',
        },
      ]
    }
  }

  return BaseResolver
}

const FollowersResolver = createBaseResolver('followers')
const FollowingResolver = createBaseResolver('following')

const main = async () => {
  const schema = (await buildSchema({
    resolvers: [UserResolver, FollowersResolver, FollowingResolver],
  })) as any

  const server = new GraphQLServer({ schema })

  server.start(() => console.log('Server is running on localhost:4000'))
}

main()

Here I just used Typegraphql and Graphql-Yoga server as for dependencies.

And I got the same exact issue, the first resolver I inherit from the baseResolver is the only one that is detected the other below it is just ignored, and when I swap them the first resolver works and the other don't

Screenshot 2021-08-06 031021

YassinEldeeb avatar Aug 06 '21 01:08 YassinEldeeb