@@ -2,6 +2,7 @@ import { Container, Operation } from "@azure/cosmos";
22import { KeyValueCache } from "apollo-server-caching" ;
33import DataLoader from "dataloader" ;
44import { EJSON } from "bson" ;
5+ import { CosmosDataSourceOptions } from "./datasource" ;
56
67// https://github.com/graphql/dataloader#batch-function
78const orderDocs = < V > ( ids : readonly string [ ] ) => (
@@ -33,33 +34,53 @@ const orderDocs = <V>(ids: readonly string[]) => (
3334export interface createCatchingMethodArgs {
3435 container : Container ;
3536 cache : KeyValueCache ;
37+ options : CosmosDataSourceOptions ;
3638}
3739
3840export interface FindArgs {
3941 ttl ?: number ;
4042}
4143
4244export interface CachedMethods < DType > {
43- findOneById : ( id : string , args : FindArgs ) => Promise < DType | undefined > ;
45+ findOneById : ( id : string , args ? : FindArgs ) => Promise < DType | undefined > ;
4446 findManyByIds : (
4547 ids : string [ ] ,
46- args : FindArgs
48+ args ? : FindArgs
4749 ) => Promise < ( DType | undefined ) [ ] > ;
4850 deleteFromCacheById : ( id : string ) => Promise < void > ;
4951}
5052
5153export const createCachingMethods = < DType > ( {
5254 container,
5355 cache,
56+ options,
5457} : createCatchingMethodArgs ) : CachedMethods < DType > => {
5558 const loader = new DataLoader < string , DType > ( async ( ids ) => {
56- const operations = ids . map < Operation > ( ( id ) => ( {
57- operationType : "Read" ,
58- id,
59- } ) ) ;
60- const response = await container . items . bulk ( operations ) ;
61- const responseDocs = response . map ( ( r ) =>
62- r . resourceBody ? ( ( r . resourceBody as unknown ) as DType ) : undefined
59+ options ?. logger ?. debug (
60+ `CosmosDataSource/DataLoader: loading for IDs: ${ ids } `
61+ ) ;
62+ // const operations = ids.map<Operation>((id) => ({
63+ // operationType: "Read",
64+ // id,
65+ // }));
66+ // const response = await container.items.bulk(operations);
67+ const querySpec = {
68+ query : `select * from c where c.id in (${ ids
69+ . map ( ( id ) => `'${ id } '` )
70+ . join ( "," ) } )`,
71+ // query: "select * from c where c.id in (@ids)",
72+ // parameters: [{ name: "@ids", value: ids }],
73+ } ;
74+ const response = await container . items . query ( querySpec ) . fetchAll ( ) ;
75+
76+ options ?. logger ?. debug (
77+ `CosmosDataSource/DataLoader: response count: ${ response . resources . length } `
78+ ) ;
79+ options ?. logger ?. debug (
80+ `data: ${ JSON . stringify ( response . resources [ 0 ] , null , " " ) } `
81+ ) ;
82+ const responseDocs = response . resources . map ( ( r ) =>
83+ r ? ( ( r as unknown ) as DType ) : undefined
6384 ) ;
6485 return orderDocs < DType > ( ids ) ( responseDocs ) ;
6586 } ) ;
@@ -68,6 +89,7 @@ export const createCachingMethods = <DType>({
6889
6990 const methods : CachedMethods < DType > = {
7091 findOneById : async ( id , { ttl } = { } ) => {
92+ options ?. logger ?. debug ( `CosmosDataSource: Running query for ID ${ id } ` ) ;
7193 const key = cachePrefix + id ;
7294
7395 const cacheDoc = await cache . get ( key ) ;
@@ -84,8 +106,10 @@ export const createCachingMethods = <DType>({
84106 return doc ;
85107 } ,
86108
87- findManyByIds : ( ids , args = { } ) =>
88- Promise . all ( ids . map ( ( id ) => methods . findOneById ( id , args ) ) ) ,
109+ findManyByIds : ( ids , args = { } ) => {
110+ options ?. logger ?. debug ( `CosmosDataSource: Running query for IDs ${ ids } ` ) ;
111+ return Promise . all ( ids . map ( ( id ) => methods . findOneById ( id , args ) ) ) ;
112+ } ,
89113
90114 deleteFromCacheById : async ( id ) => {
91115 loader . clear ( id ) ;
0 commit comments