Skip to content

Projection not being added to query in relation #376

Open
@CDTiernan

Description

@CDTiernan

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions