{"id":20648700,"url":"https://github.com/webpod/graphql-megaera","last_synced_at":"2025-04-07T17:10:39.857Z","repository":{"id":247284305,"uuid":"825463498","full_name":"webpod/graphql-megaera","owner":"webpod","description":"GraphQL to TypeScript Generator","archived":false,"fork":false,"pushed_at":"2024-12-01T21:30:53.000Z","size":73,"stargazers_count":42,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-31T14:15:28.987Z","etag":null,"topics":["code-generation","graphql","graphql-codegen","typescript"],"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/webpod.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":"2024-07-07T21:02:42.000Z","updated_at":"2025-03-30T00:52:48.000Z","dependencies_parsed_at":"2025-02-07T13:11:16.293Z","dependency_job_id":"ab39c351-5883-4175-9991-b6709f2c47d1","html_url":"https://github.com/webpod/graphql-megaera","commit_stats":null,"previous_names":["webpod/graphql-megaera"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpod%2Fgraphql-megaera","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpod%2Fgraphql-megaera/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpod%2Fgraphql-megaera/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpod%2Fgraphql-megaera/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webpod","download_url":"https://codeload.github.com/webpod/graphql-megaera/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247694876,"owners_count":20980733,"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":["code-generation","graphql","graphql-codegen","typescript"],"created_at":"2024-11-16T17:10:00.587Z","updated_at":"2025-04-07T17:10:39.834Z","avatar_url":"https://github.com/webpod.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GraphQL Megaera\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\".github/graphql-megaera.svg\" width=\"220\" alt=\"GraphQL Megaera\"\u003e\u003cbr\u003e\n  \u003cstrong\u003eGraphQL to TypeScript Generator\u003c/strong\u003e\u003cbr\u003e\u003cbr\u003e\n  \u003ca href=\"https://www.npmjs.com/package/megaera\"\u003e\u003cimg src=\"https://badgen.net/npm/v/megaera\" alt=\"npm\"\u003e\u003c/a\u003e \u003ca href=\"https://github.com/webpod/graphql-megaera/actions/workflows/test.yml\"\u003e\u003cimg src=\"https://github.com/webpod/graphql-megaera/actions/workflows/test.yml/badge.svg?branch=main\" alt=\"test\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## Example\n\n\u003ctable align=\"center\"\u003e\n\u003ctr\u003e\n  \u003cth\u003eFrom GraphQL\u003c/th\u003e\n  \u003cth\u003eTo TypeScript\u003c/th\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\n\n```graphql\nquery IssuesQuery {\n  issues(first: 100) {\n    totalCount\n    nodes {\n      createdAt\n      closedAt\n      closed\n      author {\n        login\n      }\n      number\n      title\n      labels(first: 10) {\n        totalCount\n        nodes {\n          name\n        }\n      }\n      body\n      comments(first: 10) {\n        totalCount\n        nodes {\n          body\n        }\n      }\n      repository {\n        owner {\n          login\n        }\n        name\n      }\n    }\n    pageInfo {\n      hasNextPage\n      endCursor\n    }\n  }\n  rateLimit {\n    limit\n    cost\n    remaining\n    resetAt\n  }\n}\n```\n\n\u003c/td\u003e\n\u003ctd\u003e\n\n```ts\ntype IssuesQuery = () =\u003e {\n  issues: {\n    totalCount: number\n    nodes: Array\u003c{\n      createdAt: string\n      closedAt: string | null\n      closed: boolean\n      author: {\n        login: string\n      }\n      number: number\n      title: string\n      labels: {\n        totalCount: number\n        nodes: Array\u003c{\n          name: string\n        }\u003e\n      }\n      body: string\n      comments: {\n        totalCount: number\n        nodes: Array\u003c{\n          body: string\n        }\u003e\n      }\n      repository: {\n        owner: {\n          login: string\n        }\n        name: string\n      }\n    }\u003e\n    pageInfo: {\n      hasNextPage: boolean\n      endCursor: string | null\n    }\n  }\n  rateLimit: {\n    limit: number\n    cost: number\n    remaining: number\n    resetAt: string\n  }\n}\n```\n\n  \u003c/td\u003e\n\u003c/tr\u003e\n\u003c/table\u003e\n\n## Installation\n\n```bash\nnpm install megaera\n```\n\n## Usage\n\n```bash\nmegaera --schema=schema.graphql ./src/**/*.graphql\n```\n\nMegaera will generate TypeScript code for all queries in the specified files.\n\n## FAQ\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eHow to use Megaera?\u003c/strong\u003e\u003c/summary\u003e\n\nPut your queries in `.graphql` files, and run `megaera` to generate TypeScript code from them.\n\nMegaera will copy the query string to the generated TypeScript file, so you can\nimport it in your TypeScript code.\n\n```ts\nimport { IssuesQuery } from './query.graphql.ts'\n```\n\nThe `IssuesQuery` variable is a string with the GraphQL query. You can use it\ndirectly in your code, or pass it to a function that accepts a query.\n\nAlso, `IssuesQuery` carries the type of the query, so you can use it to infer\nthe return type of the query, and the types of the input variables.\n\n```ts\ntype Result = ReturnType\u003cIssuesQuery\u003e\n```\n\nThe type `IssuesQuery` can also be used independently:\n\n```ts\nimport type { IssuesQuery } from './query.graphql.ts'\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eHow to get the return type of a query?\u003c/strong\u003e\u003c/summary\u003e\n\nMegaera generates TypeScript types for queries as functions.\n\n```ts\ntype UserQuery = (vars: { login?: string }) =\u003e {\n  user: {\n    login: string\n    avatarUrl: string\n    name: string\n  }\n}\n```\n\nTo get the return type of a query, use the `ReturnType` utility type:\n\n```ts\ntype Result = ReturnType\u003cUserQuery\u003e\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eHow to get the types of the variables of a query?\u003c/strong\u003e\u003c/summary\u003e\n\nThe first parameter of the query function is the variables.\n\nYou can use TypeScript's `Parameters` utility type to get the types of the variables:\n\n```ts\ntype Variables = Parameters\u003cUserQuery\u003e[0]\n```\n\nOr you can use the `Variables` utility type to get the types of the variables:\n\n```ts\nimport { Variables } from 'megaera'\n\ntype Variables = Variables\u003cUserQuery\u003e\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eWhy query string is copied to TypeScript file as well?\u003c/strong\u003e\u003c/summary\u003e\n\nTo make it easier to import queries in TypeScript projects. As well to connect\ngenerated output types with query source code.\n\nThis allows for library authors to create a function that accepts a query, and\ninfers the return type from the query, as well as the types of the variables.\n\nFor example, wrap [Octokit](https://github.com/octokit/octokit.js) in a function\nthat accepts a query and returns the result:\n\n```ts\nimport { Query, Variables } from 'megaera'\nimport { IssuesQuery } from './query.graphql.ts'\n\nfunction query\u003cT extends Query\u003e(query: T, variables?: Variables\u003cT\u003e) {\n  return octokit.graphql\u003cReturnType\u003cT\u003e\u003e(query, variables)\n}\n\n// Return type, and types of variables are inferred from the query.\nconst { issues } = await query(IssuesQuery, { login: 'webpod' })\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eDoes Megaera support fragments?\u003c/strong\u003e\u003c/summary\u003e\n\nYes, Megaera fully supports fragments. Fragments are generated as separate types,\nand can be used independently.\n\n```graphql\nquery IssuesQuery($login: String) {\n  issues(login: $login) {\n    totalCount\n    nodes {\n      ...Issue\n    }\n  }\n}\n\nfragment Issue on Issue {\n  number\n  author {\n    login\n  }\n  createdAt\n  closedAt\n}\n```\n\nThe generated TypeScript code will have a type `Issue` that can be used independently:\n\n```ts\nimport { Issue, IssuesQuery } from './query.graphql.ts'\n\nconst firstIssue: Issue = query(IssuesQuery).issues.nodes[0]\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eCan Megaera extract queries from `.ts` files?\u003c/strong\u003e\u003c/summary\u003e\n\nNo. To simplify development of Megaera, it is only possible to extract queries\nfrom `.graphql` files.\n\nBut it should be possible to create plugins for webpack, rollup, or other\nbundlers that can extract queries from `.ts` files. If you are interested in\nthis, please open an issue.\n\n\u003c/details\u003e\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpod%2Fgraphql-megaera","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebpod%2Fgraphql-megaera","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpod%2Fgraphql-megaera/lists"}