You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/introduction/dependency-injection.md
+100-7Lines changed: 100 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -220,18 +220,44 @@ export const MyModule = new GraphQLModule({
220
220
221
221
## Hooks
222
222
223
-
### `OnRequest hook`
223
+
### `OnInit` hook
224
+
225
+
This hook is called once when your application is started.
226
+
227
+
Example;
228
+
229
+
```typescript
230
+
import { Injectable } from'@graphql-modules/di';
231
+
import { OnRequest } from'@graphql-modules/core';
232
+
@Injectable()
233
+
exportclassDatabaseProviderimplementsOnInit {
234
+
constructor(privatedbClient:DbClient) {}
235
+
onInit() {
236
+
this.dbClient.connect();
237
+
console.info('Database Client is connected!');
238
+
}
239
+
}
240
+
```
241
+
242
+
### `OnRequest` hook
224
243
225
244
You can get access to useful information: the top `GraphQLModule` instance, GraphQL Context, and the network session by defining this hook as a method in your class provider.
> `OnResponse` hook is called on each HTTP GraphQL request with a single `ModuleSessionInfo` parameter.
305
+
[API of `OnResponse` is available here](/docs/api/core/api-interfaces-onresponse)
306
+
[API of `ModuleSessionInfo` is available here](/docs/api/core/api-classes-modulesessioninfo)
307
+
243
308
### `OnConnect hook`
244
309
245
310
This hook is similar to `OnRequest` hook, but this is called on the initialization of WebSockets connection. It is exactly same with `OnConnect` hook that is passed to `subscriptions` in **Apollo Server**.
246
311
247
312
[You can learn more from Apollo docs.](https://www.apollographql.com/docs/graphql-subscriptions/authentication.html)
> `OnConnect` hook is called once for each WebSocket GraphQL connection.
333
+
[API of `OnConnect` is available here](/docs/api/core/api-interfaces-onconnct)
334
+
335
+
### `OnDisconnect hook`
336
+
337
+
This hook is similar to `OnResponse` hook, but this is called on the termination of WebSockets connection. It is exactly same with `OnDisconnect` hook that is passed to `subscriptions` in **Apollo Server**.
338
+
339
+
[You can learn more from Apollo docs.](https://www.apollographql.com/docs/graphql-subscriptions/authentication.html)
When a GraphQL request arrives in GraphQL-Modules, GraphQL-Modules creates a scope only for that network request. GraphQL-Modules identifies this scope by a unique object that is given in the global application context. Global application context defined in your GraphQL server or library is not the same with module's context; because every resolvers, context builders, dependency injection and all other logics like these are encapsulated.
8
+
9
+
You can decide how you want to pass this session object like in your application context building phase.
10
+
11
+
GraphQL-Modules tries to get `session` property of your global application context first, but if there is no `session` property, it takes all application context object as your network session object.
12
+
13
+
### Using in `express-graphql`
14
+
For example `express-graphql` passes `express.Request` by default as global application context;
15
+
16
+
```typescript
17
+
const MyModule =newGraphQLModule({
18
+
context(session:express.Request) {
19
+
return {
20
+
authToken: session.headers.authorization,
21
+
};
22
+
}
23
+
});
24
+
25
+
// Some express code
26
+
app.use('/graphql', graphqlHTTP({
27
+
schema: MyModule.schema
28
+
}));
29
+
```
30
+
31
+
What if we need more stuff in network session;
32
+
33
+
```typescript
34
+
interfaceMyModuleSession {
35
+
req:express.Request,
36
+
res:express.Response
37
+
}
38
+
const MyModule =newGraphQLModule({
39
+
context(session:MyModuleSession) {
40
+
res.on('finish', () => {
41
+
// Some cleanup
42
+
});
43
+
return {
44
+
authToken: session.req.headers.authorization,
45
+
};
46
+
}
47
+
});
48
+
// Some express code
49
+
app.use('/graphql', graphqlHTTP((req, res) => ({
50
+
schema: MyModule.schema,
51
+
context: { session: { req, res }, otherThingsWillBeIgnored: ... }
52
+
// or without session property
53
+
context: { req, res }
54
+
})));
55
+
```
56
+
57
+
### Using in `apollo-server`
58
+
59
+
On the other hand, `apollo-server` needs to be passed it like below;
60
+
61
+
```typescript
62
+
newApolloServer({
63
+
modules: [
64
+
MyModule
65
+
],
66
+
context: ({ req, res }) => ({ req, res }),
67
+
// or
68
+
context: ({ req, res }) => ({ session: { req, res } }),
69
+
// or
70
+
context: session=> ({ session }),
71
+
// or
72
+
context: session=>session,
73
+
})
74
+
```
75
+
76
+
### Using in another application that doesn't use GraphQL Modules on the top
77
+
78
+
If you want to use a `GraphQLModule` in a non-GraphQLModules application, you can safely pass context builder of `GraphQLModule`.
79
+
And you can use internal context of your `GraphQLModule` including **Dependency Injection**.
80
+
GraphQL-Modules internally handles `session` without the need of passing `session` specifically.
This is what `Session` means in GraphQL-Modules. You can read more about **Provider Scopes** in **Dependency Injection** and **Provider Scopes** sections of our documentation.
0 commit comments