Skip to content

Commit

Permalink
fix: gql where args (#298)
Browse files Browse the repository at this point in the history
* fix: gql where obj to gql string

* fix: review comments
  • Loading branch information
ruslan-sh-r authored Jan 24, 2024
1 parent 462bbe8 commit b57ef0a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,5 @@ typings/
# End of https://www.gitignore.io/api/node,intellij+all,visualstudiocode
.yarn
.DS_Store

nibiru/
26 changes: 22 additions & 4 deletions src/indexer-nibi/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,31 @@ export const arg = (name: string, value: any, ignoreQuotes?: boolean) => {
return `${name}: ${isString}${value}${isString}`
}

export const getWhereArgArr = (whereArgs: any) =>
`where: {
${Object.keys(whereArgs)
.map((key) => arg(key, whereArgs[key]))
export const objToGql = (obj: any): string | number => {
// Make sure we don't alter integers.
if (typeof obj === "number") {
return obj
}

// Stringify everything other than objects and arrays.
if (typeof obj !== "object" || Array.isArray(obj)) {
return JSON.stringify(obj)
}

// Iterate through object keys to convert into a string
// to be interpolated into the query.
const res = `{
${Object.keys(obj)
.map((key) => `${key}:${objToGql(obj[key])}`)
.join(", ")}
}`

return res
}

export const getWhereArgArr = (whereArgs: any) =>
`where: ${objToGql(whereArgs)}`

export const convertObjectToPropertiesString = (obj: any) => {
let result = ""

Expand Down
4 changes: 4 additions & 0 deletions src/indexer-nibi/heart-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,10 @@ const testWasm = async (args: QueryWasmArgs, fields?: GqlWasmFields) => {
test("wasm", async () => {
await testWasm({
userContracts: {
where: {
contractAddress: { like: "123" },
userAddress: { eq: "456" },
},
limit: 1,
},
})
Expand Down

0 comments on commit b57ef0a

Please sign in to comment.