Skip to content

Commit 2cbb86a

Browse files
committed
Updated context to not only be extendable with a given type, but to by default have introspection into the dataSources field.
1 parent d017b5a commit 2cbb86a

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

examples/composition/property-component/resolvers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
export const resolvers = {
44
Query: {
5-
propertyById(_, { id }, { dataSources}) {
5+
propertyById(_, { id }, { dataSources }) {
66
return dataSources.PropertyDataSource.getPropertyById(id);
77
}
88
}

src/index.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,23 @@
2424
configuration?: SubschemaConfig;
2525
}
2626

27-
export type GlobalContext = { [key: string]: unknown };
27+
export interface ComponentContext extends Record<string, unknown> {
28+
dataSources: DataSourceMap;
29+
}
2830

29-
export type ContextFunction = ((ctx: GlobalContext) => any);
31+
export type ContextFunction = ((context: Record<string, unknown>) => any);
3032

3133
export interface IDataSource {
3234
name: string
3335
}
3436

3537
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
3739
}
3840

3941
export type DataSourceMap = {[key: string]: IDataSource};
4042

41-
export type DataSourceInjectionFunction = ((ctx: GlobalContext) => DataSourceMap);
43+
export type DataSourceInjectionFunction = ((context: Record<string, unknown>) => DataSourceMap);
4244

4345
export interface IContextConfig {
4446
namespace: string;
@@ -49,9 +51,9 @@
4951
use: (name: string|ContextFunction|null, fn?: ContextFunction|string) => void;
5052
}
5153

52-
export interface IGraphQLComponentOptions {
54+
export interface IGraphQLComponentOptions<TContextType extends ComponentContext = ComponentContext> {
5355
types?: TypeSource
54-
resolvers?: IResolvers;
56+
resolvers?: IResolvers<any, TContextType>;
5557
mocks?: boolean | IMocks;
5658
imports?: (IGraphQLComponent | IGraphQLComponentConfigObject)[];
5759
context?: IContextConfig;
@@ -63,22 +65,22 @@
6365
transforms?: SchemaMapper[]
6466
}
6567

66-
export interface IGraphQLComponent {
68+
export interface IGraphQLComponent<TContextType extends ComponentContext = ComponentContext> {
6769
readonly name: string;
6870
readonly schema: GraphQLSchema;
6971
readonly context: IContextWrapper;
7072
readonly types: TypeSource;
71-
readonly resolvers: IResolvers;
73+
readonly resolvers: IResolvers<any, TContextType>;
7274
readonly imports?: (IGraphQLComponent | IGraphQLComponentConfigObject)[];
7375
readonly dataSources?: IDataSource[];
7476
readonly dataSourceOverrides?: IDataSource[];
7577
federation?: boolean;
7678
}
7779

78-
export default class GraphQLComponent implements IGraphQLComponent {
80+
export default class GraphQLComponent<TContextType extends ComponentContext = ComponentContext> implements IGraphQLComponent {
7981
_schema: GraphQLSchema;
8082
_types: TypeSource;
81-
_resolvers: IResolvers<any, any>;
83+
_resolvers: IResolvers<any, TContextType>;
8284
_mocks: boolean | IMocks;
8385
_imports: IGraphQLComponentConfigObject[];
8486
_context: ContextFunction;
@@ -141,7 +143,7 @@
141143
}) : [];
142144

143145

144-
this._context = async (globalContext: GlobalContext): Promise<GlobalContext> => {
146+
this._context = async (globalContext: Record<string, unknown>): Promise<TContextType> => {
145147
//TODO: currently the context injected into data sources won't have data sources on it
146148
const ctx = {
147149
dataSources: this._dataSourceContextInject(globalContext)
@@ -163,14 +165,14 @@
163165
Object.assign(ctx[context.namespace], await context.factory.call(this, globalContext));
164166
}
165167

166-
return ctx;
168+
return ctx as TContextType;
167169
};
168170

169171
}
170172

171173
get context(): IContextWrapper {
172174

173-
const contextFn = async (context): Promise<GlobalContext> => {
175+
const contextFn = async (context): Promise<ComponentContext> => {
174176
debug(`building root context`);
175177

176178
for (let { name, fn } of contextFn._middleware) {
@@ -283,7 +285,7 @@
283285
return this._types;
284286
}
285287

286-
get resolvers(): IResolvers {
288+
get resolvers(): IResolvers<any, TContextType> {
287289
return this._resolvers;
288290
}
289291

0 commit comments

Comments
 (0)