Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schema): make the context type arg optional and add docs on its use #89

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export interface UserDoc {
name: string;
}

export class UserDataSource extends CosmosDataSource<UserDoc, ApolloContext> {}
export class PostDataSource extends CosmosDataSource<PostDoc, ApolloContext> {}
export class UserDataSource extends CosmosDataSource<UserDoc> {}
export class PostDataSource extends CosmosDataSource<PostDoc> {}
```

`server.ts`
Expand Down Expand Up @@ -45,6 +45,20 @@ const server = new ApolloServer({
});
```

## Context

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:

```typescript
interface MyQueryContext {
currentUserId: string
}

/////

const userDataSource extends CosmosDataSource<UserDoc, MyQueryContext> {}
```

## Custom Queries

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.
Expand Down
3 changes: 2 additions & 1 deletion src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Logger } from "./helpers";
import { isCosmosDbContainer } from "./helpers";
import { createCachingMethods, CachedMethods, FindArgs } from "./cache";


export interface CosmosDataSourceOptions {
logger?: Logger;
}
Expand All @@ -24,7 +25,7 @@ export interface CosmosQueryDbArgs {

export type QueryFindArgs = FindArgs & CosmosQueryDbArgs;

export class CosmosDataSource<TData extends { id: string }, TContext>
export class CosmosDataSource<TData extends { id: string }, TContext = null>
extends DataSource<TContext>
implements CachedMethods<TData> {
container: Container;
Expand Down
19 changes: 19 additions & 0 deletions tests/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint @typescript-eslint/no-unused-vars: "off" */

import { CosmosDataSource } from "../src/datasource";

interface UserDoc {
id: string;
email: string;
partitionKey?: string;
}

interface Context {
something: string;
}

// normal context specified
class UserDataSource1 extends CosmosDataSource<UserDoc, Context> {}

// no context specified
class UserDataSource2 extends CosmosDataSource<UserDoc> {}
Loading