graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

Using id as an alias for _id

Open corydeppen opened this issue 5 years ago • 9 comments

Prior to using graphql-compose, my schema included types with an id field courtesy of the Mongoose models. However, the types now have _id fields instead and I'm faced with having to tweak my entire application to use the new field name.

I was wondering if it's possible to configure graphql-compose to possibly alias the _id field so the types would have an id field instead.

corydeppen avatar Jun 08 '19 16:06 corydeppen

For example, you generate UserTC:

const UserTC = composeWithMongoose(UserModel);

After that, you may modify generated type with adding a new field:

UserTC.addField('id', { // set `id` name for new field
  type: 'MongoID', // set type MongoID
  resolve: (source) => source._id, // write resolve method, which returns _id value for the current field
  projection: { _id: true }, // add information, that need to reques _id field from database, when you request `id` field
});

And also you may remove unneeded field:

UserTC.removeField('_id');

The same trick may do with any output fields. Resolve methods may do any calculations or be async when you retrieve information from some data sources.

nodkz avatar Jun 08 '19 17:06 nodkz

Thanks for the quick reply - it works as I had hoped.

Are there any things I should consider before making this change across all my types?

I really appreciate all your work on the graphql-compose libraries.

corydeppen avatar Jun 08 '19 23:06 corydeppen

One thing I just noticed is that all of the related input types (e.g. FilterFindManyBreedInput, SortFindManyBreedInput) still have the _id field and not the new id field. Is it possible to modify the config so that the input types reflect the new id field?

corydeppen avatar Jun 09 '19 21:06 corydeppen

I noticed at

https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/src/resolvers/helpers/filter.js#L75-L79

That _ids and _id can't be modified, if I am not mistaken.

However, looking around the code, there's something that we can do with the opts argument but I am not sure what that entails.

hrgui avatar Jul 09 '19 16:07 hrgui

Moreover, then _id and _ids fields are used in resolve methods

_ids: https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/src/resolvers/helpers/filter.js#L106-L110

And implicit using of _id in: https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/src/resolvers/helpers/filter.js#L112-L121

So resolvers should be properly changed too.


That _ids and _id can't be modified, if I am not mistaken.

You may change what you want. Current resolvers just initial basic resolvers, then you may delete any argument. Also, you may write your own resolvers.

From graphql-compose-mongoose you may take only type generation from mongoose models. And write all resolvers from scratch.

PS. Anyway, if somebody wants to make PR with adding a config option which provides the ability to change the name for _id fields - welcome ;) Only one restriction, you should not check this option in resolve methods at runtime; you need to generate resolve function only once (at startup).

nodkz avatar Jul 10 '19 12:07 nodkz

I'm also interested in a solution for this. Hope this wasn't abandoned.

geiszla avatar Jul 30 '20 10:07 geiszla

The solution is:

// 'id' field
UserTC.addFields({
  id: "MongoID",
});

// remove 'id' field
UserTC.removeField("_id");

ajaybhatia avatar Dec 21 '20 05:12 ajaybhatia

I can confirm that @ajaybhatia solution works perfectly

mattmannion avatar Feb 06 '21 21:02 mattmannion

@ajaybhatia's solution worked perfectly for me when I wanted to allow the client to set an ID for a create resolver!

dannydi12 avatar Oct 07 '21 19:10 dannydi12