Skip to content

Commit 8c970ed

Browse files
committed
Update README.md
1 parent 2d31fa7 commit 8c970ed

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

README.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,55 @@ the return type of the query, and the types of the input variables.
155155
type Result = ReturnType<IssuesQuery>
156156
```
157157

158-
> [!NOTE]
159-
> The generated TypeScript code will have a type `IssuesQuery` that can be used
160-
> independently:
161-
>
162-
> ```ts
163-
> import type { IssuesQuery } from './query.graphql.ts'
164-
> ```
158+
The type `IssuesQuery` can also be used independently:
159+
160+
```ts
161+
import type { IssuesQuery } from './query.graphql.ts'
162+
```
163+
164+
</details>
165+
166+
<details>
167+
<summary><strong>How to get the return type of a query?</strong></summary>
168+
169+
Megaera generates TypeScript types for queries as functions.
170+
171+
```ts
172+
type UserQuery = (vars: { login?: string }) => {
173+
user: {
174+
login: string
175+
avatarUrl: string
176+
name: string
177+
}
178+
}
179+
```
180+
181+
To get the return type of a query, use the `ReturnType` utility type:
182+
183+
```ts
184+
type Result = ReturnType<UserQuery>
185+
```
186+
187+
</details>
188+
189+
<details>
190+
<summary><strong>How to get the types of the variables of a query?</strong></summary>
191+
192+
The first parameter of the query function is the variables.
193+
194+
You can use TypeScript's `Parameters` utility type to get the types of the variables:
195+
196+
```ts
197+
type Variables = Parameters<UserQuery>[0]
198+
```
199+
200+
Or you can use the `Variables` utility type to get the types of the variables:
201+
202+
```ts
203+
import { Variables } from 'megaera'
204+
205+
type Variables = Variables<UserQuery>
206+
```
165207
166208
</details>
167209
@@ -178,8 +220,8 @@ For example, wrap [Octokit](https://github.com/octokit/octokit.js) in a function
178220
that accepts a query and returns the result:
179221
180222
```ts
181-
import {Query, Variables} from 'megaera'
182-
import {IssuesQuery} from './query.graphql.js'
223+
import { Query, Variables } from 'megaera'
224+
import { IssuesQuery } from './query.graphql.ts'
183225

184226
function query<T extends Query>(query: T, variables?: Variables<T>) {
185227
return octokit.graphql<ReturnType<T>>(query, variables)
@@ -220,7 +262,7 @@ fragment Issue on Issue {
220262
The generated TypeScript code will have a type `Issue` that can be used independently:
221263
222264
```ts
223-
import {Issue, IssuesQuery} from './query.graphql.js'
265+
import { Issue, IssuesQuery } from './query.graphql.ts'
224266

225267
const firstIssue: Issue = query(IssuesQuery).issues.nodes[0]
226268
```

0 commit comments

Comments
 (0)