{"id":13727390,"url":"https://github.com/mpgon/apollo-type-patcher","last_synced_at":"2025-10-25T00:30:47.318Z","repository":{"id":57182401,"uuid":"173587615","full_name":"mpgon/apollo-type-patcher","owner":"mpgon","description":"Type patcher for Apollo's apollo-link-rest","archived":false,"fork":false,"pushed_at":"2019-07-14T20:36:28.000Z","size":263,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-31T04:32:01.275Z","etag":null,"topics":["apollo","apollo-link","apollo-link-rest","graqphql","patcher","patching","type","type-definition","type-definitions","typename","typepatcher"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mpgon.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-03-03T14:44:54.000Z","updated_at":"2021-02-07T09:46:23.000Z","dependencies_parsed_at":"2022-09-14T06:00:32.358Z","dependency_job_id":null,"html_url":"https://github.com/mpgon/apollo-type-patcher","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/mpgon%2Fapollo-type-patcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpgon%2Fapollo-type-patcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpgon%2Fapollo-type-patcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpgon%2Fapollo-type-patcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mpgon","download_url":"https://codeload.github.com/mpgon/apollo-type-patcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238046852,"owners_count":19407625,"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":["apollo","apollo-link","apollo-link-rest","graqphql","patcher","patching","type","type-definition","type-definitions","typename","typepatcher"],"created_at":"2024-08-03T01:03:53.272Z","updated_at":"2025-10-25T00:30:41.922Z","avatar_url":"https://github.com/mpgon.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# apollo-type-patcher\nUtility to generate **Type Patcher** functions for [apollo-link-rest](https://github.com/apollographql/apollo-link-rest).\n\n[![Build Status](https://dev.azure.com/miguelgopereira/apollo-type-patcher/_apis/build/status/mpgon.apollo-type-patcher?branchName=master)](https://dev.azure.com/miguelgopereira/apollo-type-patcher/_build/latest?definitionId=2\u0026branchName=master)\n![npm bundle size](https://img.shields.io/bundlephobia/min/apollo-type-patcher.svg)\n![npm](https://img.shields.io/npm/v/apollo-type-patcher.svg?label=latest)\n![npm](https://img.shields.io/npm/dt/apollo-type-patcher.svg)\n\nJump to: [Goal](#goal) | [Installation](#installation) | [Usage](#usage) | [How it works](#how-it-works) | [Features](#features) | [Contribute](#contribute)\n\n## Goal\nThe [apollo-link-rest](https://github.com/apollographql/apollo-link-rest) library enables a smooth, frontend-first transition into GraphQL, by allowing you to leverage this query language in a project powered by a REST API (read some of the reasons [here](https://www.apollographql.com/docs/link/links/rest.html)).\nHowever, since you don't have a schema for your _type definitions_ (because you don't have a GraphQL server), you have to generate the **type patcher** functions [yourself](https://www.apollographql.com/docs/link/links/rest.html#options.typePatcher). This is a very verbose, time consuming and error-prone process, specially if you're trying to consume endpoints with a lot of nested fields whose types you want to normalize into the cache. \n\nThe goal of this `apollo-type-patcher` library is to generate the **type patcher** functions easily in a safe, maintainable way, with an object contaiting your type definition mappings.\n\n\n## Installation\nTo add this library to your project's `dependencies` simply run:\n```\nyarn add apollo-type-patcher\n```\n\n## Usage\n\u003e [Try it out in the browser](https://codesandbox.io/s/jll0xolo49?fontsize=14)\n\n1. Create your type definitions object. This object is in the form of\n```javascript\ntypeDefinitions = {\n    TYPE: {\n        FIELD: TYPE,\n        ...\n    },\n    ...\n}\n```\nfor example:\n```javascript\n// typeDefinitions.js\nexport const typeDefinitions = {\n    Student: {\n        classes: \"Class\",\n        extra_activities: \"Activities\"\n    },\n    Class: {\n        chair_professor: \"Professor\"\n    }\n}\n```\n2. When setting up your Apollo link rest configuration, add the type patcher as follows:\n```jsx\nimport { typePatcher } from \"apollo-type-patcher\";\nimport { RestLink } from \"apollo-link-rest\";\n// your type definitions\nimport { typeDefinitions } from \"./typeDefinitions\";\n\nconst restLink = new RestLink({\n  typePatcher: typePatcher(typeDefinitions),\n  ...\n});\n```\n\n## How it works\nThe type patcher adds the typename property to nested types, since the root type is injected directly by Apollo:\n```jsx\n// for the Query:\nconst GET_STUDENT = gql`\n  query getStudent($id: ID!) {\n    student(id: $id) @rest(type: \"Student\", path: \"student/{args.id}\") {\n      id\n      degree {\n        id\n      }\n    }\n  }\n`;\n\n// and for the type defintions:\ntypeDefinitions = {\n    Student: {\n        degree: Degree,\n    },\n}\n\n// the output of the Apollo request (pre type patching) is:\nstudent = {\n    id: 60,\n    degree: {\n        id: 5,\n    },\n    __typename: \"Student\" // \u003c- Added by Apollo directly\n}\n\n// the final of the Query (pos type patching) is:\nstudent = {\n    id: 60,\n    degree: {\n        id: 5,\n        __typename: \"Degree\" // \u003c- Added by the typePatcher\n    },\n    __typename: \"Student\"\n}\n```\n\n## Features\n- add types in nested objects\n```javascript\n// typeDef\nStudent: {\n    class: \"Class\"\n}\n// out\nstudent: {\n    class: {\n        __typename: \"Class\"\n    }\n}\n```\n- add types in nested arrays\n```javascript\n// typeDef\nStudent: {\n    topics: \"Topic\"\n}\n// out\nstudent: {\n    topics: [{\n        __typename: \"Topic\"\n    }, ...]\n}\n```\n- add types in deeply nested properties\n```javascript\n// typeDef\nStudent: {\n    \"needs.medical_needs.insurance\": \"Insurance\"\n}\n// out\nstudent: {\n    needs: {\n        medical_needs: {\n            insurance: {\n                __typename: \"Insurance\"\n            }\n        }\n    }\n}\n```\n\n## Contribute\nAfter cloning the repo locally, you can run:\n```node\nyarn // install dependencies\nyarn build // build the bundle w/webpack\nyarn test // run unit tests\n```\nDone with the support of [@cgcote](https://github.com/cgcote) and [Recare](https://github.com/veyo-care)!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpgon%2Fapollo-type-patcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmpgon%2Fapollo-type-patcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpgon%2Fapollo-type-patcher/lists"}