|
24 | 24 | configuration?: SubschemaConfig; |
25 | 25 | } |
26 | 26 |
|
27 | | - export type GlobalContext = { [key: string]: unknown }; |
| 27 | + export interface ComponentContext extends Record<string, unknown> { |
| 28 | + dataSources: DataSourceMap; |
| 29 | + } |
28 | 30 |
|
29 | | - export type ContextFunction = ((ctx: GlobalContext) => any); |
| 31 | + export type ContextFunction = ((context: Record<string, unknown>) => any); |
30 | 32 |
|
31 | 33 | export interface IDataSource { |
32 | 34 | name: string |
33 | 35 | } |
34 | 36 |
|
35 | 37 | export type DataSource<T> = { |
36 | | - [P in keyof T]: T[P] extends (ctx: GlobalContext, ...p: infer P) => infer R ? (...p: P) => R : never |
| 38 | + [P in keyof T]: T[P] extends (context: ComponentContext, ...p: infer P) => infer R ? (...p: P) => R : never |
37 | 39 | } |
38 | 40 |
|
39 | 41 | export type DataSourceMap = {[key: string]: IDataSource}; |
40 | 42 |
|
41 | | - export type DataSourceInjectionFunction = ((ctx: GlobalContext) => DataSourceMap); |
| 43 | + export type DataSourceInjectionFunction = ((context: Record<string, unknown>) => DataSourceMap); |
42 | 44 |
|
43 | 45 | export interface IContextConfig { |
44 | 46 | namespace: string; |
|
49 | 51 | use: (name: string|ContextFunction|null, fn?: ContextFunction|string) => void; |
50 | 52 | } |
51 | 53 |
|
52 | | - export interface IGraphQLComponentOptions { |
| 54 | + export interface IGraphQLComponentOptions<TContextType extends ComponentContext = ComponentContext> { |
53 | 55 | types?: TypeSource |
54 | | - resolvers?: IResolvers; |
| 56 | + resolvers?: IResolvers<any, TContextType>; |
55 | 57 | mocks?: boolean | IMocks; |
56 | 58 | imports?: (IGraphQLComponent | IGraphQLComponentConfigObject)[]; |
57 | 59 | context?: IContextConfig; |
|
63 | 65 | transforms?: SchemaMapper[] |
64 | 66 | } |
65 | 67 |
|
66 | | - export interface IGraphQLComponent { |
| 68 | + export interface IGraphQLComponent<TContextType extends ComponentContext = ComponentContext> { |
67 | 69 | readonly name: string; |
68 | 70 | readonly schema: GraphQLSchema; |
69 | 71 | readonly context: IContextWrapper; |
70 | 72 | readonly types: TypeSource; |
71 | | - readonly resolvers: IResolvers; |
| 73 | + readonly resolvers: IResolvers<any, TContextType>; |
72 | 74 | readonly imports?: (IGraphQLComponent | IGraphQLComponentConfigObject)[]; |
73 | 75 | readonly dataSources?: IDataSource[]; |
74 | 76 | readonly dataSourceOverrides?: IDataSource[]; |
75 | 77 | federation?: boolean; |
76 | 78 | } |
77 | 79 |
|
78 | | - export default class GraphQLComponent implements IGraphQLComponent { |
| 80 | + export default class GraphQLComponent<TContextType extends ComponentContext = ComponentContext> implements IGraphQLComponent { |
79 | 81 | _schema: GraphQLSchema; |
80 | 82 | _types: TypeSource; |
81 | | - _resolvers: IResolvers<any, any>; |
| 83 | + _resolvers: IResolvers<any, TContextType>; |
82 | 84 | _mocks: boolean | IMocks; |
83 | 85 | _imports: IGraphQLComponentConfigObject[]; |
84 | 86 | _context: ContextFunction; |
|
141 | 143 | }) : []; |
142 | 144 |
|
143 | 145 |
|
144 | | - this._context = async (globalContext: GlobalContext): Promise<GlobalContext> => { |
| 146 | + this._context = async (globalContext: Record<string, unknown>): Promise<TContextType> => { |
145 | 147 | //TODO: currently the context injected into data sources won't have data sources on it |
146 | 148 | const ctx = { |
147 | 149 | dataSources: this._dataSourceContextInject(globalContext) |
|
163 | 165 | Object.assign(ctx[context.namespace], await context.factory.call(this, globalContext)); |
164 | 166 | } |
165 | 167 |
|
166 | | - return ctx; |
| 168 | + return ctx as TContextType; |
167 | 169 | }; |
168 | 170 |
|
169 | 171 | } |
170 | 172 |
|
171 | 173 | get context(): IContextWrapper { |
172 | 174 |
|
173 | | - const contextFn = async (context): Promise<GlobalContext> => { |
| 175 | + const contextFn = async (context): Promise<ComponentContext> => { |
174 | 176 | debug(`building root context`); |
175 | 177 |
|
176 | 178 | for (let { name, fn } of contextFn._middleware) { |
|
283 | 285 | return this._types; |
284 | 286 | } |
285 | 287 |
|
286 | | - get resolvers(): IResolvers { |
| 288 | + get resolvers(): IResolvers<any, TContextType> { |
287 | 289 | return this._resolvers; |
288 | 290 | } |
289 | 291 |
|
|
0 commit comments