graphql-compose-mongoose
graphql-compose-mongoose copied to clipboard
Using id as an alias for _id
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.
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.
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.
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?
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.
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).
I'm also interested in a solution for this. Hope this wasn't abandoned.
The solution is:
// 'id' field
UserTC.addFields({
id: "MongoID",
});
// remove 'id' field
UserTC.removeField("_id");
I can confirm that @ajaybhatia solution works perfectly
@ajaybhatia's solution worked perfectly for me when I wanted to allow the client to set an ID for a create resolver!