graphql-js: Unable to query for MongoDB ObjectIDs
I 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);
});
});
}
}
}
});
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 7
- Comments: 23 (4 by maintainers)
Commits related to this issue
- Call 'toJSON' if present for ID and String serialize Fixes #1518 — committed to IvanGoncharov/graphql-js by IvanGoncharov 6 years ago
- Call 'toJSON' if present for ID and String serialize Fixes #1518 — committed to IvanGoncharov/graphql-js by IvanGoncharov 6 years ago
- Call 'toJSON' if present for ID and String serialize Fixes #1518 — committed to IvanGoncharov/graphql-js by IvanGoncharov 6 years ago
- Call 'toJSON' if present for ID and String serialize (#1520) Fixes #1518 — committed to graphql/graphql-js by IvanGoncharov 6 years ago
- feat(objectid): add valueOf to ObjectId prototype fix #7299 fix graphql/graphql-js#1518 this make interop of ObjectId and GraphQL easier — committed to sibelius/mongoose by sibelius 6 years ago
@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.I had the same issue, i resolved this issue by writing this code in the types definition file of graphql
@RadAcademy @GlauberF We plan to include it into the next release. However since instead of depending on
toStringwe now usingtoJSONit can’t be a patch release. So we are working on14.1.0that will include this and a few other features. We will try to release RC ASAP.I’ve made a field resolver to help on this
so if you want to expose an _id in your GraphQL, you need a custom type or transform ObjectId to string
@sibelius not a bad idea. Opened up a mongoose issue to track this
any changes this gets into 14.0.3?