{"id":19281756,"url":"https://github.com/scality/react-chained-query","last_synced_at":"2025-04-22T01:31:05.840Z","repository":{"id":223666637,"uuid":"592681253","full_name":"scality/react-chained-query","owner":"scality","description":"A wrapper of react-query useQuery hook allowing chaining queries.","archived":false,"fork":false,"pushed_at":"2024-04-24T16:54:30.000Z","size":404,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":21,"default_branch":"main","last_synced_at":"2024-04-24T19:19:53.301Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scality.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-24T09:47:07.000Z","updated_at":"2024-07-17T19:13:54.259Z","dependencies_parsed_at":"2024-02-27T15:31:42.626Z","dependency_job_id":"a5b81065-828c-45b7-a580-bb9d8e31a8b1","html_url":"https://github.com/scality/react-chained-query","commit_stats":null,"previous_names":["scality/react-chained-query"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scality%2Freact-chained-query","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scality%2Freact-chained-query/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scality%2Freact-chained-query/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scality%2Freact-chained-query/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scality","download_url":"https://codeload.github.com/scality/react-chained-query/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250161969,"owners_count":21385014,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T21:24:03.436Z","updated_at":"2025-04-22T01:31:05.545Z","avatar_url":"https://github.com/scality.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-chained-query\n\n## Features\n\n### useChainedQuery\n\n`useChainedQuery` hook consit of a wrapper on top of react-query `useQuery`. This `useChainedQuery` hook allow chaining queries instead of runnning them concurently, it aims to solve problems that may occurs when hitting a slow backend with too many requests.\n\nBy managing a queue and executing the request one after another, it could give the capability for an application to display the information sequentially.\n\n### useChainedMutations\n\nThis `useChainedMutations` hook takes an array of mutations and a function to compute the variables for the next mutation in the chain. It returns an object containing a `mutate` function that triggers the chain of mutations, a `computeVariablesForNext` function that computes the variables for the next mutation, and an array of `mutationsWithRetry` that includes a retry function for each mutation.\n\n## Install\n\n```bash\nnpm install @scality/react-chained-query\n```\n\n## Quickstart\n\n### useChainedQuery\n\n```js\nimport { QueryClient, QueryClientProvider } from 'react-query';\nimport { ChainedQueryProvider, useChainedQuery } from './useChainedQuery';\nimport { useEffect, useState } from 'react';\n\nconst queryClient = new QueryClient();\n\nfunction Component1() {\n  const { data } = useChainedQuery({\n    queryKey: ['key', 'arg'],\n    queryFn: async () =\u003e {\n      await new Promise((resolve) =\u003e setTimeout(resolve, 2_000));\n      return '1';\n    },\n  });\n  return \u003c\u003e{data}\u003c/\u003e;\n}\n\nfunction Component2() {\n  const { data } = useChainedQuery({\n    queryKey: ['key', 'arg1'],\n    queryFn: async () =\u003e {\n      await new Promise((resolve) =\u003e setTimeout(resolve, 1_000));\n      return '2';\n    },\n  });\n  return \u003c\u003e{data}\u003c/\u003e;\n}\n\nexport default function App() {\n  return (\n    \u003cQueryClientProvider client={queryClient}\u003e\n      \u003cChainedQueryProvider\u003e\n        \u003cdiv className=\"App\"\u003e\n          \u003ch2\u003eHello, useChainedQuery! \u003c/h2\u003e\n          \u003cComponent1 /\u003e\n          \u003cComponent2 /\u003e\n        \u003c/div\u003e\n      \u003c/ChainedQueryProvider\u003e\n    \u003c/QueryClientProvider\u003e\n  );\n}\n```\n\n[A complete example here](https://codesandbox.io/s/use-chained-query-forked-j3mfed?file=/src/App.tsx)\n\n### useChainedMutations\n\n```js\nimport { useMutation } from 'react-query';\nimport { useChainedMutations } from './useChainedMutations';\n\nconst useUpdatePosts = () =\u003e {\n  return useMutation({\n    mutationFn: async (id: string) =\u003e {\n      const res = await fetch(\n        `https://jsonplaceholder.typicode.com/posts/${id}`,\n        {\n          headers: {\n            'Content-type': 'application/json; charset=UTF-8',\n          },\n          method: 'PUT',\n          body: JSON.stringify({\n            id: 1,\n            title: 'foo',\n            body: 'bar',\n            userId: id,\n          }),\n        },\n      );\n\n      if (!res.ok) throw res.statusText;\n\n      return await res.json();\n    },\n  });\n};\n\nconst mutations = [{ ...useUpdatePosts, 'user1'}, { ...useUpdatePosts, 'user2'}];\nconst { mutate } = useChainedMutations({\n  mutations,\n  computeVariablesForNext: {\n    user1: () =\u003e {\n      return 'user1';\n    },\n    user2: () =\u003e {\n      return 'user2';\n    }\n  },\n});\n\nexport default function App() {\n  return (\n    \u003cdiv className=\"App\"\u003e\n      \u003ch2\u003eHello, useChainedMutations! \u003c/h2\u003e\n      \u003cbutton onClick={() =\u003e mutate()}\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n## Advanced Documentation\n\nIn order to use `useChainedQuery` in your component, it has be below `QueryClientProvider` and `ChainedQueryProvider`.\n\nIt's possibile to have several `ChainedQueryProvider` each of them would then holds it's own queue of queries.\n\n```js\n\u003cQueryClientProvider\u003e\n  \u003cChainedQueryProvider\u003e\n    \u003cYourComponent /\u003e\n  \u003c/ChainedQueryProvider\u003e\n\u003c/QueryClientProvider\u003e\n```\n\nMade with ❤️ by Pod-UI at [Scality](https://github.com/scality/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscality%2Freact-chained-query","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscality%2Freact-chained-query","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscality%2Freact-chained-query/lists"}