graphql-compose / graphql-compose/graphql-compose-mongoose
Projection not being added to query in relation
- Dominant language
- TypeScript
- Stars
- 706
- Forks
- 98
- PR merge metrics
- No merged PRs in 30d
Description
I have a schema with users that are part of an organization:
```js
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'
}
}
);
```
```js
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
```js
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?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.