Skip to content

Commit cf69b1f

Browse files
committed
query now returns cosmos result info
1 parent a1ba63d commit cf69b1f

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/datasource.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DataSource } from "apollo-datasource";
22
import { ApolloError } from "apollo-server-errors";
33
import { InMemoryLRUCache, KeyValueCache } from "apollo-server-caching";
4-
import { Container, SqlQuerySpec } from "@azure/cosmos";
4+
import { Container, SqlQuerySpec, FeedOptions } from "@azure/cosmos";
55
import { Logger } from "./helpers";
66

77
import { isCosmosDbContainer } from "./helpers";
@@ -16,12 +16,21 @@ const placeholderHandler = () => {
1616
throw new Error("DataSource not initialized");
1717
};
1818

19+
export interface CosmosQueryDbArgs {
20+
/** Maps to CosmosDB feed/request options for parameters like maxItemCount
21+
* See https://docs.microsoft.com/en-us/javascript/api/%40azure/cosmos/feedoptions?view=azure-node-latest
22+
*/
23+
requestOptions?: FeedOptions;
24+
}
25+
26+
export type QueryFindArgs = FindArgs & CosmosQueryDbArgs;
27+
1928
export class CosmosDataSource<TData extends { id: string }, TContext = any>
2029
extends DataSource<TContext>
2130
implements CachedMethods<TData> {
2231
container: Container;
2332
context?: TContext;
24-
private options: CosmosDataSourceOptions;
33+
options: CosmosDataSourceOptions;
2534
// these get set by the initializer but they must be defined or nullable after the constructor
2635
// runs, so we guard against using them before init
2736
findOneById: CachedMethods<TData>["findOneById"] = placeholderHandler;
@@ -30,13 +39,26 @@ export class CosmosDataSource<TData extends { id: string }, TContext = any>
3039
dataLoader: CachedMethods<TData>["dataLoader"];
3140
primeLoader: CachedMethods<TData>["primeLoader"] = placeholderHandler;
3241

33-
async findManyByQuery(query: SqlQuerySpec | string, { ttl }: FindArgs = {}) {
34-
const results = await this.container.items.query<TData>(query).fetchAll();
42+
/**
43+
* Same as findManyByQuery but returns the entire CosmosDB response which is sometimes useful
44+
* @param query
45+
* @param param1
46+
*/
47+
async findManyByQuery(
48+
query: string | SqlQuerySpec,
49+
{ ttl, requestOptions }: QueryFindArgs = {}
50+
) {
51+
this.options?.logger?.debug(
52+
`findManyByQuery: CosmosQuery: ${(query as any).query || query}`
53+
);
54+
const results = await this.container.items
55+
.query<TData>(query, requestOptions)
56+
.fetchAll();
3557
// prime these into the dataloader and maybe the cache
3658
if (this.dataLoader && results.resources) {
3759
this.primeLoader(results.resources, ttl);
3860
}
39-
return results.resources;
61+
return results;
4062
}
4163

4264
constructor(container: Container, options: CosmosDataSourceOptions = {}) {

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CosmosDataSource } from "./datasource";
1+
import { CosmosDataSource, QueryFindArgs } from "./datasource";
22
import { FindArgs } from "./cache";
33

4-
export { CosmosDataSource, FindArgs };
4+
export { CosmosDataSource, FindArgs, QueryFindArgs };

0 commit comments

Comments
 (0)