Skip to content

Missing fields from projection in addRelation #377

Open
@trollcus

Description

@trollcus

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?

Activity

nodkz

nodkz commented on Sep 14, 2021

@nodkz
Member

@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

trollcus commented on Sep 15, 2021

@trollcus
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

nodkz commented on Sep 15, 2021

@nodkz
Member

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

trollcus

trollcus commented on Sep 15, 2021

@trollcus
Author

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

nodkz commented on Sep 16, 2021

@nodkz
Member

@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 commented on Sep 16, 2021

@nodkz
Member

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

trollcus commented on Sep 17, 2021

@trollcus
Author

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

tatejones

tatejones commented on Oct 19, 2021

@tatejones

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

susyabashti commented on Feb 28, 2023

@susyabashti

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

susyabashti commented on Apr 4, 2023

@susyabashti

susyabashti

??

danimayfield

danimayfield commented on Mar 27, 2024

@danimayfield

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

danimayfield commented on Mar 27, 2024

@danimayfield

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

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

        Participants

        @tatejones@nodkz@trollcus@susyabashti@danimayfield

        Issue actions

          Missing fields from projection in addRelation · Issue #377 · graphql-compose/graphql-compose-mongoose