Skip to content

Commit 0d8a83a

Browse files
authored
feat(schema): make the context type arg optional and add docs on its use (#89)
* make the context type arg optional and add docs on its use * fix lint
1 parent c64ec3b commit 0d8a83a

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export interface UserDoc {
1616
name: string;
1717
}
1818

19-
export class UserDataSource extends CosmosDataSource<UserDoc, ApolloContext> {}
20-
export class PostDataSource extends CosmosDataSource<PostDoc, ApolloContext> {}
19+
export class UserDataSource extends CosmosDataSource<UserDoc> {}
20+
export class PostDataSource extends CosmosDataSource<PostDoc> {}
2121
```
2222

2323
`server.ts`
@@ -45,6 +45,20 @@ const server = new ApolloServer({
4545
});
4646
```
4747

48+
## Context
49+
50+
It is often useful to define a context. See [Apollo docs on context](https://www.apollographql.com/docs/apollo-server/data/context/) To make this strongly typed, there is a second type paramater on the CosmosDbDataSource:
51+
52+
```typescript
53+
interface MyQueryContext {
54+
currentUserId: string
55+
}
56+
57+
/////
58+
59+
const userDataSource extends CosmosDataSource<UserDoc, MyQueryContext> {}
60+
```
61+
4862
## Custom Queries
4963

5064
CosmosDataSource exposes a `findManyByQuery` method that accepts a ComosDB SQL query either as a string or a `SqlQuerySpec` object containing the query and a parameter collection. This can be used directly in the resolvers, but probably better to create wrappers that hide the query details.

src/datasource.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Logger } from "./helpers";
77
import { isCosmosDbContainer } from "./helpers";
88
import { createCachingMethods, CachedMethods, FindArgs } from "./cache";
99

10+
1011
export interface CosmosDataSourceOptions {
1112
logger?: Logger;
1213
}
@@ -24,7 +25,7 @@ export interface CosmosQueryDbArgs {
2425

2526
export type QueryFindArgs = FindArgs & CosmosQueryDbArgs;
2627

27-
export class CosmosDataSource<TData extends { id: string }, TContext>
28+
export class CosmosDataSource<TData extends { id: string }, TContext = null>
2829
extends DataSource<TContext>
2930
implements CachedMethods<TData> {
3031
container: Container;

tests/schema.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* eslint @typescript-eslint/no-unused-vars: "off" */
2+
3+
import { CosmosDataSource } from "../src/datasource";
4+
5+
interface UserDoc {
6+
id: string;
7+
email: string;
8+
partitionKey?: string;
9+
}
10+
11+
interface Context {
12+
something: string;
13+
}
14+
15+
// normal context specified
16+
class UserDataSource1 extends CosmosDataSource<UserDoc, Context> {}
17+
18+
// no context specified
19+
class UserDataSource2 extends CosmosDataSource<UserDoc> {}

0 commit comments

Comments
 (0)