Unable to query for MongoDB ObjectIDs
See original GitHub issueI recently updated from 0.13.2 to 14.0.2 which includes breaking changes.
This introduced errors with existing queries which include MongoDB Object Ids (probably from #1382):
ID cannot represent value: { _bsontype: "ObjectID", id: <Buffer 5b 96 3d bf 98 0a 04 09 85 c6 6e a1> }
Repository with complete, minimal repeatable example here:
const Thing = mongoose.model('Thing', new mongoose.Schema({
id: mongoose.Schema.Types.ObjectId,
name: String
}));
const ThingType = new GraphQLObjectType({
name: 'thing',
fields: function () {
return {
id: { type: GraphQLID },
name: { type: GraphQLString }
}
}
});
const RootMutation = new GraphQLObjectType({
name: 'CreateMutation',
fields: {
create: {
type: ThingType,
description: 'Create new thing',
args: {
name: {
name: 'Name of Thing',
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: (root, args) => {
const newThing = new Thing({ name: args.name });
newThing.id = newThing._id;
return new Promise((res, rej) => {
newThing.save(err => {
if (err) return rej(err);
res(newThing);
});
});
}
}
}
});
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:23 (4 by maintainers)
Top Results From Across the Web
Unable to search for ObjectID in MongoDB stitch service
In the case above, the _id field is an objectId, but the user_id field is a string. The schema has to match what...
Read more >Wrong query for ObjectId - Working with Data - MongoDB
Hi I'm using MongoDB driver for .Net Core 3.1 and there is a wrong query translation when I'm using lambda expression in the...
Read more >Nothing is returned on search of an existing document on the ...
One possible scenario I can think of is that maybe you have an _id field with ObjectId type and you search it as...
Read more >Aggregate $match _id $eq $toObjectId not working - MongoDB
Using Compass 1.30.1, I was testing an aggregation and getting unexpected results. A $match was not working as expected.
Read more >Querying with Object ID vs Querying with array - MongoDB
You should check if your current unique IDs are compatible with this. One advantage of having ObjectId is that you can also do...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Hashnode Post
No results found

@alexmcmillan @sibelius @RadAcademy @GlauberF @axe-z I went ahead and merged #1520. So now you can use our
npmbranch as a temporary solution until we figure out how to release14.1.0: https://github.com/graphql/graphql-js#want-to-ride-the-bleeding-edge@alexmcmillan Thanks for detail description and especially example repo 👍 It should be fixed by #1520
Problem is that you can define GraphQL types in SDL without working directly with
GraphQL*classes:So we can’t use
GraphQLIDin error message because it will confuse SDL users.We can’t have
mongooseas a dependency so we can’t detect that some object is coming from Mongo.