-
Notifications
You must be signed in to change notification settings - Fork 128
Added useFlowQueryRaw hook #2523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
624d46c
Added rawQuery function with improved query implementation
mfbz 98507ff
Implemented useFlowQueryRaw hook with tests
mfbz 493fa30
Added changeset
mfbz e127f27
Added shared function to encode query args
mfbz c1f10ac
Added encodeQueryArgs tests
mfbz 43cb3d8
Changed rawQuery to queryRaw
mfbz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
--- | ||
"@onflow/fcl-core": minor | ||
"@onflow/fcl": minor | ||
"@onflow/kit": minor | ||
--- | ||
|
||
Added useFlowQueryRaw hook to execute a query and get non-decoded data as result. |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,34 @@ | ||
import * as sdk from "@onflow/sdk" | ||
import {QueryOptions, rawQuery} from "./raw-query" | ||
|
||
/** | ||
* @description Allows you to submit scripts to query the blockchain. | ||
* | ||
* @param opts Query Options and configuration | ||
* @param opts.cadence Cadence Script used to query Flow | ||
* @param opts.args Arguments passed to cadence script | ||
* @param opts.template Interaction Template for a script | ||
* @param opts.isSealed Block Finality | ||
* @param opts.limit Compute Limit for Query | ||
* @returns A promise that resolves to the query result | ||
* | ||
* @example | ||
* const cadence = ` | ||
* cadence: ` | ||
* access(all) fun main(a: Int, b: Int, c: Address): Int { | ||
* log(c) | ||
* return a + b | ||
* } | ||
* `.trim() | ||
* | ||
* const args = (arg, t) => [ | ||
* arg(5, t.Int), | ||
* arg(7, t.Int), | ||
* arg("0xb2db43ad6bc345fec9", t.Address), | ||
* ] | ||
* | ||
* await query({ cadence, args }) | ||
*/ | ||
export async function query(opts: QueryOptions = {}): Promise<any> { | ||
return rawQuery(opts).then(sdk.decode) | ||
} |
This file contains hidden or 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,55 @@ | ||
import * as sdk from "@onflow/sdk" | ||
import type {ArgsFn} from "./args" | ||
import {normalizeArgs} from "./utils/normalize-args" | ||
import {preQuery} from "./utils/pre" | ||
import {prepTemplateOpts} from "./utils/prep-template-opts" | ||
|
||
export interface QueryOptions { | ||
cadence?: string | ||
args?: ArgsFn | ||
template?: any | ||
isSealed?: boolean | ||
limit?: number | ||
} | ||
|
||
/** | ||
* @description Allows you to submit scripts to query the blockchain and get raw response data. | ||
* | ||
* @param opts Query Options and configuration | ||
* @param opts.cadence Cadence Script used to query Flow | ||
* @param opts.args Arguments passed to cadence script | ||
* @param opts.template Interaction Template for a script | ||
* @param opts.isSealed Block Finality | ||
* @param opts.limit Compute Limit for Query | ||
* @returns A promise that resolves to the raw query result | ||
* | ||
* @example | ||
* const cadence = ` | ||
* cadence: ` | ||
* access(all) fun main(a: Int, b: Int, c: Address): Int { | ||
* log(c) | ||
* return a + b | ||
* } | ||
* `.trim() | ||
* | ||
* const args = (arg, t) => [ | ||
* arg(5, t.Int), | ||
* arg(7, t.Int), | ||
* arg("0xb2db43ad6bc345fec9", t.Address), | ||
* ] | ||
* | ||
* await rawQuery({ cadence, args }) | ||
*/ | ||
export async function rawQuery(opts: QueryOptions = {}): Promise<any> { | ||
await preQuery(opts) | ||
opts = await prepTemplateOpts(opts) | ||
|
||
return sdk.send([ | ||
sdk.script(opts.cadence!), | ||
sdk.args(normalizeArgs(opts.args || [])), | ||
sdk.atLatestBlock(opts.isSealed ?? false), | ||
opts.limit && | ||
typeof opts.limit === "number" && | ||
(sdk.limit(opts.limit!) as any), | ||
]) | ||
} |
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
packages/fcl-core/src/interaction-template-utils/get-interaction-template-audits.js
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export { | ||
VERSION, | ||
query, | ||
rawQuery, | ||
verifyUserSignatures, | ||
serialize, | ||
tx, | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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,186 @@ | ||
import {renderHook, act, waitFor} from "@testing-library/react" | ||
import * as fcl from "@onflow/fcl" | ||
import {FlowProvider} from "../provider" | ||
import {useFlowQueryRaw} from "./useFlowQueryRaw" | ||
|
||
jest.mock("@onflow/fcl", () => require("../__mocks__/fcl").default) | ||
|
||
describe("useFlowQueryRaw", () => { | ||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test("returns undefined when no cadence is provided", async () => { | ||
const {result} = renderHook(() => useFlowQueryRaw({cadence: ""}), { | ||
wrapper: FlowProvider, | ||
}) | ||
|
||
expect(result.current.data).toBeUndefined() | ||
await waitFor(() => expect(result.current.isLoading).toBe(false)) | ||
}) | ||
|
||
test("fetches data successfully", async () => { | ||
const cadenceScript = "access(all) fun main(): Int { return 42 }" | ||
const expectedResult = 42 | ||
const queryMock = jest.mocked(fcl.rawQuery) | ||
queryMock.mockResolvedValueOnce(expectedResult) | ||
|
||
let hookResult: any | ||
|
||
await act(async () => { | ||
const {result} = renderHook( | ||
() => useFlowQueryRaw({cadence: cadenceScript}), | ||
{ | ||
wrapper: FlowProvider, | ||
} | ||
) | ||
hookResult = result | ||
}) | ||
|
||
expect(hookResult.current.data).toBeUndefined() | ||
|
||
await waitFor(() => expect(hookResult.current.isLoading).toBe(false)) | ||
expect(hookResult.current.data).toEqual(expectedResult) | ||
expect(queryMock).toHaveBeenCalledWith({ | ||
cadence: cadenceScript, | ||
args: undefined, | ||
}) | ||
}) | ||
|
||
test("does not fetch data when enabled is false", async () => { | ||
const cadenceScript = "access(all) fun main(): Int { return 42 }" | ||
const queryMock = jest.mocked(fcl.rawQuery) | ||
|
||
renderHook( | ||
() => useFlowQueryRaw({cadence: cadenceScript, query: {enabled: false}}), | ||
{ | ||
wrapper: FlowProvider, | ||
} | ||
) | ||
|
||
// wait a little to ensure fcl.rawQuery isn't called | ||
await waitFor(() => { | ||
expect(queryMock).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
test("handles error from fcl.rawQuery", async () => { | ||
const cadenceScript = "access(all) fun main(): Int { return 42 }" | ||
const testError = new Error("Query failed") | ||
const queryMock = jest.mocked(fcl.rawQuery) | ||
queryMock.mockRejectedValueOnce(testError) | ||
|
||
let hookResult: any | ||
|
||
await act(async () => { | ||
const {result} = renderHook( | ||
() => useFlowQueryRaw({cadence: cadenceScript}), | ||
{ | ||
wrapper: FlowProvider, | ||
} | ||
) | ||
hookResult = result | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.isLoading).toBe(false)) | ||
expect(hookResult.current.data).toBeUndefined() | ||
expect(hookResult.current.error).toEqual(testError) | ||
}) | ||
|
||
test("refetch function works correctly", async () => { | ||
const cadenceScript = "access(all) fun main(): Int { return 42 }" | ||
const initialResult = 42 | ||
const updatedResult = 100 | ||
const queryMock = jest.mocked(fcl.rawQuery) | ||
queryMock.mockResolvedValueOnce(initialResult) | ||
|
||
let hookResult: any | ||
|
||
await act(async () => { | ||
const {result} = renderHook( | ||
() => useFlowQueryRaw({cadence: cadenceScript}), | ||
{ | ||
wrapper: FlowProvider, | ||
} | ||
) | ||
hookResult = result | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.data).toEqual(initialResult)) | ||
|
||
queryMock.mockResolvedValueOnce(updatedResult) | ||
act(() => { | ||
hookResult.current.refetch() | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.data).toEqual(updatedResult)) | ||
expect(queryMock).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
test("supports args function parameter", async () => { | ||
const cadenceScript = "access(all) fun main(a: Int): Int { return a }" | ||
const expectedResult = 7 | ||
const queryMock = jest.mocked(fcl.rawQuery) | ||
queryMock.mockResolvedValueOnce(expectedResult) | ||
|
||
const argsFunction = (arg: typeof fcl.arg, t: typeof fcl.t) => [ | ||
arg(7, t.Int), | ||
] | ||
|
||
let hookResult: any | ||
|
||
await act(async () => { | ||
const {result} = renderHook( | ||
() => useFlowQueryRaw({cadence: cadenceScript, args: argsFunction}), | ||
{ | ||
wrapper: FlowProvider, | ||
} | ||
) | ||
hookResult = result | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.isLoading).toBe(false)) | ||
expect(hookResult.current.data).toEqual(expectedResult) | ||
expect(queryMock).toHaveBeenCalledWith({ | ||
cadence: cadenceScript, | ||
args: argsFunction, | ||
}) | ||
}) | ||
|
||
test("detects args changes", async () => { | ||
const cadenceScript = "access(all) fun main(a: Int): Int { return a }" | ||
const initialResult = 7 | ||
const updatedResult = 42 | ||
const queryMock = jest.mocked(fcl.rawQuery) | ||
queryMock.mockResolvedValueOnce(initialResult) | ||
|
||
const argsFunction = (arg: typeof fcl.arg, t: typeof fcl.t) => [ | ||
arg(7, t.Int), | ||
] | ||
|
||
let hookResult: any | ||
let hookRerender: any | ||
|
||
await act(async () => { | ||
const {result, rerender} = renderHook(useFlowQueryRaw, { | ||
wrapper: FlowProvider, | ||
initialProps: {cadence: cadenceScript, args: argsFunction}, | ||
}) | ||
hookResult = result | ||
hookRerender = rerender | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.isLoading).toBe(false)) | ||
expect(hookResult.current.data).toEqual(initialResult) | ||
|
||
queryMock.mockResolvedValueOnce(updatedResult) | ||
await act(() => { | ||
hookRerender({ | ||
cadence: cadenceScript, | ||
args: (arg: typeof fcl.arg, t: typeof fcl.t) => [arg(42, t.Int)], | ||
}) | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.data).toEqual(updatedResult)) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.