Search code examples
mongoosegraphqlnestjstypegoose

Using Typegoose with GraphQL


I am trying to use typegoose with graphql. I have a list of nested object within another object. I have defined the classes as follows

@ObjectType()
export class ChildClass {
  @prop()
  @Field({ nullable: true })
  someField: string;

  @prop()
  @Field({ nullable: true })
  anotherField: string;
}

@ObjectType()
@modelOptions()
export class ParentClass extends Typegoose {
  @prop()
  @Field(() => String)
  someField: string;

  @prop()
  @Field(() => [ChildClass], { nullable: 'itemsAndList' })
  children: ChildClass[];
}

When i call the following query

query {
  parentClass {
    someField
    children {
      someField
      anotherField
    }
  }
}

it always gives me null for the children's someField and anotherField. However, if i use mongoose instead of typegoose, it works correctly. MongoDb is returning fine while using typegoose. Its the conversion to graphql thats going wonky.

What am i missing here?


Solution

  • After tinkering around a bit, finally got it why this was happening. Updating the model to be like

      @prop( { type: () => [ChildClass] } )
      @Field(() => [ChildClass], { nullable: 'itemsAndList' })
      children: ChildClass[];
    

    does the magic. As per the documentation