diff --git a/README.md b/README.md index 0641160..4030f32 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ GraphQL TypeScript Generator

+## Example + @@ -114,6 +116,84 @@ type IssuesQuery = () => {
From GraphQL
+## Installation + +```bash +npm install megaera +``` + +## Usage + +```bash +megaera --schema=schema.graphql ./src/**/*.graphql +``` + +Import generated code in your TypeScript file: + +```ts +import { IssuesQuery } from './query.graphql.js' + +const { issues } = await octokit.graphql>(IssuesQuery) +``` + +## FAQ + +### Why query string is copied to TypeScript file as well? + +To make it easier to import queries in TypeScript projects. As well to connect +generated output types with query source code. + +This allows for library authors to create a function that accepts a query, and +infers the return type from the query, as well as the types of the variables. + +For example, wrap [Octokit](https://github.com/octokit/octokit.js) in a function +that accepts a query and returns the result: + +```ts +import { Query, Variables } from 'megaera' +import { IssuesQuery } from './query.graphql.js' + +function query(query: T, variables?: Variables) { + return octokit.graphql>(query, variables) +} + +// Return type, and types of variables are inferred from the query. +const { issues } = await query(IssuesQuery, { login: 'webpod' }) +``` + +### Does Megaera support fragments? + +Yes, Megaera fully supports fragments. Fragments are generated as separate types, +and can be used independently. + +```graphql +query IssuesQuery($login: String) { + issues(login: $login) { + totalCount + nodes { + ...Issue + } + } +} + +fragment Issue on Issue { + number + author { + login + } + createdAt + closedAt +} +``` + +The generated TypeScript code will have a type `Issue` that can be used independently: + +```ts +import { Issue, IssuesQuery } from './query.graphql.js' + +const firstIssue: Issue = query(IssuesQuery).issues.nodes[0] +``` + ## License [MIT](LICENSE)