Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jul 10, 2024
1 parent 3a3215c commit d08c3cf
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<strong>GraphQL TypeScript Generator</strong>
</p>

## Example

<table>
<tr>
<th>From GraphQL</th>
Expand Down Expand Up @@ -114,6 +116,84 @@ type IssuesQuery = () => {
</tr>
</table>
## 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<ReturnType<IssuesQuery>>(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<T extends Query>(query: T, variables?: Variables<T>) {
return octokit.graphql<ReturnType<T>>(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)

0 comments on commit d08c3cf

Please sign in to comment.