{"id":19273955,"url":"https://github.com/rintoj/gql-hook-codegen","last_synced_at":"2026-02-03T22:04:02.853Z","repository":{"id":53345280,"uuid":"369939235","full_name":"rintoj/gql-hook-codegen","owner":"rintoj","description":null,"archived":false,"fork":false,"pushed_at":"2024-05-28T12:17:38.000Z","size":515,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-23T03:30:44.933Z","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/rintoj.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-23T01:41:46.000Z","updated_at":"2024-05-28T12:17:00.000Z","dependencies_parsed_at":"2024-05-28T15:12:03.127Z","dependency_job_id":null,"html_url":"https://github.com/rintoj/gql-hook-codegen","commit_stats":{"total_commits":76,"total_committers":1,"mean_commits":76.0,"dds":0.0,"last_synced_commit":"ee23be3ef2f00a1f89b09c72cc5d905db23ef04f"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rintoj%2Fgql-hook-codegen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rintoj%2Fgql-hook-codegen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rintoj%2Fgql-hook-codegen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rintoj%2Fgql-hook-codegen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rintoj","download_url":"https://codeload.github.com/rintoj/gql-hook-codegen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245188263,"owners_count":20574734,"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-09T20:44:38.215Z","updated_at":"2026-02-03T22:03:57.796Z","avatar_url":"https://github.com/rintoj.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gql-hook-codegen\n\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n\nThis tool generates TypeScript types for queries/mutations written in an GraphQL project given a\nvalid GraphQL schema.\n\n![hook-generation-git](https://github.com/rintoj/gql-hook-codegen/assets/10824903/b254ea6c-a813-4b42-828d-8f78a4d3eb3b)\n\n## Install\n\n### Yarn\n\n```sh\nyarn add gql-hook-codegen\n```\n\n### NPM\n\n```sh\nnpm install gql-hook-codegen\n```\n\n## How to use\n\nStep 1: Create a Typescript file `use-user.gql.ts` with the following content\n\n```ts\nimport gql from 'graphql-tag'\n\nconst query = gql`\n  query {\n    user {\n      name\n    }\n  }\n`\n```\n\nStep 2: Add schema file `schema.gql`\n\n```gql\ntype User {\n  id: ID!\n  name: String\n}\n\ntype Query {\n  user(id: ID!): User\n}\n```\n\nStep 3: Run the following code:\n\n```sh\nnpx gql-hook-codegen generate --pattern=\"*.gql.ts\"\n```\n\nStep 4: Script will update `use-user.gql.ts` to the following:\n\n```ts\nimport { QueryHookOptions, useQuery } from '@apollo/client'\nimport gql from 'graphql-tag'\n\nconst query = gql`\n  query fetchUser($id: ID!) {\n    user(id: $id) {\n      name\n    }\n  }\n`\n\nexport interface RequestType {\n  id: string | undefined\n}\n\nexport interface QueryType {\n  user?: UserType\n}\n\nexport interface UserType {\n  name?: string\n  __typename?: 'User'\n}\n\nexport function useUserQuery(\n  request: RequestType,\n  options?: QueryHookOptions\u003cQueryType, RequestType\u003e,\n) {\n  return useQuery\u003cQueryType, RequestType\u003e(query, {\n    variables: request,\n    skip: !request.id,\n    ...options,\n  })\n}\n```\n\n## VS Code Integration\n\nInstall [Save and Run](https://marketplace.visualstudio.com/items?itemName=wk-j.save-and-run) plugin\nand add the following code to `.settings.json`\n\n```json\n{\n  \"saveAndRun\": {\n    \"commands\": [\n      {\n        \"match\": \".gql.ts\",\n        \"cmd\": \"npx gql-hook-codegen generate --schema-file=../partner-portal-be/schema.gql --pattern='${file}'\",\n        \"useShortcut\": false,\n        \"silent\": false\n      }\n    ]\n  }\n}\n```\n\n## More Examples\n\n\u003c!-- vscode-markdown-toc --\u003e\n\n1. [Schema](./docs/examples.md#Schema)\n2. [Query](./docs/examples.md#Query)\n3. [Query with no parameters](./docs/examples.md#Querywithnoparameters)\n4. [Batched Queries](./docs/examples.md#BatchedQueries)\n5. [Query with multiple inputs](./docs/examples.md#Querywithmultipleinputs)\n6. [Query with enum](./docs/examples.md#Querywithenum)\n7. [Query with date](./docs/examples.md#Querywithdate)\n8. [Query with shared variable](./docs/examples.md#Querywithsharedvariable)\n9. [Mutation](./docs/examples.md#Mutation)\n10. [Lazy query](./docs/examples.md#Lazyquery)\n11. [Query with union](./docs/examples.md#Querywithunion)\n\n## Options\n\n```sh\ngql-hook-codegen   \u003cgenerate\u003e [--help] [--doc]\n\nCOMMANDS\n\ngenerate\n\nCOMMON\n\n--help     Show help\n\n--doc      Generate documentation\n\n```\n\n## gql-hook-codegen generate\n\n```sh\n\ngql-hook-codegen generate   [--pattern=\u003cstring\u003e]\n                                      [--file=\u003cstring\u003e]\n                                      [--schema-file=\u003cstring\u003e]\n                                      [--schema-url=\u003cstring\u003e]\n                                      [--ignore=\u003cstring\u003e]\n                                      [--package=\u003cstring\u003e]\n                                      [--save]\n\nOPTIONS\n\n--pattern=\u003cstring\u003e       File pattern\n\n--file=\u003cstring\u003e          A specific file to process\n\n--schema-file=\u003cstring\u003e   Location of the schema file\n\n--schema-url=\u003cstring\u003e    Url to fetch graphql schema from\n\n--ignore=\u003cstring\u003e        Folders to ignore\n\n--package=\u003cstring\u003e       Default package to use\n\n--save                   Save schema locally if --schema-url is\n                         used\n\n```\n\n## Automatic Release\n\nHere is an example of the release type that will be done based on a commit messages:\n\n| Commit message      | Release type          |\n| ------------------- | --------------------- |\n| fix: [comment]      | Patch Release         |\n| feat: [comment]     | Minor Feature Release |\n| perf: [comment]     | Major Feature Release |\n| doc: [comment]      | No Release            |\n| refactor: [comment] | No Release            |\n| chore: [comment]    | No Release            |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frintoj%2Fgql-hook-codegen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frintoj%2Fgql-hook-codegen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frintoj%2Fgql-hook-codegen/lists"}