@@ -17,6 +17,15 @@ type OptionalBooleanOr<T, Otherwise> = T extends boolean
17
17
? boolean | undefined
18
18
: Otherwise
19
19
20
+ /**
21
+ * DeepPartial: A utility type that makes every field and nested field of an
22
+ * object optional. This is useful for writing GraphQL queries that only use a
23
+ * subset of fields and enables you to only pull in what's needed.
24
+ *
25
+ * Without `DeepPartial<T>`, we would have to query entire objects for different
26
+ * fields, which adds unnecessary bloat to the both the server and corresponding
27
+ * response.
28
+ * */
20
29
export type DeepPartial < T > = OptionalStringOr <
21
30
T ,
22
31
OptionalNumberOr <
@@ -51,13 +60,13 @@ export const checkFields = <T>(objects: T[], fields: string[]) => {
51
60
52
61
export const queryBatchHandler = async < T > (
53
62
queryQueryStrings : string [ ] ,
54
- endpt : string
63
+ endpt : string ,
55
64
) => < T > doGqlQuery ( `{ ${ queryQueryStrings . join ( "\n" ) } }` , endpt )
56
65
57
66
export const arg = < T > (
58
67
name : string ,
59
68
value : unknown ,
60
- ignoreQuotes ?: boolean
69
+ ignoreQuotes ?: boolean ,
61
70
) => {
62
71
const isString = typeof value === "string" && ! ignoreQuotes ? `"` : ""
63
72
@@ -92,7 +101,7 @@ export const getWhereArgArr = <T>(whereArgs: IterableDictionary<T>) =>
92
101
`where: ${ objToGql ( whereArgs ) } `
93
102
94
103
export const convertObjectToPropertiesString = < T > (
95
- obj : IterableDictionary < T >
104
+ obj : IterableDictionary < T > ,
96
105
) => {
97
106
let result = ""
98
107
@@ -106,14 +115,14 @@ export const convertObjectToPropertiesString = <T>(
106
115
${ Object . keys ( item )
107
116
. map ( ( k ) => `${ k } ` )
108
117
. join ( "\n" ) }
109
- }`
118
+ }` ,
110
119
)
111
120
. join ( "\n" )
112
121
result += `${ innerString } \n`
113
122
} else if ( typeof value === "object" && value !== null ) {
114
123
result += `${ key } {
115
124
${ convertObjectToPropertiesString (
116
- value as IterableDictionary < T >
125
+ value as IterableDictionary < T > ,
117
126
) }
118
127
}\n`
119
128
} else {
@@ -142,20 +151,20 @@ export const gqlQuery = <T>(
142
151
name : string ,
143
152
typedQueryArgs : IterableDictionary < T > ,
144
153
properties : string ,
145
- excludeParentObject ?: boolean
154
+ excludeParentObject ?: boolean ,
146
155
) => {
147
156
const queryArgList = [ ]
148
157
149
158
if ( typedQueryArgs . where !== undefined ) {
150
159
queryArgList . push (
151
- getWhereArgArr ( typedQueryArgs . where as IterableDictionary < T > )
160
+ getWhereArgArr ( typedQueryArgs . where as IterableDictionary < T > ) ,
152
161
)
153
162
}
154
163
155
164
delete typedQueryArgs . where
156
165
157
166
Object . keys ( typedQueryArgs ) . forEach ( ( key ) =>
158
- queryArgList . push ( arg < T > ( key , typedQueryArgs [ key ] , true ) )
167
+ queryArgList . push ( arg < T > ( key , typedQueryArgs [ key ] , true ) ) ,
159
168
)
160
169
161
170
const hasQueryList = ( char : string ) => ( queryArgList . length > 0 ? char : "" )
@@ -170,7 +179,7 @@ export const gqlQuery = <T>(
170
179
export const doGqlQuery = async < T > (
171
180
gqlQuery : string ,
172
181
gqlEndpt : string ,
173
- headers ?: HeadersInit
182
+ headers ?: HeadersInit ,
174
183
) => {
175
184
const rawResp = await fetch ( gqlEndpt , {
176
185
method : "POST" ,
0 commit comments