Skip to content

Commit 56de51d

Browse files
committed
add tests, update docs
1 parent 2dfb950 commit 56de51d

7 files changed

Lines changed: 13661 additions & 3840 deletions

File tree

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ This is a CosmosDB DataSource for the Apollo GraphQL Server. It was adapted from
44

55
## Usage
66

7-
Use by creating a new class, inheriting from `CosmsosDataSource` passing in the CosmosDb container instance (created from the CosmosDB Javascript API)
7+
Use by creating a new class, inheriting from `CosmsosDataSource` passing in the CosmosDb container instance (created from the CosmosDB Javascript API). Use a separate DataSource for each data type.
8+
9+
Example:
810

911
`data-sources/Users.ts`
1012

1113
```typescript
14+
export interface UserDoc {
15+
id: string; // a string id value is required for entities using this library
16+
name: string;
17+
}
18+
1219
export class UserDataSource extends CosmosDataSource<UserDoc, ApolloContext> {}
20+
export class PostDataSource extends CosmosDataSource<PostDoc, ApolloContext> {}
1321
```
1422

1523
`server.ts`
@@ -25,19 +33,23 @@ const cosmosClient = new CosmosClient({
2533
const cosmosContainer = cosmosClient.database("MyDatabase").container("Items");
2634

2735
import UserDataSource from "./data-sources/Users.js";
36+
import PostDataSource from "./data-sources/Users.js";
2837

2938
const server = new ApolloServer({
3039
typeDefs,
3140
resolvers,
3241
dataSources: () => ({
3342
users: new UserDataSource(cosmosContainer),
43+
posts: new PostDataSource(cosmosContainer),
3444
}),
3545
});
3646
```
3747

3848
## Custom Queries
3949

40-
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 direclty in the resolvers, but probably better to create wrappers that hide the query details:
50+
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 direclty in the resolvers, but probably better to create wrappers that hide the query details.
51+
52+
Creating a derived class with custom query methods, you can hide all of your query logic in the DataSource class:
4153

4254
```typescript
4355
export class UserDataSource extends CosmosDataSource<UserDoc, ApolloContext> {
@@ -91,12 +103,11 @@ Caching is available on an opt-in basis by passing a `ttl` option on queries.
91103

92104
## Typescript
93105

94-
This library is written in Typescript and exports full type definitions, but usable in pure Javascript as well.
106+
This library is written in Typescript and exports full type definitions, but usable in pure Javascript as well. This works really well with [GraphQL Codegen's typed resolvers](https://the-guild.dev/blog/better-type-safety-for-resolvers-with-graphql-codegen).
95107

96108
# API
97109

98110
```typescript
99-
100111
const thisUser = await users.findOneById(id: string, {ttl}) // => Promise<T | undefined>
101112

102113
const userPair = await users.findManyByIds([id1, id2], {ttl}) // => Promise<(T | undefined)[]>

0 commit comments

Comments
 (0)