Adrinalin4ik / Nestjs-Graphql-Tools

NestJS Graphql Tools is a flexible solution that provides a bunch of decorators for solving problems like n+1 request, filtering, sorting, pagination, polymorphic relation, graphql field extraction. It is fully based on decorators. To use it you can just add a decorator to your resolver.
GNU General Public License v3.0
79 stars 8 forks source link

Polimorphic relations are not working #29

Closed jaydev-jainam closed 1 year ago

jaydev-jainam commented 1 year ago

https://github.com/Adrinalin4ik/Nestjs-Graphql-Tools/discussions/28#discussion-5619026

@Adrinalin4ik Can you please resolve this issue

Adrinalin4ik commented 1 year ago

@jaydev-jainam Hey, can you please describe the issue here with the steps of reproduction, examples, actual and expected results.

jaydev-jainam commented 1 year ago

This is my resolver Field

  @ResolveField('reference',() => [ReferenceUnion])
    @GraphqlLoader({
        polymorphic: {
          id: 'referenceId',
          descriminator : 'type'
        }
      }) 
    async reference(@Loader() loader : PolymorphicLoaderData<[CourseObjectType | UserObjectType],string,TestimonialTypeEnum>,
                    @SelectedUnionTypes() types : SelectedUnionTypesResult)  {

        const result = await this.testimonialService.getBatchReference(loader,types);
        console.log(loader.ids)
       return loader.helpers.mapOneToManyPolymorphicRelation(result, loader.ids,'referenceId');
    }

And This is my service function for resolver field

async getBatchReference(loader : PolymorphicLoaderData<[CourseObjectType | UserObjectType],string,TestimonialTypeEnum>,
                            types  : SelectedUnionTypesResult)  {
        let results = [];

        for( let item of loader.polimorphicTypes){
            switch(item.descriminator){

                case TestimonialTypeEnum.INSTRUCTOR : 
                const instructors = await this.userRepository.createQueryBuilder("user")
                                    .where({
                                        id: In(item.ids)
                                      })
                                    .getMany()

                                    console.log(instructors)

                const instructorMap = this.mapper.mapArray(instructors,User,UserObjectType)

                    results.push({ descriminator: TestimonialTypeEnum.INSTRUCTOR, entities: instructorMap})
                break ;

                case TestimonialTypeEnum.COURSE : 
                const courses = await this.courseRepository.createQueryBuilder("course")
                                    .setFindOptions({ relations: ["courseMentors","courseLanguages"] })
                                    .where({
                                        id: In(item.ids)
                                      })
                                    .getMany()
                const courseMap = this.mapper.mapArray(courses,Course,CourseObjectType)         
                    results.push({ descriminator: TestimonialTypeEnum.COURSE, entities: courseMap})
                break ;

            }

        }
        console.log(results)
        return results;
    }

The problem arises when attempting to map Course and Instructor entities to the Testimonial entity, which have a polymorphic relationship. The provided code snippets appear to be correctly structured, but there may be a discrepancy in the configuration or implementation that prevents successful mapping. Further examination is needed to pinpoint the exact source of the problem. We have many to One relation of testimonial and course to instructor.In resolver field its return null even I have reference id in entity.

@Adrinalin4ik Can you help me

Adrinalin4ik commented 1 year ago

What does this.mapper.mapArray do?

jaydev-jainam commented 1 year ago

This will mapped entity into dto type

@Adrinalin4ik

Adrinalin4ik commented 1 year ago

Can you show me what the output of your mapper

Adrinalin4ik commented 1 year ago

Another question, what is your timezone and available time? Maybe we can sync with you and you'll show me.

jaydev-jainam commented 1 year ago

The Timezone is IST in India, and I will be available from 10 am to 5 pm according to the India timezone, We can do a google meet or zoom call?

Adrinalin4ik commented 1 year ago

Can you leave your email, I'll arrange some time

jaydev-jainam commented 1 year ago

jaydev.jadav@jainam.biz

Adrinalin4ik commented 1 year ago

@jaydev-jainam you wasn't attended to call. You didn't explain your problem properly so I can't help you.

Adrinalin4ik commented 1 year ago

@jaydev-jainam I guess the problem here with your this.mapper.mapArray. Could you provide me info about it?

Adrinalin4ik commented 1 year ago

Closed due to inactivity