-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.ts
28 lines (26 loc) · 924 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
export type Key = string | number
export interface Datasource<T extends {}> {
getMany?(ids: Key[], ctx: any): Promise<Array<T>> | Array<T>
}
/**
* A datasource is a way to fetch data from a remote source. It can be a REST API, a database, an
* external third party service, etc. All that's needed is to extend the class and pass it to a node.
*
* @example
* ```ts
* class RestDatasource extends Datasource<T> {
* getMany(ids: Key[], ctx: any): Promise<Array<T>> | Array<T> {
* // getMany is optional, however using it has a performance benefit.
* // a node will be able to resolve all the ids in a single request rather
* // than n (being the amount of ids) requests.
* }
*
* getOne(id: Key, ctx: any): Promise<T> | T {
* return fetch().then(x => x.json())
* }
* }
* ```
*/
export abstract class Datasource<T extends {}> {
abstract getOne(id: Key, ctx: any): Promise<T> | T
}