Skip to content

Commit 7273151

Browse files
committed
merge
2 parents 171517d + cff6f64 commit 7273151

14 files changed

Lines changed: 20058 additions & 3910 deletions

.eslintrc.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{
3+
"root": true,
4+
"parser": "@typescript-eslint/parser",
5+
"plugins": ["@typescript-eslint"],
6+
"env": {
7+
"es6": true,
8+
"node": true,
9+
"mocha": true
10+
},
11+
"extends": [
12+
"eslint:recommended",
13+
"plugin:@typescript-eslint/eslint-recommended",
14+
"plugin:@typescript-eslint/recommended"
15+
],
16+
"parserOptions": {
17+
"ecmaVersion": 2018,
18+
"sourceType": "module"
19+
},
20+
"rules": {
21+
"no-console": "off",
22+
"linebreak-style": "off",
23+
"quotes": [
24+
"error",
25+
"double",
26+
{ "allowTemplateLiterals": true }
27+
],
28+
"keyword-spacing": ["error", { "before": true }],
29+
"space-before-blocks": ["error"],
30+
"@typescript-eslint/explicit-module-boundary-types": "off"
31+
}
32+
}

.github/workflows/npm-publish.yml

Lines changed: 0 additions & 50 deletions
This file was deleted.

.github/workflows/publish-github.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ on:
33
push:
44
braches:
55
- main
6+
pull_request:
7+
branches:
8+
- main
69

710
jobs:
811
build:
@@ -17,9 +20,13 @@ jobs:
1720
# scope: '@octocat'
1821
- run: npm install
1922
- run: npm run build
23+
- run: npm run test
24+
- run: npm run lint
25+
- run: npm run coverage
2026

2127
publish-npm:
2228
needs: build
29+
if: github.ref == 'refs/heads/main'
2330
runs-on: ubuntu-latest
2431
steps:
2532
- uses: actions/checkout@v2
@@ -35,6 +42,7 @@ jobs:
3542

3643
publish-gpr:
3744
needs: build
45+
if: github.ref == 'refs/heads/main'
3846
runs-on: ubuntu-latest
3947
steps:
4048
- uses: actions/checkout@v2

.nycrc.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"cache": false,
3+
"check-coverage": false,
4+
"extension": [
5+
".ts"
6+
],
7+
"include": [
8+
"**/src/*.ts"
9+
],
10+
"reporter": [
11+
"cobertura",
12+
"text-summary"
13+
],
14+
"statements": 90,
15+
"branches": 90,
16+
"functions": 90,
17+
"lines": 90
18+
}

README.md

Lines changed: 20 additions & 5 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> {
@@ -76,7 +88,11 @@ This DataSource has some built in mutation methods to create, update and delete
7688
```typescript
7789
await context.dataSources.users.createOne(userDoc);
7890

79-
await context.dataSources.uploads.deleteOne(uploadKey);
91+
await context.dataSources.users.updateOne(userDoc);
92+
93+
await context.dataSources.users.updateOnePartial(user_id, { name: "Bob" });
94+
95+
await context.dataSources.users.deleteOne(userId);
8096
```
8197

8298
The data loader (and cache, if used) are updated after mutation operations.
@@ -91,12 +107,11 @@ Caching is available on an opt-in basis by passing a `ttl` option on queries.
91107

92108
## Typescript
93109

94-
This library is written in Typescript and exports full type definitions, but usable in pure Javascript as well.
110+
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).
95111

96112
# API
97113

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

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

0 commit comments

Comments
 (0)