Skip to content

Commit

Permalink
docs(gql): add JSDoc comment for DeepPartial
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Mar 5, 2024
1 parent 599041f commit 26176e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Binary file added bun.lockb
Binary file not shown.
27 changes: 18 additions & 9 deletions src/gql/utils/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ type OptionalBooleanOr<T, Otherwise> = T extends boolean
? boolean | undefined
: Otherwise

/**
* DeepPartial: A utility type that makes every field and nested field of an
* object optional. This is useful for writing GraphQL queries that only use a
* subset of fields and enables you to only pull in what's needed.
*
* Without `DeepPartial<T>`, we would have to query entire objects for different
* fields, which adds unnecessary bloat to the both the server and corresponding
* response.
* */
export type DeepPartial<T> = OptionalStringOr<
T,
OptionalNumberOr<
Expand Down Expand Up @@ -51,13 +60,13 @@ export const checkFields = <T>(objects: T[], fields: string[]) => {

export const queryBatchHandler = async <T>(
queryQueryStrings: string[],
endpt: string
endpt: string,
) => <T>doGqlQuery(`{ ${queryQueryStrings.join("\n")} }`, endpt)

export const arg = <T>(
name: string,
value: unknown,
ignoreQuotes?: boolean
ignoreQuotes?: boolean,
) => {
const isString = typeof value === "string" && !ignoreQuotes ? `"` : ""

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

export const convertObjectToPropertiesString = <T>(
obj: IterableDictionary<T>
obj: IterableDictionary<T>,
) => {
let result = ""

Expand All @@ -106,14 +115,14 @@ export const convertObjectToPropertiesString = <T>(
${Object.keys(item)
.map((k) => `${k}`)
.join("\n")}
}`
}`,
)
.join("\n")
result += `${innerString}\n`
} else if (typeof value === "object" && value !== null) {
result += `${key} {
${convertObjectToPropertiesString(
value as IterableDictionary<T>
value as IterableDictionary<T>,
)}
}\n`
} else {
Expand Down Expand Up @@ -142,20 +151,20 @@ export const gqlQuery = <T>(
name: string,
typedQueryArgs: IterableDictionary<T>,
properties: string,
excludeParentObject?: boolean
excludeParentObject?: boolean,
) => {
const queryArgList = []

if (typedQueryArgs.where !== undefined) {
queryArgList.push(
getWhereArgArr(typedQueryArgs.where as IterableDictionary<T>)
getWhereArgArr(typedQueryArgs.where as IterableDictionary<T>),
)
}

delete typedQueryArgs.where

Object.keys(typedQueryArgs).forEach((key) =>
queryArgList.push(arg<T>(key, typedQueryArgs[key], true))
queryArgList.push(arg<T>(key, typedQueryArgs[key], true)),
)

const hasQueryList = (char: string) => (queryArgList.length > 0 ? char : "")
Expand All @@ -170,7 +179,7 @@ export const gqlQuery = <T>(
export const doGqlQuery = async <T>(
gqlQuery: string,
gqlEndpt: string,
headers?: HeadersInit
headers?: HeadersInit,
) => {
const rawResp = await fetch(gqlEndpt, {
method: "POST",
Expand Down

0 comments on commit 26176e3

Please sign in to comment.