Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ddezoysa opened this issue Aug 25, 2021 · 6 comments
Closed

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

ddezoysa opened this issue Aug 25, 2021 · 6 comments
Labels

Comments

@ddezoysa
Copy link

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.

@nodkz nodkz closed this as completed in c2449a9 Aug 25, 2021
@nodkz
Copy link
Member

nodkz commented Aug 25, 2021

It's quite strange. I wrote a test and everything is working as expected.
Please see c2449a9

@ddezoysa
Copy link
Author

ddezoysa commented Aug 27, 2021

Hi @nodkz,

Thank you so much for the test. I tried your test and it works.
When I compare my code with your test, the only difference is I'm creating a sub graph for federation.

Instead of this:

const schema = schemaComposer.buildSchema();

I have this:

const typeDefs = gql(schemaComposer.toSDL({ exclude: ['Boolean', 'String', 'Int'] }));
const resolvers = schemaComposer.getResolveMethods();

const federatedSchema = buildSubgraphSchema([{ typeDefs: mergeTypeDefs([typeDefs, customTypeDefs, gql(KeycloakTypeDefs)]) }]);
const schema = addResolversToSchema({
  schema: federatedSchema,
  resolvers: mergeResolvers([resolvers, customResolvers]),
  inheritResolversFromInterfaces: true,
});

When I revert it back to buildSchema as per your test it works.

Instead of using buildSubgraphSchema method, I tried with buildFederatedSchema method as well. But issue persists.

const schema = buildFederatedSchema([
  { typeDefs: gql(KeycloakTypeDefs) },
  { typeDefs, resolvers: resolvers as any },
  { typeDefs: customTypeDefs, resolvers: customResolvers },
]);

Any suggestions on why it is not working with Federation?

@ddezoysa ddezoysa changed the title addRelation: projection not working as expected addRelation: projection not working as expected (with Federation) Aug 27, 2021
@github-actions
Copy link

🎉 This issue has been resolved in version 9.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@ddezoysa
Copy link
Author

ddezoysa commented Sep 6, 2021

Hi @nodkz,
Issue still persists in version 9.6.0 as well with federation. Do you have a test case with federation for this?

@nodkz
Copy link
Member

nodkz commented Sep 16, 2021

Maybe the federation recreates graphql types under the hood and removes projection field. Please try the following solution #377 (comment)

@weibin159
Copy link

I tried the solution #377 (comment) but issue still persists. Is there any update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants