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

chore: add doc about adding new states to context #3170

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions content/graphql/resolvers-map.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,54 @@ With the above base class defined, we can now easily create specialized types th
class PaginatedAuthor extends Paginated(Author) {}
```

#### Adding state to context

If you need to share some state, or event an instance of a class between different resolvers you can attach them to the context object and then you can access them with `@Context` decorator. To do that first we need to attach those states to the context, let's say that we need to have access to the `request`, and `response` objects in our resolver. To do so we need to configure the `context` option:

```typescript
@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
// ...
context: ({ req, res }) => {
return { req, res };
},
}),
],
})
export class AppModule {}
```

And now you can define a custom type (let's call it `graphql-context.type.ts`) for your GraphQL context like this:

```typescript
import { Response, Request } from 'express';

export interface GraphQLResolveContext {
req: Request;
res: Response;
}
```

And then in your resolver you can access it like this:

```typescript
import { GraphQLResolveContext } from 'graphql-context.type';

@Resolver(() => Author)
export class AuthorsResolver {
@Query(() => Author)
author(
@Args('id', { type: () => Int }) id: number,
@Context() context: GraphQLResolveContext,
) {
// context.req
}
}
```

> info **Hint** context object will be recreated for each request. This means that if you're attaching something like a [Dataloader](https://www.npmjs.com/package/dataloader) to your context object NestJS will instantiate a new instance for each request which is exactly what most people need when they are configuring Dataloader.

#### Schema first

As mentioned in the [previous](/graphql/quick-start) chapter, in the schema first approach we start by manually defining schema types in SDL (read [more](https://graphql.org/learn/schema/#type-language)). Consider the following SDL type definitions.
Expand Down
4 changes: 2 additions & 2 deletions content/techniques/queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ import { Job } from 'bullmq';
export class AudioConsumer extends WorkerHost {
async process(job: Job<any, any, string>): Promise<any> {
let progress = 0;
for (i = 0; i < 100; i++) {
for (let i = 0; i < 100; i++) {
await doSomething(job.data);
progress += 1;
await job.progress(progress);
await job.updateProgress(progress);
}
return {};
}
Expand Down