Skip to content

Commit 4e2757b

Browse files
committed
Add README example for TypedDocumentString
1 parent 7e1adfe commit 4e2757b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,46 @@ Additionally, `GraphQlQueryResponseData` has been exposed to users:
260260
import type { GraphQlQueryResponseData } from "@octokit/graphql";
261261
```
262262

263+
### Usage with graphql-codegen
264+
265+
If your query is represented as a `String & DocumentTypeDecoration`, for example as [`TypedDocumentString` from graphql-codegen and its client preset](https://the-guild.dev/graphql/codegen/docs/guides/vanilla-typescript), then you can get type-safety for the query's parameters and return values.
266+
267+
Assuming you have configured graphql-codegen, with [GitHub's GraphQL schema](https://docs.github.com/en/graphql/overview/public-schema), and an output under `./graphql`:
268+
269+
```tsx
270+
import * as octokit from "@octokit/graphql";
271+
272+
// The graphql query factory from graphql-codegen's output
273+
import { graphql } from "./graphql/graphql.js";
274+
275+
const result = await octokit.graphql(
276+
graphql`
277+
query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
278+
repository(owner: $owner, name: $repo) {
279+
issues(last: $num) {
280+
edges {
281+
node {
282+
title
283+
}
284+
}
285+
}
286+
}
287+
}
288+
`,
289+
{
290+
owner: "octokit",
291+
repo: "graphql.js",
292+
headers: {
293+
authorization: `token secret123`,
294+
},
295+
},
296+
// ^ parameters are required; owner and repo are type-checked
297+
);
298+
299+
type Return = typeof result;
300+
// ^? { repository: {issues: {edges: Array<{node: {title: string}>}} }
301+
```
302+
263303
## Errors
264304

265305
In case of a GraphQL error, `error.message` is set to a combined message describing all errors returned by the endpoint.

0 commit comments

Comments
 (0)