Skip to content

addRelation: projection not working as expected (with Federation) #370

Closed
@ddezoysa

Description

@ddezoysa

Hi,

I have the following relationship added to UserTC to fetch user's organizations. User schema has the _organizationIds field with list of organization ids.

export const UserSchema = new mongoose.Schema(
  {
    firstName: {
      type: String,
      required: true,
    },
    lastName: {
      type: String,
      required: true,
    },
    _organizationIds: [
      {
        type: Schema.Types.ObjectId,
        ref: 'Organization',
        index: true,
      },
    ],
  },
  { timestamps: true }
);

...

  UserTC.addRelation('organizations', {
    resolver: () => TCMap.OrganizationTC.getResolver('findByIds'),
    prepareArgs: {
      _ids: (source: IUser) => source._organizationIds,
      skip: null,
      sort: null,
    },
    projection: { _organizationIds: 1 },    <---
  });

Execution 01

When querying userFindMany with _organizationIds as a return fields,

QUERY:

query Query {
  userFindMany {
    id
    firstName
    lastName
    _organizationIds   <---
    organizations {
      name
    }
  }
}

RESPONSE:

{
  "data": {
    "userFindMany": [
      {
        "id": "611bdc46155bb03f6e39ef80",
        "firstName": "Dinuth",
        "lastName": "De Zoysa",
        "_organizationIds": [    <---
          "611e438239c2b7926d537358"
        ],
        "organizations": [    <---
          {
            "name": "MyOrg"
          }
        ]
      }
    ]
  }
}

MONGOOSE LOGS:

Mongoose: users.find({}, { limit: 100, projection: { id: true, firstName: true, lastName: true, _organizationIds: true, 'organizations.name': true }})
Mongoose: organizations.find({ _id: { '$in': [ ObjectId("611e438239c2b7926d537358") ] } }, { limit: 100, projection: { name: true } })

_organizationIds: true projection can be found in the query.

Execution 02

When querying userFindMany without _organizationIds as a return fields,

QUERY:

query Query {
  userFindMany {
    id
    firstName
    lastName
    organizations {
      name
    }
  }
}

RESPONSE:

{
  "data": {
    "userFindMany": [
      {
        "id": "611bdc46155bb03f6e39ef80",
        "firstName": "Dinuth",
        "lastName": "De Zoysa",
        "organizations": []     <---
      }
    ]
  }
}

MONGOOSE LOGS:

Mongoose: users.find({}, { limit: 100, projection: { id: true, firstName: true, lastName: true, 'organizations.name': true }})

_organizationIds: true projection can not be found in the query and organizations are not getting loaded.

But projection is set to for the required field _organizationIds in the relationship.

projection: { _organizationIds: 1 }, 

Expected behaviour is to fetch _organizationIds automatically obeying to the defined projection.

Please help to resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions