Open
Description
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?
Activity
test: check 377 issue that `projection` works
test: stabilize sort order in test case
nodkz commentedon Sep 14, 2021
@trollcus please check test case 2c334fe
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 commentedon Sep 15, 2021
@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 commentedon Sep 15, 2021
In such case only repro repo can help. Can you please create a public repo with your setup?
trollcus commentedon Sep 15, 2021
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 commentedon Sep 16, 2021
@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 customprojection
field added to fields when you are creating relations.Hopefully, it's easy to fix in your code moving
projection
intoextensions
:This is why my tests were working correctly, but not working in your application.
nodkz commentedon Sep 16, 2021
Please don't close this issue. I'll publish a fix in the near future which will allow using
projection
without wrapping it inextensions
.trollcus commentedon Sep 17, 2021
@nodkz BIG big thanks for the help. It worked when moving into extensions. Your work on these libraries is amazing.
tatejones commentedon Oct 19, 2021
I had the same break when DiscriminatorTypeComposer had an addRelation that require a projection to provide an _id to a findById.
susyabashti commentedon Feb 28, 2023
I'm also using discriminators and this doesn't work for me, any updates on this?
susyabashti commentedon Apr 4, 2023
??
danimayfield commentedon Mar 27, 2024
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 commentedon Mar 27, 2024
I have a discriminator called
BProduct
which is a discriminator off the base ofAProduct
. And this discriminator has a property I'm trying to add a relation to: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: