Skip to content

Commit 26176e3

Browse files
committed
docs(gql): add JSDoc comment for DeepPartial
1 parent 599041f commit 26176e3

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

bun.lockb

470 KB
Binary file not shown.

src/gql/utils/consts.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ type OptionalBooleanOr<T, Otherwise> = T extends boolean
1717
? boolean | undefined
1818
: Otherwise
1919

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+
* */
2029
export type DeepPartial<T> = OptionalStringOr<
2130
T,
2231
OptionalNumberOr<
@@ -51,13 +60,13 @@ export const checkFields = <T>(objects: T[], fields: string[]) => {
5160

5261
export const queryBatchHandler = async <T>(
5362
queryQueryStrings: string[],
54-
endpt: string
63+
endpt: string,
5564
) => <T>doGqlQuery(`{ ${queryQueryStrings.join("\n")} }`, endpt)
5665

5766
export const arg = <T>(
5867
name: string,
5968
value: unknown,
60-
ignoreQuotes?: boolean
69+
ignoreQuotes?: boolean,
6170
) => {
6271
const isString = typeof value === "string" && !ignoreQuotes ? `"` : ""
6372

@@ -92,7 +101,7 @@ export const getWhereArgArr = <T>(whereArgs: IterableDictionary<T>) =>
92101
`where: ${objToGql(whereArgs)}`
93102

94103
export const convertObjectToPropertiesString = <T>(
95-
obj: IterableDictionary<T>
104+
obj: IterableDictionary<T>,
96105
) => {
97106
let result = ""
98107

@@ -106,14 +115,14 @@ export const convertObjectToPropertiesString = <T>(
106115
${Object.keys(item)
107116
.map((k) => `${k}`)
108117
.join("\n")}
109-
}`
118+
}`,
110119
)
111120
.join("\n")
112121
result += `${innerString}\n`
113122
} else if (typeof value === "object" && value !== null) {
114123
result += `${key} {
115124
${convertObjectToPropertiesString(
116-
value as IterableDictionary<T>
125+
value as IterableDictionary<T>,
117126
)}
118127
}\n`
119128
} else {
@@ -142,20 +151,20 @@ export const gqlQuery = <T>(
142151
name: string,
143152
typedQueryArgs: IterableDictionary<T>,
144153
properties: string,
145-
excludeParentObject?: boolean
154+
excludeParentObject?: boolean,
146155
) => {
147156
const queryArgList = []
148157

149158
if (typedQueryArgs.where !== undefined) {
150159
queryArgList.push(
151-
getWhereArgArr(typedQueryArgs.where as IterableDictionary<T>)
160+
getWhereArgArr(typedQueryArgs.where as IterableDictionary<T>),
152161
)
153162
}
154163

155164
delete typedQueryArgs.where
156165

157166
Object.keys(typedQueryArgs).forEach((key) =>
158-
queryArgList.push(arg<T>(key, typedQueryArgs[key], true))
167+
queryArgList.push(arg<T>(key, typedQueryArgs[key], true)),
159168
)
160169

161170
const hasQueryList = (char: string) => (queryArgList.length > 0 ? char : "")
@@ -170,7 +179,7 @@ export const gqlQuery = <T>(
170179
export const doGqlQuery = async <T>(
171180
gqlQuery: string,
172181
gqlEndpt: string,
173-
headers?: HeadersInit
182+
headers?: HeadersInit,
174183
) => {
175184
const rawResp = await fetch(gqlEndpt, {
176185
method: "POST",

0 commit comments

Comments
 (0)