Skip to content

Commit

Permalink
feat(types): expose GraphQlQueryResponseData type (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscard0m authored Feb 2, 2021
1 parent 9ddc5b8 commit f697627
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- [Pass query together with headers and variables](#pass-query-together-with-headers-and-variables)
- [Use with GitHub Enterprise](#use-with-github-enterprise)
- [Use custom `@octokit/request` instance](#use-custom-octokitrequest-instance)
- [TypeScript](#typescript)
- [Additional Types](#additional-types)
- [Errors](#errors)
- [Partial responses](#partial-responses)
- [Writing tests](#writing-tests)
Expand Down Expand Up @@ -247,6 +249,18 @@ await myGraphql(`
// requestCounter is now 2
```

## TypeScript

`@octokit/graphql` is exposing proper types for its usage with TypeScript projects.

### Additional Types

Additionally, `GraphQlQueryResponseData` has been exposed to users:

```ts
import type { GraphQlQueryResponseData } from "octokit/graphql";
```

## Errors

In case of a GraphQL error, `error.message` is set to the first error from the response’s `errors` array. All errors can be accessed at `error.errors`. `error.request` has the request options such as query, variables and headers set for easier debugging.
Expand All @@ -269,14 +283,14 @@ try {
} catch (error) {
// server responds with
// {
// "data": null,
// "errors": [{
// "message": "Field 'bioHtml' doesn't exist on type 'User'",
// "locations": [{
// "line": 3,
// "column": 5
// }]
// }]
// "data": null,
// "errors": [{
// "message": "Field 'bioHtml' doesn't exist on type 'User'",
// "locations": [{
// "line": 3,
// "column": 5
// }]
// }]
// }

console.log("Request failed:", error.request); // { query, variables: {}, headers: { authorization: 'token secret123' } }
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const graphql = withDefaults(request, {
url: "/graphql",
});

export { GraphQlQueryResponseData } from "./types";

export function withCustomRequest(customRequest: typeof request) {
return withDefaults(customRequest, {
method: "POST",
Expand Down

0 comments on commit f697627

Please sign in to comment.