Skip to content

Using String id instead of ObjectId make it crash #47

@Didier542

Description

@Didier542

Hello,

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 repo, it seems that it can handle string Id
findOneById( id: ObjectId | **string**, options?: Options ): Promise<TData | null | undefined>

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.

I don't find anywhere how I can handle this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions