In GraphQL, a context is an object shared by all the resolvers of a specific execution. It's useful for keeping data such as authentication info, the current user, database connection, data sources and other things you need for running your business logic.
The context is available as the 3rd argument to each resolver:
GraphQL Modules follow the same approach, so context is shared across modules. That's why there's no API for context building in GraphQL Modules, it's managed by GraphQL server implementation.
Shaping Context type
GraphQL Modules expose a global namespace called
GraphQLModules, so there's no need to pass the same signature over and over again as part of generic types of different APIs.
Context is global and shared across modules and application which means you can define it once and it applies automatically everywhere.
GraphQLModules.GlobalContext like this:
Now every piece of GraphQL Modules understands the context and you gain much stronger type-safety.
Using Context type
GlobalContext you need to define and also
Context that you need to consume.
Context contains provided
GlobalContext internally and includes also
GlobalContexttype is only to define the context, do not use it anywhere.
GraphQLModules.Context is simple and because it's a globally available type, you just use it, there's no need to import it from
Context in Dependency Injection
To get familiar with Dependency Injection, please read the "Dependency Injection - Introduction" chapter.
The Context can be accessed directly in resolve function or within Dependency Injection using