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

Projection not being added to query in relation #376

Open
CDTiernan opened this issue Apr 8, 2021 · 3 comments
Open

Projection not being added to query in relation #376

CDTiernan opened this issue Apr 8, 2021 · 3 comments

Comments

@CDTiernan
Copy link

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?

@oklas
Copy link
Contributor

oklas commented Sep 11, 2021

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
}

@nodkz nodkz transferred this issue from graphql-compose/graphql-compose Sep 11, 2021
nodkz added a commit that referenced this issue Sep 11, 2021
@nodkz
Copy link
Member

nodkz commented Sep 11, 2021

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.

brianlenz added a commit to SocialStrata/graphql-compose-mongoose that referenced this issue Apr 28, 2022
* Updated the test case for issue 376 so that it exposes at least one scenario where this error is occurring. I restructured the schema a bit and added further typings to mirror the schema I have been testing with.

* Apparently the projection is added in some simple cases with findMany(), but when using findOne() (at a minimum), it appears to not work. There are now two tests: the first one fails (with findOne(), and the second one succeeds (with findMany()).

* Updated to graphql-compose 9.0.8 to ensure testing is against the latest version.

graphql-compose#376
graphql-compose/graphql-compose#382
@brianlenz
Copy link

brianlenz commented Apr 28, 2022

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...

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

No branches or pull requests

4 participants