Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript declarations #13

Merged
merged 4 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
declare module 'apollo-datasource-mongodb' {
import { DataSource } from 'apollo-datasource'
import { Collection as MongoCollection, ObjectId } from 'mongodb'
import {
Collection as MongooseCollection,
Document,
Model as MongooseModel
} from 'mongoose'

export type Collection<T> = T extends Document
? MongooseCollection
: MongoCollection<T>

export type Model<T> = T extends Document ? MongooseModel<T> : undefined

export type ModelOrCollection<T> = T extends Document
? Model<T>
: Collection<T>

export interface Options {
ttl: number
}

export class MongoDataSource<TData, TContext = any> extends DataSource<
TContext
> {
protected context: TContext
protected collection: Collection<TData>
protected model: Model<TData>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only sometimes defined—should it be model? ?

Copy link
Collaborator Author

@9at8 9at8 Mar 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we change this to model?, then in a project which uses mongoose, devs would have to do this.model?.someMethod or this.model && this.model.someMethod.

As seen on line 14, this is going to conditionally have type MongooseModel<T> (if the class is specialized with a mongoose collection) or never if the constructor is specialized with a mongo collection)

Maybe I should change line 14 to this:

  export type Model<T> = T extends Document ? MongooseModel<T> : undefined

Copy link
Collaborator Author

@9at8 9at8 Mar 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change ^

this.model will have type:

  • MongooseModel<T> if constructor is passed a MongooseModel<T>
  • undefined if we're using a MongoCollection

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds more accurate than never? whatever you think best


constructor(modelOrCollection: ModelOrCollection<TData>)

findOneById(
id: ObjectId,
options?: Options
): Promise<TData | null | undefined>

findManyByIds(
ids: ObjectId[],
options?: Options
): Promise<(TData | null | undefined)[]>

deleteFromCacheById(id: ObjectId): void
}
}
Loading