Skip to content

Commit 75dff97

Browse files
committed
Add String & DocumentTypeDecoration overload
1 parent 1fffe01 commit 75dff97

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"author": "Gregor Martynus (https://github.com/gr2m)",
2525
"license": "MIT",
2626
"dependencies": {
27+
"@graphql-typed-document-node/core": "^3.2.0",
2728
"@octokit/request": "^9.0.0",
2829
"@octokit/types": "^13.0.0",
2930
"universal-user-agent": "^7.0.0"

src/types.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type {
44
EndpointInterface,
55
} from "@octokit/types";
66

7+
import type { DocumentTypeDecoration } from "@graphql-typed-document-node/core";
8+
79
import type { graphql } from "./graphql.js";
810

911
export type GraphQlEndpointOptions = EndpointOptions & {
@@ -33,6 +35,25 @@ export interface graphql {
3335
parameters?: RequestParameters,
3436
): GraphQlResponse<ResponseData>;
3537

38+
/**
39+
* Sends a GraphQL query request based on endpoint options. The query parameters are type-checked
40+
*
41+
* @param {String & DocumentTypeDecoration<ResponseData, QueryVariables>} query GraphQL query. Example: `'query { viewer { login } }'`.
42+
* @param {object} [parameters] URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
43+
*/
44+
<ResponseData, QueryVariables>(
45+
query: String & DocumentTypeDecoration<ResponseData, QueryVariables>,
46+
/**
47+
* The tuple in rest parameters allows makes RequestParameters conditionally
48+
* optional , if the query does not require any variables.
49+
*
50+
* @see https://github.com/Microsoft/TypeScript/pull/24897#:~:text=not%20otherwise%20observable).-,Optional%20elements%20in%20tuple%20types,-Tuple%20types%20now
51+
*/
52+
...[parameters]: QueryVariables extends Record<string, never>
53+
? [RequestParameters?]
54+
: [QueryVariables & RequestParameters]
55+
): GraphQlResponse<ResponseData>;
56+
3657
/**
3758
* Returns a new `endpoint` with updated route and parameters
3859
*/

src/with-defaults.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,36 @@ import type {
55
RequestParameters,
66
} from "./types.js";
77
import { graphql } from "./graphql.js";
8+
import type { DocumentTypeDecoration } from "@graphql-typed-document-node/core";
9+
10+
/**
11+
* Allows discarding the DocumentTypeDecoration information from the external
12+
* interface, and casting the String part to string. This avoids the external
13+
* API interface spreading to the internal types, while keeping type-safety for
14+
* future extensions/changes.
15+
*/
16+
type DiscardTypeDecoration<T> = T extends String &
17+
DocumentTypeDecoration<unknown, unknown>
18+
? string
19+
: T;
820

921
export function withDefaults(
1022
request: typeof Request,
1123
newDefaults: RequestParameters,
1224
): ApiInterface {
1325
const newRequest = request.defaults(newDefaults);
1426
const newApi = <ResponseData>(
15-
query: Query | RequestParameters,
27+
query:
28+
| Query
29+
| (String & DocumentTypeDecoration<unknown, unknown>)
30+
| RequestParameters,
1631
options?: RequestParameters,
1732
) => {
18-
return graphql<ResponseData>(newRequest, query, options);
33+
return graphql<ResponseData>(
34+
newRequest,
35+
query as DiscardTypeDecoration<typeof query>,
36+
options,
37+
);
1938
};
2039

2140
return Object.assign(newApi, {

0 commit comments

Comments
 (0)