@@ -155,13 +155,55 @@ the return type of the query, and the types of the input variables.
155
155
type Result = ReturnType<IssuesQuery>
156
156
` ` `
157
157
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
+ ` ` `
165
207
166
208
</details>
167
209
@@ -178,8 +220,8 @@ For example, wrap [Octokit](https://github.com/octokit/octokit.js) in a function
178
220
that accepts a query and returns the result:
179
221
180
222
` ` ` 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 '
183
225
184
226
function query<T extends Query >(query : T , variables ? : Variables <T >) {
185
227
return octokit .graphql <ReturnType <T >>(query , variables )
@@ -220,7 +262,7 @@ fragment Issue on Issue {
220
262
The generated TypeScript code will have a type ` Issue ` that can be used independently:
221
263
222
264
` ` ` ts
223
- import {Issue, IssuesQuery} from './query.graphql.js '
265
+ import { Issue , IssuesQuery } from ' ./query.graphql.ts '
224
266
225
267
const firstIssue : Issue = query (IssuesQuery ).issues .nodes [0 ]
226
268
```
0 commit comments