@@ -5,9 +5,25 @@ import type * as OctokitTypes from "@octokit/types";
55import { graphql } from "../src" ;
66import { VERSION } from "../src/version" ;
77import type { RequestParameters } from "../src/types" ;
8+ import type { DocumentTypeDecoration } from "@graphql-typed-document-node/core" ;
89
910const userAgent = `octokit-graphql.js/${ VERSION } ${ getUserAgent ( ) } ` ;
1011
12+ class TypedDocumentString < TResult , TVariables >
13+ extends String
14+ implements DocumentTypeDecoration < TResult , TVariables >
15+ {
16+ __apiType ?: DocumentTypeDecoration < TResult , TVariables > [ "__apiType" ] ;
17+
18+ constructor ( private value : string ) {
19+ super ( value ) ;
20+ }
21+
22+ toString ( ) : string & DocumentTypeDecoration < TResult , TVariables > {
23+ return this . value ;
24+ }
25+ }
26+
1127describe ( "graphql()" , ( ) => {
1228 it ( "is a function" , ( ) => {
1329 expect ( graphql ) . toBeInstanceOf ( Function ) ;
@@ -75,6 +91,72 @@ describe("graphql()", () => {
7591 } ) ;
7692 } ) ;
7793
94+ it ( "README TypedDocumentString example" , ( ) => {
95+ const mockData = {
96+ repository : {
97+ issues : {
98+ edges : [
99+ {
100+ node : {
101+ title : "Foo" ,
102+ } ,
103+ } ,
104+ {
105+ node : {
106+ title : "Bar" ,
107+ } ,
108+ } ,
109+ {
110+ node : {
111+ title : "Baz" ,
112+ } ,
113+ } ,
114+ ] ,
115+ } ,
116+ } ,
117+ } ;
118+
119+ const RepositoryDocument = new TypedDocumentString <
120+ {
121+ repository : { issues : { edges : Array < { node : { title : string } } > } } ;
122+ } ,
123+ Record < string , never >
124+ > ( /* GraphQL */ `
125+ {
126+ repository(owner: "octokit", name: "graphql.js") {
127+ issues(last: 3) {
128+ edges {
129+ node {
130+ title
131+ }
132+ }
133+ }
134+ }
135+ }
136+ ` ) ;
137+
138+ return graphql ( RepositoryDocument , {
139+ headers : {
140+ authorization : `token secret123` ,
141+ } ,
142+ request : {
143+ fetch : fetchMock . sandbox ( ) . post (
144+ "https://api.github.com/graphql" ,
145+ { data : mockData } ,
146+ {
147+ headers : {
148+ accept : "application/vnd.github.v3+json" ,
149+ authorization : "token secret123" ,
150+ "user-agent" : userAgent ,
151+ } ,
152+ } ,
153+ ) ,
154+ } ,
155+ } ) . then ( ( result ) => {
156+ expect ( JSON . stringify ( result ) ) . toStrictEqual ( JSON . stringify ( mockData ) ) ;
157+ } ) ;
158+ } ) ;
159+
78160 it ( "Variables" , ( ) => {
79161 const query = `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
80162 repository(owner:$owner, name:$repo) {
@@ -114,6 +196,54 @@ describe("graphql()", () => {
114196 } ) ;
115197 } ) ;
116198
199+ it ( "Variables with TypedDocumentString" , ( ) => {
200+ const query = new TypedDocumentString <
201+ {
202+ repository : { issues : { edges : Array < { node : { title : string } } > } } ;
203+ } ,
204+ {
205+ owner : string ;
206+ repo : string ;
207+ num ?: number ;
208+ }
209+ > ( `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
210+ repository(owner:$owner, name:$repo) {
211+ issues(last:$num) {
212+ edges {
213+ node {
214+ title
215+ }
216+ }
217+ }
218+ }
219+ }` ) ;
220+
221+ return graphql ( query , {
222+ headers : {
223+ authorization : `token secret123` ,
224+ } ,
225+ owner : "octokit" ,
226+ repo : "graphql.js" ,
227+ request : {
228+ fetch : fetchMock
229+ . sandbox ( )
230+ . post (
231+ "https://api.github.com/graphql" ,
232+ ( _url , options : OctokitTypes . RequestOptions ) => {
233+ const body = JSON . parse ( options . body ) ;
234+ expect ( body . query ) . toEqual ( query . toString ( ) ) ;
235+ expect ( body . variables ) . toStrictEqual ( {
236+ owner : "octokit" ,
237+ repo : "graphql.js" ,
238+ } ) ;
239+
240+ return { data : { } } ;
241+ } ,
242+ ) ,
243+ } ,
244+ } ) ;
245+ } ) ;
246+
117247 it ( "Pass headers together with variables as 2nd argument" , ( ) => {
118248 const query = `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
119249 repository(owner:$owner, name:$repo) {
0 commit comments