GraphQLGuide / apollo-datasource-mongodb

Apollo data source for MongoDB
MIT License
285 stars 64 forks source link

MongoDataSource Typescript issues #19

Closed jblevins1991 closed 4 years ago

jblevins1991 commented 4 years ago

I started a Typescript project with Apollo Server and have implemented this module the way the documentation expects. I have a data source as shown below as well as my index.ts file that implements the apollo server.

import { MongoDataSource } from "apollo-datasource-mongodb";

export default class Authors extends MongoDataSource {
    getAuthorById(id: string) {
        return this.findOneById(id);
    }
};
const client = new MongoClient("super secret mongo server");
client.connect();

const server = new ApolloServer({
    typeDefs,
    resolvers,
    playground: true,
    dataSources: () => ({
        users: new Authors(client.db().collection("authors")),
        posts: new Posts(client.db().collection("posts")),
    }),
});

I get the following Typescript errors:

index.ts

This error is when I attempt to pass client.db().collection("authors") to the Authors constructor. This also happens for Posts.

author.ts

lorensr commented 4 years ago

Expected 0 arguments, but got 1. ts(2554)

Shouldn't this line mean TypeScript should expect 1 argument? https://github.com/GraphQLGuide/apollo-datasource-mongodb/blob/master/index.d.ts#L31

cc @9at8

Sidenote: findOneById takes an ObjectID

jblevins1991 commented 4 years ago

image

I wish 😂.

jblevins1991 commented 4 years ago

Could my tsconfig.json be incorrect?

jblevins1991 commented 4 years ago

This is caused because a typescript types declaration does not exist. I only found this out by hovering the import statement that is not even highlighted by vs code 😕. I thought a type declaration file was merged in.

jblevins1991 commented 4 years ago

The solution was the copy the index.d.ts file from this repo into my node_modules/apollo-datasource-mongodb directory.

9at8 commented 4 years ago

@jblevins1991 can you double-check that you have v0.2.4? :sweat_smile:

I would search for apollo-datasource-mongodb in package-lock.json or yarn.lock

jblevins1991 commented 4 years ago

Let me check my package-lock. I did however rm -rf node modules and delete the package-lock.

jblevins1991 commented 4 years ago

My comment above was also pre-mature. It still is broken lol.

jblevins1991 commented 4 years ago

    "apollo-datasource-mongodb": {
      "version": "0.2.4",
      "resolved": "https://registry.npmjs.org/apollo-datasource-mongodb/-/apollo-datasource-mongodb-0.2.4.tgz",
      "integrity": "sha512-fcdxrY7mJOHLi95QgzCsrF5kivF24sLIIZTSviobws2TjmjtYp+URMbT2uwjdnUm7gSvje+BU0k63SP3If2x+A==",
      "requires": {
        "apollo-datasource": "^0.3.1",
        "apollo-server-caching": "^0.3.1",
        "apollo-server-errors": "^2.2.1",
        "dataloader": "^1.4.0"
      }
    },

Yeah, this is definitely wrong. It doesn't have any of the index.d.ts dependencies either.

jblevins1991 commented 4 years ago

I did npm install apollo-datasource-mongodb@0.2.4. I now see the index.d.ts file. I then rm -rf'd node modules and rm package-lock.json and now do not have the index.d.ts again.

image

9at8 commented 4 years ago

Ah, I can reproduce this.

Apparently index.d.ts is not there in the package that's distributed. I'm looking into it. We should have a fix soon, and then we can release 0.2.5 for a fix.

Thanks for bringing this up @jblevins1991 !

jblevins1991 commented 4 years ago

Thank you for being so quick to reply. I can look into this over the weekend if you would like?

jblevins1991 commented 4 years ago

This is from the TypeScript web site https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html

Note that the "typings" field is synonymous with "types", and could be used as well.

Also note that if your package.json includes the "files" property the "types" property will be ignored. In that case you need to pass your main declaration file to the "files" property as well.

Also note that if your main declaration file is named index.d.ts and lives at the root of the package (next to index.js) you do not need to mark the "types" property, though it is advisable to do so

9at8 commented 4 years ago

Thanks for the help @jblevins1991 !

lorensr commented 4 years ago

Thanks @jblevins1991 for reporting and researching and @9at8 for fixing! 🤗 Fix published in 0.2.5