Skip to content

Commit

Permalink
fix react query
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Oct 21, 2024
1 parent 3b926bc commit cd6db10
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions libs/interchainjs/src/react-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export const useRpcClient = <TData = ProtobufRpcClient>({
options,
rpcClientQueryKey,
}: UseRpcClientQuery<TData>) => {
const queryClient = useQueryClient();
const key = rpcClientQueryKey || DEFAULT_RPC_CLIENT_QUERY_KEY;
return useQuery<ProtobufRpcClient, Error, TData>([key, rpcEndpoint], async () => {
if(!rpcEndpoint) {
Expand All @@ -73,8 +72,6 @@ export const useRpcClient = <TData = ProtobufRpcClient>({
throw new Error('Failed to connect to rpc client');
}

queryClient.setQueryData([key], client);

return client;
}, options);
};
Expand All @@ -90,17 +87,19 @@ export function buildUseQuery<TReq, TRes>(opts: UseQueryBuilderOptions<TReq, TRe
request,
options,
rpcEndpoint,
rpcClientQueryKey,
customizedQueryKey,
}: UseQueryParams<TReq, TRes, TData>) => {
const queryClient = useQueryClient();
const key = rpcClientQueryKey || DEFAULT_RPC_CLIENT_QUERY_KEY;
const queryKey = rpcEndpoint ? [key, rpcEndpoint] : [key];
const rpc = queryClient.getQueryData<Rpc>(queryKey);
const queryFn = opts.builderQueryFn(()=>{
return rpc;
});
return useQuery<TRes, Error, TData>(customizedQueryKey || [opts.queryKeyPrefix, request], () => queryFn(request), options);
return useQuery<TRes, Error, TData>(customizedQueryKey || [opts.queryKeyPrefix, request], async () => {
const rpc = await getRpcClient(rpcEndpoint);
if(!rpc) {
throw new Error('Failed to connect to rpc client');
}
const queryFn = opts.builderQueryFn(()=>{
return rpc;
});

return queryFn(request);
}, options);
};
}

Expand All @@ -111,8 +110,7 @@ export interface UseQueryParams<TReq, TRes, TData = TRes> extends ReactQueryPara

export interface ReactMutationParams<TData, TError, TVariables, TContext = unknown> {
options?: MutationOptions<TData, TError, TVariables, TContext>;
rpcEndpoint?: string | HttpEndpoint;
signingClientQueryKey?: string;
signingClient?: ISigningClient;
}


Expand All @@ -129,20 +127,16 @@ export interface UseMutationBuilderOptions<TMsg> {
export function buildUseMutation<TMsg, TError>(opts: UseMutationBuilderOptions<TMsg>) {
return ({
options,
rpcEndpoint,
signingClientQueryKey
signingClient,
}: ReactMutationParams<DeliverTxResponse, TError, ITxArgs<TMsg>>) => {
const queryClient = useQueryClient();
const key = signingClientQueryKey || DEFAULT_SIGNING_CLIENT_QUERY_KEY;
const queryKey = rpcEndpoint ? [key, rpcEndpoint] : [DEFAULT_SIGNING_CLIENT_QUERY_KEY];
const signingClient = queryClient.getQueryData<ISigningClient>(queryKey);

const mutationFn = opts.builderMutationFn(() => {
return signingClient;
});

return useMutation<DeliverTxResponse, Error, ITxArgs<TMsg>>(
(reqData: ITxArgs<TMsg>) => mutationFn(reqData.signerAddress, reqData.message, reqData.fee, reqData.memo),
async (reqData: ITxArgs<TMsg>) => {
return mutationFn(reqData.signerAddress, reqData.message, reqData.fee, reqData.memo);
},
options as Omit<UseMutationOptions<DeliverTxResponse, Error, ITxArgs<TMsg>, unknown>, "mutationFn">
);
};
Expand Down

0 comments on commit cd6db10

Please sign in to comment.