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

Missing fields from projection in addRelation #377

Open trollcus opened 2 years ago

trollcus commented 2 years ago

Hi!

I'm having issues with the addRelation function. More specifically when projecting fields towards the graphql request. Worth noting is that this worked with the use of composeWithMongoose function previously.

Setup:

graphql-compose: "^9.0.3" graphql-compose-mongoose: "^9.6.0" mongoose: "5.13.8"

Code:

export const PrevisitSchema = new Schema(
  {
    project_id: { type: Schema.Types.ObjectId, required: true },
// ...
  },
  {
    collection: 'previsits',
  }
)

export const Previsit = mongoose.model('Previsit', PrevisitSchema)
export const PrevisitTC = composeMongoose(Previsit)

PrevisitTC.addRelation('project', {
  resolver: () => ProjectTC.mongooseResolvers.dataLoader({ lean: true }),
  prepareArgs: {
    _id: source => source.project_id || null,
  },
  projection: { project_id: 1 },
})

To further clarify.

query {
  previsitMany {
    project_id
    project {
      _id
    }
  }
}

Works while:

query {
  previsitMany {
    project {
      _id
    }
  }
}

does not

All that I get from source is the field _id and it all results in that the project field is null. Any other field I query is undefined. Any ideas to why?

nodkz commented 2 years ago

@trollcus please check test case https://github.com/graphql-compose/graphql-compose-mongoose/commit/2c334fe7a70251735a744fcdc5b494d5848b7304

In my case, projection works as you expected. Maybe I missing something. Feel free to modify this test file by providing broken behavior in new PR. According to it, I'll try to figure out what the hell is happening because the similar issue reported in #376

trollcus commented 2 years ago

@nodkz Thanks for the assist.

I tried mirroring the exact same setup as in the test with no success. Have also tried mirror your server setup and mongoose config and that did not solve it either.

Is there any way I can assist you in solving/finding the issue? I'll be more than happy to provide you with more details if you need it.

nodkz commented 2 years ago

In such case only repro repo can help. Can you please create a public repo with your setup?

trollcus commented 2 years ago

Pretty big repo but manage to crystallise the stuff so this is something that you should be able to test with. https://github.com/trollcus/gm_test

Do a classic yarn install + yarn dev and you should be good to go @nodkz

Thanks again

nodkz commented 2 years ago

@trollcus thnx for repo. It was a very interesting investigation.

In your repo, you are using graphql-middleware which recreates under the hood GraphQL types and removes the custom projection field added to fields when you are creating relations.

Hopefully, it's easy to fix in your code moving projection into extensions:

PrevisitTC.addRelation('project', {
  resolver: () => ProjectTC.mongooseResolvers.dataLoader({ lean: true }),
  // resolver: () => ProjectTC.mongooseResolvers.dataLoader({ lean: true }),
  prepareArgs: {
    _id: source => {
      return source.project_id || null
    },
  },
-  projection: { project_id: true },
+  extensions: {
+    projection: { project_id: true },
+  },
});

This is why my tests were working correctly, but not working in your application.

nodkz commented 2 years ago

Please don't close this issue. I'll publish a fix in the near future which will allow using projection without wrapping it in extensions.

trollcus commented 2 years ago

@nodkz BIG big thanks for the help. It worked when moving into extensions. Your work on these libraries is amazing.

tatejones commented 2 years ago

I had the same break when DiscriminatorTypeComposer had an addRelation that require a projection to provide an _id to a findById.

BudgetModelModelTC.addRelation("package", {
    resolver: () => PackageModelTC.getResolver("findById"),
    prepareArgs: {
        _id: (source) => source.package,
    },
    // see fixMissingProjections
    // https://github.com/graphql-compose/graphql-compose-mongoose/issues/377
    extensions: {
        projection: { package: true },
    },
})
susyabashti commented 1 year ago

I had the same break when DiscriminatorTypeComposer had an addRelation that require a projection to provide an _id to a findById.

BudgetModelModelTC.addRelation("package", { resolver: () => PackageModelTC.getResolver("findById"), prepareArgs: { _id: (source) => source.package, }, // see fixMissingProjections // https://github.com/graphql-compose/graphql-compose-mongoose/issues/377 extensions: { projection: { package: true }, }, })

I'm also using discriminators and this doesn't work for me, any updates on this?

susyabashti commented 1 year ago

susyabashti

??

danimayfield commented 3 months ago

I am not using graphql-middleware however I am using discriminators and I must use extensions in order for projections to work as well. Wondering if there's been any movement on this?

danimayfield commented 3 months ago

I have a discriminator called BProduct which is a discriminator off the base of AProduct. And this discriminator has a property I'm trying to add a relation to:

BProductTC.getFieldOTC("metadata").addRelation(
  "amenities",
  {
    resolver: () => AmenityTC.mongooseResolvers.findByIds(),
    prepareArgs: {
      _ids: (source: any) => source.amenities ?? [],
      sort: null,
      limit: null,
    },
    extensions: { projection: { amenities: true } }, <-- This doesn't work
  }
);

However the projection on this discriminator (BProduct) doesn't work at all regardless of if I use extensions or not.

The below relation on the base model of AProduct works perfectly with the use of extension though:


AProductTC.addRelation("tags", {
  resolver: () => TagTC.mongooseResolvers.findByIds()
  prepareArgs: {
    _ids: (source) => source.tags,
    sort: null,
    limit: null,
  },
  extensions: { projection: { tags: true } }, <-- works as projection should
});

Does anyone have a solution for adding relations on discriminator properties?