File tree Expand file tree Collapse file tree 2 files changed +56
-4
lines changed Expand file tree Collapse file tree 2 files changed +56
-4
lines changed Original file line number Diff line number Diff line change @@ -18,17 +18,29 @@ const NON_VARIABLE_OPTIONS = [
18
18
"mediaType" ,
19
19
] ;
20
20
21
+ const FORBIDDEN_VARIABLE_OPTIONS = [ "query" , "method" , "url" ] ;
22
+
21
23
const GHES_V3_SUFFIX_REGEX = / \/ a p i \/ v 3 \/ ? $ / ;
22
24
23
25
export function graphql < ResponseData = GraphQlQueryResponseData > (
24
26
request : typeof Request ,
25
27
query : string | RequestParameters ,
26
28
options ?: RequestParameters
27
29
) : Promise < ResponseData > {
28
- if ( typeof query === "string" && options && "query" in options ) {
29
- return Promise . reject (
30
- new Error ( `[@octokit/graphql] "query" cannot be used as variable name` )
31
- ) ;
30
+ if ( options ) {
31
+ if ( typeof query === "string" && "query" in options ) {
32
+ return Promise . reject (
33
+ new Error ( `[@octokit/graphql] "query" cannot be used as variable name` )
34
+ ) ;
35
+ }
36
+
37
+ for ( const key in options ) {
38
+ if ( ! FORBIDDEN_VARIABLE_OPTIONS . includes ( key ) ) continue ;
39
+
40
+ return Promise . reject (
41
+ new Error ( `[@octokit/graphql] "${ key } " cannot be used as variable name` )
42
+ ) ;
43
+ }
32
44
}
33
45
34
46
const parsedOptions =
Original file line number Diff line number Diff line change @@ -231,4 +231,44 @@ describe("graphql()", () => {
231
231
) ;
232
232
} ) ;
233
233
} ) ;
234
+
235
+ it ( "url variable (#264)" , ( ) => {
236
+ expect . assertions ( 1 ) ;
237
+
238
+ const query = `query GetCommitStatus($url: URI!) {
239
+ resource(url: $url) {
240
+ ... on Commit {
241
+ status {
242
+ state
243
+ }
244
+ }
245
+ }
246
+ }` ;
247
+
248
+ return graphql ( query , {
249
+ url : "https://example.com" ,
250
+ } ) . catch ( ( error ) => {
251
+ expect ( error . message ) . toEqual (
252
+ `[@octokit/graphql] "url" cannot be used as variable name`
253
+ ) ;
254
+ } ) ;
255
+ } ) ;
256
+
257
+ it ( "method variable" , ( ) => {
258
+ expect . assertions ( 1 ) ;
259
+
260
+ const query = `query($method:String!){
261
+ search(query:$method,type:ISSUE) {
262
+ codeCount
263
+ }
264
+ }` ;
265
+
266
+ return graphql ( query , {
267
+ method : "test" ,
268
+ } ) . catch ( ( error ) => {
269
+ expect ( error . message ) . toEqual (
270
+ `[@octokit/graphql] "method" cannot be used as variable name`
271
+ ) ;
272
+ } ) ;
273
+ } ) ;
234
274
} ) ;
You can’t perform that action at this time.
0 commit comments