{"id":18982291,"url":"https://github.com/profusion/apollo-query-ttl","last_synced_at":"2025-10-17T12:09:32.672Z","repository":{"id":41124306,"uuid":"388253387","full_name":"profusion/apollo-query-ttl","owner":"profusion","description":null,"archived":false,"fork":false,"pushed_at":"2021-11-10T12:09:21.000Z","size":164,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-01-01T11:42:21.766Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/profusion.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-21T21:45:55.000Z","updated_at":"2022-07-11T17:42:46.000Z","dependencies_parsed_at":"2022-09-07T22:02:29.249Z","dependency_job_id":null,"html_url":"https://github.com/profusion/apollo-query-ttl","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profusion%2Fapollo-query-ttl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profusion%2Fapollo-query-ttl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profusion%2Fapollo-query-ttl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profusion%2Fapollo-query-ttl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/profusion","download_url":"https://codeload.github.com/profusion/apollo-query-ttl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239986931,"owners_count":19729706,"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-08T16:13:02.650Z","updated_at":"2025-10-17T12:09:27.654Z","avatar_url":"https://github.com/profusion.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Apollo Queries With TTL\n\nThis project provides an implementation of Apollo Hook Queries (useQuery and useLazyQuery) with a TTL (Time To Live) for cache hits.\n\nThese hooks are useful when you want to control how long your queries can fetch data from cache. In another words, you can provide a TTL in miliseconds to the hook,\nit will use the `cache-first` fetch policy until the TTL time expires, the next fetch after expiration will use a `network-only` policy, then, the TTL will be applied again.\n\nCheck Apollo Client docs about fetch policy, [here](https://www.apollographql.com/docs/react/data/queries/#supported-fetch-policies)\n\n# Instalattion\n\n```bash\n$ npm i @profusion/apollo-query-ttl\n\n$ yarn add @profusion/apollo-query-ttl\n```\n\n# Hooks\n\nThis lib exports two hooks:\n\n- `useQueryWithTTL`: wraps Apollo's useQuery hook with TTL logic\n\nUsage:\n\n```ts\nconst QUERY = gql`\n  query ContinentsQuery {\n    data {\n      id\n      payload\n    }\n  }\n`;\n\nconst TryQueryTTL = (): JSX.Element =\u003e {\n  const { data } = useQueryWithTTL({\n    query,\n    queryOptions: {}, // you can provide all the options available on useQuery\n    ttl: 10000, // it will allow cache hits for 10 seconds\n  });\n\n  return (\n    \u003cpre\u003e\n      {JSON.stringify(data)}\n    \u003c/pre\u003e\n  )\n}\n```\n\n- `useLazyQueryWithTTL`: wraps Apollo's useLazyQuery hook with TTL logic\n\nUsage:\n\n```ts\nconst QUERY = gql`\n  query ContinentsQuery {\n    data {\n      id\n      payload\n    }\n  }\n`;\n\nconst TryQueryTTL = (): JSX.Element =\u003e {\n  const [fetch, { data }] = useLazyQueryWithTTL({\n    query,\n    queryOptions: {}, // you can provide all the options available on useQuery\n    ttl: 10000, // it will allow cache hits for 10 seconds\n  });\n\n  return (\n    \u003cdiv\u003e\n      \u003cpre\u003e\n        {JSON.stringify(data)}\n      \u003c/pre\u003e\n      \u003cbutton onClick={fetch}\u003eFetch!\u003c/button\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n# Typing the data\n\nYou can type your `data` and variables in the same way you do on default queries:\n\n```ts\ninterface DataType {}\ninterface VariablesType {}\n\nconst { data } = useQueryWithTTL\u003cDataType, VariablesType\u003e({\n  query,\n  queryOptions: {\n    variables: {},\n  },\n  ttl: 10000,\n});\n\n// data and queryOptions.variables will be\n// typed as DataType and VariablesType, respectivelly\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprofusion%2Fapollo-query-ttl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprofusion%2Fapollo-query-ttl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprofusion%2Fapollo-query-ttl/lists"}