question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to query for MongoDB ObjectIDs

See original GitHub issue

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);
          });
        });
      }
    }
  }
});

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:23 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
IvanGoncharovcommented, Oct 11, 2018

@alexmcmillan @sibelius @RadAcademy @GlauberF @axe-z I went ahead and merged #1520. So now you can use our npm branch as a temporary solution until we figure out how to release 14.1.0: https://github.com/graphql/graphql-js#want-to-ride-the-bleeding-edge

8reactions
IvanGoncharovcommented, Sep 11, 2018

@alexmcmillan Thanks for detail description and especially example repo 👍 It should be fixed by #1520


It would be nice if there was perhaps a clearer error message (GraphQLID cannot represent value… (instead of just ID) would have helped me a lot).

Problem is that you can define GraphQL types in SDL without working directly with GraphQL* classes:

type thing {
  id: ID
  name: String
}

So we can’t use GraphQLID in error message because it will confuse SDL users.

Perhaps adding an indication of how to properly cast a MongoDB ObjectID?

We can’t have mongoose as a dependency so we can’t detect that some object is coming from Mongo.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found