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 13, 2024
1 parent 107ae60 commit 2d31fa7
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## Example

<table>
<table align="center">
<tr>
<th>From GraphQL</th>
<th>To TypeScript</th>
Expand Down Expand Up @@ -83,7 +83,7 @@ type IssuesQuery = () => {
totalCount: number
nodes: Array<{
name: string
}>
}>
}
body: string
comments: {
Expand Down Expand Up @@ -129,26 +129,41 @@ npm install megaera
megaera --schema=schema.graphql ./src/**/*.graphql
```
Import generated code in your TypeScript file:
Megaera will generate TypeScript code for all queries in the specified files.
```ts
import { IssuesQuery } from './query.graphql.js'
## FAQ
// Use ReturnType to get the result type of the query.
type Result = ReturnType<IssuesQuery>
<details>
<summary><strong>How to use Megaera?</strong></summary>
// IssuesQuery is a string with GraphQL query.
typeof IssuesQuery === 'string'
```
Put your queries in `.graphql` files, and run `megaera` to generate TypeScript code from them.
Megaera will copy the query string to the generated TypeScript file, so you can
import it in your TypeScript code.
```ts
// Use Variables to get the variables type of the query.
import { Variables } from 'megaera'
import { IssuesQuery } from './query.graphql.ts'
````

The `IssuesQuery` variable is a string with the GraphQL query. You can use it
directly in your code, or pass it to a function that accepts a query.

type InputVariables = Variables<IssuesQuery>
Also, `IssuesQuery` carries the type of the query, so you can use it to infer
the return type of the query, and the types of the input variables.

```ts
type Result = ReturnType<IssuesQuery>
```

## FAQ
> [!NOTE]
> The generated TypeScript code will have a type `IssuesQuery` that can be used
> independently:
>
> ```ts
> import type { IssuesQuery } from './query.graphql.ts'
> ```

</details>

<details>
<summary><strong>Why query string is copied to TypeScript file as well?</strong></summary>
Expand All @@ -163,15 +178,15 @@ 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'
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' })
const {issues} = await query(IssuesQuery, {login: 'webpod'})
```

</details>
Expand Down Expand Up @@ -205,14 +220,13 @@ fragment Issue on Issue {
The generated TypeScript code will have a type `Issue` that can be used independently:

```ts
import { Issue, IssuesQuery } from './query.graphql.js'
import {Issue, IssuesQuery} from './query.graphql.js'
const firstIssue: Issue = query(IssuesQuery).issues.nodes[0]
```

</details>


## License

[MIT](LICENSE)

0 comments on commit 2d31fa7

Please sign in to comment.