graphql-compose-mongoose
graphql-compose-mongoose copied to clipboard
Projection not being added to query in relation
I have a schema with users that are part of an organization:
export const UserSchema = new Schema(
{
name: {
type: String,
required: true,
trim: true
},
...
orgId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Org",
required: true,
set: function (newOrgId) {
// temporarily store previous org so
// assignment to new org will work.
this._prevOrg = this.orgId
return newOrgId
}
}
},
{
collection: 'users',
},
{
timestamps: {
createdAt: 'created',
updatedAt: 'modified'
}
}
);
export const OrgSchema = mongoose.Schema(
{
name: {
type: String,
required: true,
trim: true,
unique: true,
},
Users: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}]
},
{
collection: 'orgs',
},
{
timestamps: {
createdAt: 'created',
updatedAt: 'modified'
}
}
);
I am trying to set up a relation between the User and Org as such
UserTC.addRelation('org', {
resolver: () => OrgTC.getResolver('findById'),
prepareArgs: { // Define the args passed to the resolver (eg what the _id value should be)
// Source is the filter passed to the user query
_id: (source) => {
console.log(source)
return source.orgId
}
},
projection: { "orgId": true }, // Additional fields from UserSchema we need to pass to the Org resolver
})
However, the "orgId" projection is not added to the query and source
does not contain orgId
when I make queries-- thus returning null
for the org.
This is not an issue if I explicitly query for "orgId", but my understanding of the projection parameter is that it should be used for this exact reason-- so I don't need to explicitly query for "orgId".
What am I doing wrong?
Just to clarify,
so when your query is like this:
query UserById($_id: MongoID!) {
userById(_id: $_id) {
orgId
org {
name
}
}
}
the response is like this:
{
orgId: '123...',
org: {
name: 'theX',
}
}
but when query is
query UserById($_id: MongoID!) {
userById(_id: $_id) {
org {
name
}
}
}
the response is
{
org: null
}
❓
You may check this test suite with your code and it works perfectly: https://github.com/graphql-compose/graphql-compose-mongoose/blob/5a4a9f784c939cb4bb81ec90f8e01eb21a37888f/src/tests/github_issues/376-test.ts
Just was made small changes according to v9.0.0:
UserTC.addRelation('org', {
- resolver: () => OrgTC.getResolver('findById'),
+ resolver: () => OrgTC.mongoooseResolvers.findById(),
Feel free to change this test in the new Pull Request if you want to provide a broken case.
I was running into similar issues here. I started playing around with the tests (which were working just fine) and adapted the test here for a scenario that is not working for me. I don't know if this is the only broken scenario (or if it matches the original broken scenario here), but at least it reveals the bug to be fixed.
I created PR #403 with an updated test that shows a scenario where the projection
is not being applied properly.
I've isolated the test by running this command:
yarn jest --testMatch **/376-test.ts
@nodkz, let me know if this is helpful or if you need any clarification...