Skip to content

Commit

Permalink
Merge pull request #304 from NibiruChain/develop
Browse files Browse the repository at this point in the history
fix: update mutation logic (#303)
  • Loading branch information
cgilbe27 authored Feb 5, 2024
2 parents b570599 + 8358e3e commit 8c07bab
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@
"@cosmjs/tendermint-rpc": "^0.31.0",
"bignumber.js": "^9.1.1",
"cross-fetch": "4.0.0",
"graphql": "^16.7.1",
"graphql-ws": "^5.14.0"
},
"peerDependencies": {
"@cosmjs/cosmwasm-stargate": "^0.31.0",
"@cosmjs/proto-signing": "^0.31.0",
"@cosmjs/stargate": "^0.31.0",
"@cosmjs/tendermint-rpc": "^0.31.0"
"@cosmjs/tendermint-rpc": "^0.31.0",
"graphql": "^16.7.1"
},
"devDependencies": {
"@bufbuild/buf": "^1.28.1",
Expand All @@ -74,7 +76,6 @@
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jest": "^27.1.7",
"eslint-plugin-prettier": "^4.2.1",
"graphql": "^16.7.1",
"husky": "^7.0.2",
"jest": "^29.0.0",
"lint-staged": "^13.2.2",
Expand Down
43 changes: 43 additions & 0 deletions src/gql/heart-monitor/heart-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ import {
defaultTask,
QueryMarketingArgs,
MarketingFields,
MutationMarketingArgs,
GQLMarketingMutation,
GQLTwitterUser,
} from ".."

const nibiruUrl = "testnet-1"
Expand Down Expand Up @@ -433,6 +436,46 @@ test("markPriceCandlesSubscription", async () => {
)
})

const testMarketingMutation = async (
args: MutationMarketingArgs,
fields?: GQLTwitterUser
) => {
const resp = await heartMonitor.marketingMutation(args, {}, fields)
expect(resp).toHaveProperty("marketing")

if (resp.marketing) {
const { marketing } = resp

checkFields(
[marketing],
[
"completedTasks",
"creationTimestamp",
"displayName",
"followersCount",
"followingCount",
"id",
"likes",
"listedCount",
"nibiAddress",
"tweets",
"tweetsCount",
"username",
]
)
}
}

// Create JIT JWT for this test
test.skip("marketinMutation", async () => {
await testMarketingMutation({
updateTwitterUser: {
id: "800528778854182912",
nibiAddress: "nibi1p6luzkxeufy29reymgjqnl5mv6a6gae07cphed",
},
})
})

const testMarketingQuery = async (
args: QueryMarketingArgs,
fields?: MarketingFields
Expand Down
10 changes: 6 additions & 4 deletions src/gql/heart-monitor/heart-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ import {
MarketingFields,
QueryMarketingArgs,
marketingQuery,
GQLMarketingMutation,
MutationMarketingArgs,
marketingMutation,
GqlOutMarketingMutation,
GQLTwitterUser,
} from ".."

/** IHeartMonitor is an interface for a Heart Monitor GraphQL API.
Expand Down Expand Up @@ -154,7 +154,8 @@ export interface IHeartMonitor {

readonly marketingMutation: (
args: Partial<MutationMarketingArgs>,
fields?: Partial<GQLMarketingMutation>
headers: HeadersInit,
fields?: Partial<GQLTwitterUser>
) => Promise<GqlOutMarketingMutation>

readonly marketingQuery: (
Expand Down Expand Up @@ -337,8 +338,9 @@ export class HeartMonitor implements IHeartMonitor {

marketingMutation = async (
args: Partial<MutationMarketingArgs>,
fields?: Partial<GQLMarketingMutation>
) => marketingMutation(args, this.gqlEndpt, fields)
headers: HeadersInit,
fields?: Partial<GQLTwitterUser>
) => marketingMutation(args, this.gqlEndpt, headers, fields)

markPriceCandles = async (
args: Partial<GQLQueryGqlMarkPriceCandlesArgs>,
Expand Down
13 changes: 9 additions & 4 deletions src/gql/mutation/marketing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
gqlQuery,
GQLUpdateTwitterUserInput,
GQLMutation,
GQLMarketingMutation,
GQLTwitterUser,
} from ".."

export type MutationMarketingArgs = {
Expand All @@ -19,7 +19,7 @@ export interface GqlOutMarketingMutation {
export const marketingMutationString = (
args: Partial<MutationMarketingArgs>,
excludeParentObject: boolean,
fields?: Partial<GQLMarketingMutation>
fields?: Partial<GQLTwitterUser>
) =>
gqlQuery(
"marketing",
Expand All @@ -34,6 +34,11 @@ export const marketingMutationString = (
export const marketingMutation = async (
args: Partial<MutationMarketingArgs>,
endpt: string,
fields?: Partial<GQLMarketingMutation>
authorizationHeader: HeadersInit,
fields?: Partial<GQLTwitterUser>
): Promise<GqlOutMarketingMutation> =>
doGqlQuery(marketingMutationString(args, false, fields), endpt)
doGqlQuery(
marketingMutationString(args, false, fields),
endpt,
authorizationHeader
)
20 changes: 15 additions & 5 deletions src/gql/utils/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ export const queryBatchHandler = async <T>(
endpt: string
) => <T>doGqlQuery(`{ ${queryQueryStrings.join("\n")} }`, endpt)

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

return `${name}: ${isString}${value}${isString}`
return typeof value === "object"
? `${name}: ${objToGql(value as IterableDictionary<T>)}`
: `${name}: ${isString}${value}${isString}`
}

export const objToGql = <T>(obj: IterableDictionary<T>): string | number => {
Expand Down Expand Up @@ -111,7 +117,7 @@ export const gqlQuery = <T>(
delete typedQueryArgs.where

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

const hasQueryList = (char: string) => (queryArgList.length > 0 ? char : "")
Expand All @@ -123,10 +129,14 @@ export const gqlQuery = <T>(
${excludeParentObject ? "" : "}"}`
}

export const doGqlQuery = async <T>(gqlQuery: string, gqlEndpt: string) => {
export const doGqlQuery = async <T>(
gqlQuery: string,
gqlEndpt: string,
headers?: HeadersInit
) => {
const rawResp = await fetch(gqlEndpt, {
method: "POST",
headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json", ...headers },
body: JSON.stringify({ query: gqlQuery }),
})
return cleanResponse(rawResp) as T
Expand Down

1 comment on commit 8c07bab

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 93%
94.62% (1197/1265) 87.7% (542/618) 88.03% (309/351)

Please sign in to comment.