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

Missing fields from projection in addRelation #377

Open
trollcus opened this issue Sep 13, 2021 · 12 comments
Open

Missing fields from projection in addRelation #377

trollcus opened this issue Sep 13, 2021 · 12 comments
Labels

Comments

@trollcus
Copy link

trollcus commented Sep 13, 2021

Hi!

I'm having issues with the addRelation function. More specifically when projecting fields towards the graphql request. Worth noting is that this worked with the use of composeWithMongoose function previously.

Setup:

graphql-compose: "^9.0.3"
graphql-compose-mongoose: "^9.6.0"
mongoose: "5.13.8"

Code:

export const PrevisitSchema = new Schema(
  {
    project_id: { type: Schema.Types.ObjectId, required: true },
// ...
  },
  {
    collection: 'previsits',
  }
)

export const Previsit = mongoose.model('Previsit', PrevisitSchema)
export const PrevisitTC = composeMongoose(Previsit)

PrevisitTC.addRelation('project', {
  resolver: () => ProjectTC.mongooseResolvers.dataLoader({ lean: true }),
  prepareArgs: {
    _id: source => source.project_id || null,
  },
  projection: { project_id: 1 },
})

To further clarify.

query {
  previsitMany {
    project_id
    project {
      _id
    }
  }
}

Works while:

query {
  previsitMany {
    project {
      _id
    }
  }
}

does not

All that I get from source is the field _id and it all results in that the project field is null. Any other field I query is undefined. Any ideas to why?

nodkz added a commit that referenced this issue Sep 14, 2021
nodkz added a commit that referenced this issue Sep 14, 2021
@nodkz
Copy link
Member

nodkz commented Sep 14, 2021

@trollcus please check test case 2c334fe

In my case, projection works as you expected. Maybe I missing something.
Feel free to modify this test file by providing broken behavior in new PR. According to it, I'll try to figure out what the hell is happening because the similar issue reported in #376

@trollcus
Copy link
Author

@nodkz Thanks for the assist.

I tried mirroring the exact same setup as in the test with no success. Have also tried mirror your server setup and mongoose config and that did not solve it either.

Is there any way I can assist you in solving/finding the issue? I'll be more than happy to provide you with more details if you need it.

@nodkz
Copy link
Member

nodkz commented Sep 15, 2021

In such case only repro repo can help. Can you please create a public repo with your setup?

@trollcus
Copy link
Author

trollcus commented Sep 15, 2021

Pretty big repo but manage to crystallise the stuff so this is something that you should be able to test with.
https://github.com/trollcus/gm_test

Do a classic yarn install + yarn dev and you should be good to go @nodkz

Thanks again

@nodkz
Copy link
Member

nodkz commented Sep 16, 2021

@trollcus thnx for repo. It was a very interesting investigation.

In your repo, you are using graphql-middleware which recreates under the hood GraphQL types and removes the custom projection field added to fields when you are creating relations.

Hopefully, it's easy to fix in your code moving projection into extensions:

PrevisitTC.addRelation('project', {
  resolver: () => ProjectTC.mongooseResolvers.dataLoader({ lean: true }),
  // resolver: () => ProjectTC.mongooseResolvers.dataLoader({ lean: true }),
  prepareArgs: {
    _id: source => {
      return source.project_id || null
    },
  },
-  projection: { project_id: true },
+  extensions: {
+    projection: { project_id: true },
+  },
});

This is why my tests were working correctly, but not working in your application.

@nodkz nodkz added the bug label Sep 16, 2021
@nodkz
Copy link
Member

nodkz commented Sep 16, 2021

Please don't close this issue. I'll publish a fix in the near future which will allow using projection without wrapping it in extensions.

@trollcus
Copy link
Author

@nodkz BIG big thanks for the help. It worked when moving into extensions. Your work on these libraries is amazing.

@tatejones
Copy link

tatejones commented Oct 19, 2021

I had the same break when DiscriminatorTypeComposer had an addRelation that require a projection to provide an _id to a findById.

BudgetModelModelTC.addRelation("package", {
    resolver: () => PackageModelTC.getResolver("findById"),
    prepareArgs: {
        _id: (source) => source.package,
    },
    // see fixMissingProjections
    // https://github.com/graphql-compose/graphql-compose-mongoose/issues/377
    extensions: {
        projection: { package: true },
    },
})

@susyabashti
Copy link

I had the same break when DiscriminatorTypeComposer had an addRelation that require a projection to provide an _id to a findById.

BudgetModelModelTC.addRelation("package", {
resolver: () => PackageModelTC.getResolver("findById"),
prepareArgs: {
_id: (source) => source.package,
},
// see fixMissingProjections
// #377
extensions: {
projection: { package: true },
},
})

I'm also using discriminators and this doesn't work for me, any updates on this?

@susyabashti
Copy link

susyabashti

??

@danimayfield
Copy link

I am not using graphql-middleware however I am using discriminators and I must use extensions in order for projections to work as well. Wondering if there's been any movement on this?

@danimayfield
Copy link

danimayfield commented Mar 27, 2024

I have a discriminator called BProduct which is a discriminator off the base of AProduct. And this discriminator has a property I'm trying to add a relation to:

BProductTC.getFieldOTC("metadata").addRelation(
  "amenities",
  {
    resolver: () => AmenityTC.mongooseResolvers.findByIds(),
    prepareArgs: {
      _ids: (source: any) => source.amenities ?? [],
      sort: null,
      limit: null,
    },
    extensions: { projection: { amenities: true } }, <-- This doesn't work
  }
);

However the projection on this discriminator (BProduct) doesn't work at all regardless of if I use extensions or not.

The below relation on the base model of AProduct works perfectly with the use of extension though:

AProductTC.addRelation("tags", {
  resolver: () => TagTC.mongooseResolvers.findByIds()
  prepareArgs: {
    _ids: (source) => source.tags,
    sort: null,
    limit: null,
  },
  extensions: { projection: { tags: true } }, <-- works as projection should
});

Does anyone have a solution for adding relations on discriminator properties?

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

5 participants