We have a MongoDB database using string ID's (no 12 bytes || 24 hex) Theses ID's were generated by MeteorJS.
We now develop a GraphQL layer with apollo server.
We want to use the apollo-datasource-mongodb to load our data but when we call the findOnebyId method we got this error :
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
at new ObjectID (/app/node_modules/bson/lib/bson/objectid.js:59:11)
at Function.ObjectID (/app/node_modules/bson/lib/bson/objectid.js:40:43)
at file:///app/connectors/legacy.js:3:21
at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
at async Loader.import (internal/modules/esm/loader.js:166:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
/app/node_modules/bson/lib/bson/objectid.js:59
It's because the module try to transform our string Id into a MongoDB ObjectId which is impossible with our current Id format.
After looking at the apollo-datasource-mongodb repo, it seems that it can handle string Id
findOneById(
id: ObjectId | string,
options?: Options
): Promise<TData | null | undefined>
Source: https://github.com/GraphQLGuide/apollo-datasource-mongodb/blob/master/index.d.ts
Our Users dataSource:
import { MongoDataSource } from 'apollo-datasource-mongodb'
export class Users extends MongoDataSource {
}
The initiation (context layer):
Users: new Users(legacy.collection('users'))
The call (model layer):
async findOneById(obj, { id }, {dataSources: { Users }}) {
return await Users.findOneById(id)
}
We don't use mongoose & typescript.
Is anyone has a idea to make it work ?