@@ -5,9 +5,25 @@ import { getUserAgent } from "universal-user-agent";
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 ) ;
@@ -77,6 +93,72 @@ describe("graphql()", () => {
7793 } ) ;
7894 } ) ;
7995
96+ it ( "README TypedDocumentString example" , ( ) => {
97+ const mockData = {
98+ repository : {
99+ issues : {
100+ edges : [
101+ {
102+ node : {
103+ title : "Foo" ,
104+ } ,
105+ } ,
106+ {
107+ node : {
108+ title : "Bar" ,
109+ } ,
110+ } ,
111+ {
112+ node : {
113+ title : "Baz" ,
114+ } ,
115+ } ,
116+ ] ,
117+ } ,
118+ } ,
119+ } ;
120+
121+ const RepositoryDocument = new TypedDocumentString <
122+ {
123+ repository : { issues : { edges : Array < { node : { title : string } } > } } ;
124+ } ,
125+ Record < string , never >
126+ > ( /* GraphQL */ `
127+ {
128+ repository(owner: "octokit", name: "graphql.js") {
129+ issues(last: 3) {
130+ edges {
131+ node {
132+ title
133+ }
134+ }
135+ }
136+ }
137+ }
138+ ` ) ;
139+
140+ return graphql ( RepositoryDocument , {
141+ headers : {
142+ authorization : `token secret123` ,
143+ } ,
144+ request : {
145+ fetch : fetchMock . sandbox ( ) . post (
146+ "https://api.github.com/graphql" ,
147+ { data : mockData } ,
148+ {
149+ headers : {
150+ accept : "application/vnd.github.v3+json" ,
151+ authorization : "token secret123" ,
152+ "user-agent" : userAgent ,
153+ } ,
154+ } ,
155+ ) ,
156+ } ,
157+ } ) . then ( ( result ) => {
158+ expect ( JSON . stringify ( result ) ) . toStrictEqual ( JSON . stringify ( mockData ) ) ;
159+ } ) ;
160+ } ) ;
161+
80162 it ( "Variables" , ( ) => {
81163 const query = `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
82164 repository(owner:$owner, name:$repo) {
@@ -116,6 +198,54 @@ describe("graphql()", () => {
116198 } ) ;
117199 } ) ;
118200
201+ it ( "Variables with TypedDocumentString" , ( ) => {
202+ const query = new TypedDocumentString <
203+ {
204+ repository : { issues : { edges : Array < { node : { title : string } } > } } ;
205+ } ,
206+ {
207+ owner : string ;
208+ repo : string ;
209+ num ?: number ;
210+ }
211+ > ( `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
212+ repository(owner:$owner, name:$repo) {
213+ issues(last:$num) {
214+ edges {
215+ node {
216+ title
217+ }
218+ }
219+ }
220+ }
221+ }` ) ;
222+
223+ return graphql ( query , {
224+ headers : {
225+ authorization : `token secret123` ,
226+ } ,
227+ owner : "octokit" ,
228+ repo : "graphql.js" ,
229+ request : {
230+ fetch : fetchMock
231+ . sandbox ( )
232+ . post (
233+ "https://api.github.com/graphql" ,
234+ ( _url , options : OctokitTypes . RequestOptions ) => {
235+ const body = JSON . parse ( options . body ) ;
236+ expect ( body . query ) . toEqual ( query . toString ( ) ) ;
237+ expect ( body . variables ) . toStrictEqual ( {
238+ owner : "octokit" ,
239+ repo : "graphql.js" ,
240+ } ) ;
241+
242+ return { data : { } } ;
243+ } ,
244+ ) ,
245+ } ,
246+ } ) ;
247+ } ) ;
248+
119249 it ( "Pass headers together with variables as 2nd argument" , ( ) => {
120250 const query = `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
121251 repository(owner:$owner, name:$repo) {
0 commit comments