Home > Software design >  Typescript mismatch between mongodb <Collection> and apollo-datasources-mongodb <Collection
Typescript mismatch between mongodb <Collection> and apollo-datasources-mongodb <Collection

Time:08-20

The typescript demo for apollo-datasources-mongodb no longer works. It generates the same error that my own code is generating:

--- ERROR ---

Argument of type 'import("C:/Users/user/Code/development/server/node_modules/mongodb/mongodb").Collection<import("C:/Users/user/Code/development/server/node_modules/bson/bson").Document>' is not assignable to parameter of type 'import("C:/Users/user/Code/development/server/node_modules/apollo-datasource-mongodb/node_modules/@types/mongodb/index").Collection<import("C:/Users/user/Code/development/server/node_modules/bson/bson").Document>'. Type 'Collection' is missing the following properties from type 'Collection': geoHaystackSearch, group, parallelCollectionScan, reIndex, savet

--- APOLLO SERVER ---

const server : ApolloServer = new ApolloServer({ schema : authorizedSchema
        , dataSources: () => ({  
            users: new Test( mongoClient.db("myapp").collection("users") ) // intellisense highlights the input to new Test as the error
        })
        , context: async ( { req } ) => { await verifyAccessToken( req ) }
        , csrfPrevention: true
    });

--- DEPENDENCIES ---

"dependencies": {
    "@apollo/client": "^3.6.9",
    "@azure/msal-common": "^7.1.0",
    "@graphql-tools/schema": "^8.5.0",
    "@graphql-tools/utils": "^8.8.0",
    "@types/express": "github:types/express",
    "@types/jsonwebtoken": "^8.5.8",
    "@types/node": "^18.0.3",
    "apollo-datasource-mongodb": "^0.5.4",
    "apollo-server": "^3.9.0",
    "apollo-server-core": "^3.9.0",
    "cookie-parser": "^1.4.6",
    "debug": "^4.3.4",
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "express-jwt": "^7.7.5",
    "graphql": "^15.8.0",
    "graphql-tools": "^8.3.0",
    "http-errors": "^2.0.0",
    "jsonwebtoken": "^8.5.1",
    "jwks-rsa": "^2.1.4",
    "jws": "^4.0.0",
    "mongodb": "^4.8.1",
    "openid-client": "^5.1.8",
    "type-graphql": "^1.1.1",
    "typescript": "^4.7.4"
  },

It seems that there is a mismatch between the <Collection> from MongoClient and the <Collection> in apollo-datasources-mongodb.

That is the mongodb <Collection> not fitting with the apollo-datasource-mongodb <Collection> type, right?

Am I missing something?

EDIT:

I tried extending the MongoDB Collection with the missing properties set to optional. That got past the error about those properties to the mongodb types and now I get another error that a different property is wrong.

enter image description here

CodePudding user response:

Please try updating the apollo server packages in your dependencies to the latest version.

"apollo-server": "^3.10.1",
"apollo-server-core": "^3.10.1"

CodePudding user response:

The newest versions of MongoDB has built in types, but apollo-datasources-mongodb is using the following types file:

"@types/mongodb": "^3.5.27"

By drawing the Collection type from this location the error was resolved.

  • Related