| 265 | }; |
| 266 | |
| 267 | const v4uncached = async ( |
| 268 | query: string, |
| 269 | options: GhGraphQlApiOptions = v4defaults, |
| 270 | ): Promise<AnyObject> => { |
| 271 | const personalToken = await getToken(); |
| 272 | |
| 273 | if (!personalToken) { |
| 274 | throw new RefinedGitHubApiError('Personal token required for this feature'); |
| 275 | } |
| 276 | |
| 277 | // GraphQL uses POST for everything, so check the query type instead of the HTTP method |
| 278 | if (/^\s*mutation[\s({]/.test(query)) { |
| 279 | await assertCurrentUser(); |
| 280 | } |
| 281 | |
| 282 | // TODO: Remove automatic usage of globals via `getRepo()` |
| 283 | // https://github.com/refined-github/refined-github/issues/5821 |
| 284 | const currentRepoIfAny = getRepo(); // Don't destructure, it's `undefined` outside repos |
| 285 | query = query.replace('repository() {', () => 'repository(owner: $owner, name: $name) {'); |
| 286 | |
| 287 | // Automatically provide variables common variables only when used. |
| 288 | // GraphQL doesn't like unused variables. |
| 289 | const variables: JsonObject = {}; |
| 290 | const parameters: string[] = []; |
| 291 | if (query.includes('$owner')) { |
| 292 | variables.owner = currentRepoIfAny!.owner; |
| 293 | parameters.push('$owner: String!'); |
| 294 | } |
| 295 | |
| 296 | if (query.includes('$name')) { |
| 297 | variables.name = currentRepoIfAny!.name; |
| 298 | parameters.push('$name: String!'); |
| 299 | } |
| 300 | |
| 301 | Object.assign(variables, options.variables); |
| 302 | |
| 303 | const fullQuery = /^\s*(?:query|mutation)/.test(query) |
| 304 | ? query |
| 305 | : parameters.length === 0 |
| 306 | ? `query {${query}}` |
| 307 | : `query (${parameters.join(',')}) {${query}}`; |
| 308 | |
| 309 | log.http(fullQuery); |
| 310 | |
| 311 | const response = await fetch(api4, { |
| 312 | headers: { |
| 313 | 'user-agent': 'Refined GitHub', |
| 314 | 'Content-Type': 'application/json', |
| 315 | Authorization: `bearer ${personalToken}`, |
| 316 | }, |
| 317 | method: 'POST', |
| 318 | body: JSON.stringify({ |
| 319 | variables, |
| 320 | query: fullQuery, |
| 321 | }), |
| 322 | }); |
| 323 | |
| 324 | const apiResponse: GraphQlResponse = await response.json(); |