@@ -13,9 +13,38 @@ export interface ComponentContext extends Record<string, unknown> {
1313export type ContextFunction = ( ( context : Record < string , unknown > ) => any ) ;
1414export interface IDataSource {
1515 name : string ;
16+ [ key : string | symbol ] : any ;
1617}
18+ /**
19+ * Type for implementing data sources
20+ * When defining a data source class, methods should accept context as their first parameter
21+ * @example
22+ * class MyDataSource {
23+ * name = 'MyDataSource';
24+ *
25+ * // Context is required as first parameter when implementing
26+ * getData(context: ComponentContext, id: string) {
27+ * return { id };
28+ * }
29+ * }
30+ */
31+ export type DataSourceDefinition < T > = {
32+ [ P in keyof T ] : T [ P ] extends Function ? ( context : ComponentContext , ...args : any [ ] ) => any : T [ P ] ;
33+ } ;
34+ /**
35+ * Type for consuming data sources in resolvers
36+ * When using a data source method, the context is automatically injected
37+ * @example
38+ * // In a resolver:
39+ * Query: {
40+ * getData(_, { id }, context) {
41+ * // Context is automatically injected, so you don't pass it
42+ * return context.dataSources.MyDataSource.getData(id);
43+ * }
44+ * }
45+ */
1746export type DataSource < T > = {
18- [ P in keyof T ] : T [ P ] extends ( context : ComponentContext , ...p : infer P ) => infer R ? ( ...p : P ) => R : never ;
47+ [ P in keyof T ] : T [ P ] extends ( context : ComponentContext , ...p : infer P ) => infer R ? ( ...p : P ) => R : T [ P ] ;
1948} ;
2049export type DataSourceMap = {
2150 [ key : string ] : IDataSource ;
0 commit comments