-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tally-fetch): added fetch from subgraph
- Loading branch information
1 parent
95d0f95
commit 981a67a
Showing
3 changed files
with
75 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { config } from "~/config"; | ||
|
||
import type { Tally } from "./types"; | ||
|
||
import { createCachedFetch } from "./fetch"; | ||
|
||
const cachedFetch = createCachedFetch({ ttl: 1000 * 60 * 10 }); | ||
|
||
export interface GraphQLResponse { | ||
data?: { | ||
tally: Tally; | ||
}; | ||
} | ||
|
||
const tallyQuery = ` | ||
query TallyQuery { | ||
tally (id: $id) { | ||
id | ||
results { | ||
id | ||
result | ||
} | ||
} | ||
} | ||
`; | ||
|
||
/** | ||
* Maps the tally data to the ITallyData interface | ||
* | ||
* @param tally - The tally data | ||
* @returns The mapped tally data | ||
*/ | ||
function mappedTallyData(tally: Tally): Tally { | ||
return { | ||
id: tally.id, | ||
results: tally.results, | ||
}; | ||
} | ||
|
||
/** | ||
* Fetches the tally data from the subgraph | ||
* | ||
* @returns The tally data | ||
*/ | ||
export async function fetchTally(id: string): Promise<Tally | undefined> { | ||
const tally = await cachedFetch<{ tally: Tally }>(config.maciSubgraphUrl, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
query: tallyQuery.replace("id: $id", `id: "${id}"`), | ||
}), | ||
}).then((response: GraphQLResponse) => response.data?.tally); | ||
// cast this to a TallyData array so that we can deal with one object only in MACIContext | ||
return tally ? mappedTallyData(tally) : undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters