{"id":22220550,"url":"https://github.com/drawbotics/urql-computed-exchange","last_synced_at":"2025-07-27T15:33:05.291Z","repository":{"id":43912143,"uuid":"196635317","full_name":"Drawbotics/urql-computed-exchange","owner":"Drawbotics","description":"An URQL exchange to compute data from resolvers in domain entities","archived":false,"fork":false,"pushed_at":"2023-01-05T12:22:09.000Z","size":1023,"stargazers_count":13,"open_issues_count":11,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-02-26T11:16:06.859Z","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":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Drawbotics.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}},"created_at":"2019-07-12T19:34:30.000Z","updated_at":"2022-11-27T22:54:43.000Z","dependencies_parsed_at":"2022-09-09T22:41:48.678Z","dependency_job_id":null,"html_url":"https://github.com/Drawbotics/urql-computed-exchange","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Furql-computed-exchange","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Furql-computed-exchange/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Furql-computed-exchange/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drawbotics%2Furql-computed-exchange/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Drawbotics","download_url":"https://codeload.github.com/Drawbotics/urql-computed-exchange/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227817177,"owners_count":17824199,"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-12-02T23:09:03.414Z","updated_at":"2024-12-02T23:09:03.927Z","avatar_url":"https://github.com/Drawbotics.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# URQL Computed Exchange\n\nAn [URQL](https://github.com/FormidableLabs/urql) exchange to compute data using resolvers and entities.\n\n## Installation\n\n```bash\n$ npm i urql-computed-exchange\n```\n\n## Usage\n\nFirst, create your entities and their resolvers:\n\n```javascript\n// entities.js\nimport { createEntity, mergeEntities } from 'urql-computed-exchange';\n\nconst Pokemon = createEntity('Pokemon', {\n  numberOfEvolutions: {\n    dependencies: gql`\n      fragment _ on Pokemon {\n        evolutions {\n          id\n        }\n      }\n    `,\n    resolver: (pokemon) =\u003e {\n      return (pokemon.evolutions \u0026\u0026 pokemon.evolutions.length) ?? 0;\n    },\n  },\n});\n\nexport default mergeEntities(Pokemon);\n```\n\nThen, add it to the list of exchanges in URQL when setting up the client:\n\n```javascript\n// client.js\n\nimport { computedExchange } from 'urql-computed-exchange';\nimport {\n  createClient,\n  cacheExchange,\n  dedupExchange,\n  fetchExchange,\n} from 'urql';\n\nimport entities from './entities';\n\n\nconst client = createClient({\n  url: 'https://graphql-pokemon.now.sh/',\n  exchanges: [\n    dedupExchange,\n    cacheExchange,\n    computedExchange({ entities }),\n    fetchExchange,\n  ],\n});\n\nexport default client;\n```\n\nFinally, use the `@computed` directive when declaring your GraphQL queries. Don't forget to indicate the corresponding `type`:\n\n```javascript\n// App.js\n\nimport React from 'react';\nimport { useQuery } from 'urql';\nimport gql from 'graphql-tag';\n\nconst PokemonQuery = gql`\n  query PokemonQuery {\n    pokemon(name: \"charmander\") {\n      id\n      name\n      numberOfEvolutions @computed(type: Pokemon)\n    }\n  }\n`;\n\nconst App = () =\u003e {\n  const [ res ] = useQuery({\n    query: PokemonQuery,\n  });\n\n  if (res.fetching) {\n    return 'Loading...';\n  }\n\n  return (\n    \u003cpre\u003e\n      {JSON.stringify(res.data, null, 2)}\n    \u003c/pre\u003e\n  );\n};\n\nexport default App;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrawbotics%2Furql-computed-exchange","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrawbotics%2Furql-computed-exchange","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrawbotics%2Furql-computed-exchange/lists"}