@@ -11,12 +11,14 @@ import { FieldNode, GraphQLResolveInfo, Kind } from 'graphql'
11
11
*
12
12
* @export
13
13
* @interface IGraphQLExtractSelectionMap
14
+ * @template T The type of the source.
15
+ * @template K The keys included of the source.
14
16
*/
15
- export interface IGraphQLExtractSelectionMap {
16
- [ k : string ] : string | ( ( parentKeys : string [ ] , fieldName : string ) => string )
17
+ export type IGraphQLExtractSelectionMap < T extends object = object , K extends keyof T = Exclude < ObjectKeys < T > , KeysMatching < T , Date > > > = {
18
+ [ k in K ] ? : string | ( ( parentKeys : string [ ] , fieldName : K ) => string )
17
19
}
18
20
19
- export interface IExtractGraphQLSelectionsParams {
21
+ interface IExtractGraphQLSelectionsParams < T extends object = object , K extends keyof T = Exclude < ObjectKeys < T > , KeysMatching < T , Date > > > {
20
22
/**
21
23
* The root node of the GraphQL request.
22
24
*
@@ -28,10 +30,10 @@ export interface IExtractGraphQLSelectionsParams {
28
30
/**
29
31
* The map for renaming the field names to database navigation keys.
30
32
*
31
- * @type {IGraphQLExtractSelectionMap }
33
+ * @type {IGraphQLExtractSelectionMap<T, K> }
32
34
* @memberof IParams
33
35
*/
34
- selectionMap ?: IGraphQLExtractSelectionMap
36
+ selectionMap ?: IGraphQLExtractSelectionMap < T , K >
35
37
36
38
/**
37
39
* The name array of the parent fields.
@@ -51,8 +53,10 @@ export interface IExtractGraphQLSelectionsParams {
51
53
* extraction.
52
54
*
53
55
* @return {* } An object of `selected` field names.
56
+ * @template T The type of the source.
57
+ * @template K The keys included of the source.
54
58
*/
55
- export function extractGraphQLSelections ( params : IExtractGraphQLSelectionsParams ) {
59
+ export function extractGraphQLSelections < T extends object = object , K extends keyof T = Exclude < ObjectKeys < T > , KeysMatching < T , Date > > > ( params : IExtractGraphQLSelectionsParams < T , K > ) {
56
60
const { node, parentFieldKeys = [ ] , selectionMap = { } } = params
57
61
58
62
const { selectionSet } = node
@@ -69,7 +73,7 @@ export function extractGraphQLSelections(params: IExtractGraphQLSelectionsParams
69
73
parentFieldKeys : [ ...parentFieldKeys , fieldName ]
70
74
} )
71
75
72
- const mapper = selectionMap [ fieldName ]
76
+ const mapper = selectionMap [ fieldName as keyof typeof selectionMap ] as any
73
77
let mappedValue : string
74
78
if ( typeof mapper === 'string' ) {
75
79
mappedValue = mapper
@@ -97,7 +101,7 @@ export function extractGraphQLSelections(params: IExtractGraphQLSelectionsParams
97
101
} , { } as Record < string , any > )
98
102
}
99
103
100
- export interface IExtractGraphQLSelectionPathParams {
104
+ interface IExtractGraphQLSelectionPathParams < T extends object = object , K extends keyof T = Exclude < ObjectKeys < T > , KeysMatching < T , Date > > > {
101
105
/**
102
106
* The initial path of the request.
103
107
*
@@ -109,11 +113,11 @@ export interface IExtractGraphQLSelectionPathParams {
109
113
/**
110
114
* The map for renaming the field names to database navigation keys.
111
115
*
112
- * @type {IGraphQLExtractSelectionMap }
116
+ * @type {IGraphQLExtractSelectionMap<T, K> }
113
117
* @memberof IParams
114
118
* @default {}
115
119
*/
116
- selectionMap ?: IGraphQLExtractSelectionMap
120
+ selectionMap ?: IGraphQLExtractSelectionMap < T , K >
117
121
118
122
/**
119
123
* The initial root paths.
@@ -131,10 +135,14 @@ export interface IExtractGraphQLSelectionPathParams {
131
135
* @export
132
136
* @param {IExtractGraphQLSelectionPathParams } params The parameters for
133
137
* extraction.
138
+ *
134
139
* @return {string[] } An array containing field names from the outer level to
135
140
* the inner level.
141
+ *
142
+ * @template T The type of the source.
143
+ * @template K The keys included of the source.
136
144
*/
137
- export function extractGraphQLSelectionPath ( params : IExtractGraphQLSelectionPathParams ) : string [ ] {
145
+ export function extractGraphQLSelectionPath < T extends object = object , K extends keyof T = Exclude < ObjectKeys < T > , KeysMatching < T , Date > > > ( params : IExtractGraphQLSelectionPathParams < T , K > ) : string [ ] {
138
146
const { path, selectionMap = { } , rootPaths = [ ] } = params
139
147
if ( typeof path !== 'object' ) return rootPaths
140
148
@@ -145,7 +153,7 @@ export function extractGraphQLSelectionPath(params: IExtractGraphQLSelectionPath
145
153
rootPaths
146
154
} )
147
155
148
- const mapper = selectionMap [ key ]
156
+ const mapper = selectionMap [ key as keyof typeof selectionMap ] as any
149
157
let mappedValue : string
150
158
if ( typeof mapper === 'string' ) {
151
159
mappedValue = mapper
0 commit comments