graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
708 stars 94 forks source link

Nested relationship with getFieldOTC and addResolver broke after migration to v9 #382

Closed sajadghawami closed 3 years ago

sajadghawami commented 3 years ago

Hey there,

so i migrated to v9 and everything works fine expect the nested relationship created with getFieldOTC and addResolver:

VendorTC.getFieldOTC('shop').addRelation('shopAddress', {
  resolver: () => ShopAddressTC.mongooseResolvers.findById(),
  prepareArgs: {
    _id: (source: IShop) => source.shopAddress,
  },
  projection: { shopAddress: true },
});

When querying, shopAddress will be null.

This is the code from an earlier version that definately worked:

VendorTC.getFieldOTC('shop').addRelation('shopAddress', {
  resolver: () => ShopAddressTC.getResolver('findById'),
  prepareArgs: {
    _id: (source: IShop) => source.shopAddress,
  },
});

As you can see, i changed to the new mongooseResolvers and added the projection that was missing from before (still worked without the projection).

At some other point i have the following:

ShopTC.addRelation('shopAddress', {
  resolver: () => ShopAddressTC.mongooseResolvers.findById(),
  prepareArgs: {
    _id: (source: any) => source.shopAddress,
  },
});

And it seems that when i remove that part, everything works as expected.

Anyone solved this yet ?

sajadghawami commented 3 years ago

I found out that adding:

  projection: { shopAddress: true },

To both relationships will make it work. I am not sure why though, but i will just leave this here!