33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import { ApolloQueryResult , FetchResult , MutationOptions , NetworkStatus , QueryOptions } from 'apollo-boost' ;
6+ import { ApolloQueryResult , DocumentNode , FetchResult , MutationOptions , NetworkStatus , QueryOptions } from 'apollo-boost' ;
77import * as vscode from 'vscode' ;
88import { AuthenticationError , AuthProvider , GitHubServerType , isSamlError } from '../common/authentication' ;
99import Logger from '../common/logger' ;
@@ -197,7 +197,7 @@ export class GitHubRepository implements vscode.Disposable {
197197 }
198198 }
199199
200- query = async < T > ( query : QueryOptions , ignoreSamlErrors : boolean = false ) : Promise < ApolloQueryResult < T > > => {
200+ query = async < T > ( query : QueryOptions , ignoreSamlErrors : boolean = false , legacyFallback ?: { query : DocumentNode } ) : Promise < ApolloQueryResult < T > > => {
201201 const gql = this . authMatchesServer && this . hub && this . hub . graphql ;
202202 if ( ! gql ) {
203203 Logger . debug ( `Not available for query: ${ query } ` , GRAPHQL_COMPONENT_ID ) ;
@@ -214,6 +214,11 @@ export class GitHubRepository implements vscode.Disposable {
214214 try {
215215 rsp = await gql . query < T > ( query ) ;
216216 } catch ( e ) {
217+ if ( legacyFallback ) {
218+ query . query = legacyFallback . query ;
219+ return this . query ( query , ignoreSamlErrors ) ;
220+ }
221+
217222 // Some queries just result in SAML errors, and some queries we may not want to retry because it will be too disruptive.
218223 if ( ! ignoreSamlErrors && e . message ?. startsWith ( 'GraphQL error: Resource protected by organization SAML enforcement.' ) ) {
219224 await this . _credentialStore . recreate ( ) ;
@@ -226,7 +231,7 @@ export class GitHubRepository implements vscode.Disposable {
226231 return rsp ;
227232 } ;
228233
229- mutate = async < T > ( mutation : MutationOptions < T > ) : Promise < FetchResult < T > > => {
234+ mutate = async < T > ( mutation : MutationOptions < T > , legacyFallback ?: { mutation : DocumentNode , deleteProps : string [ ] } ) : Promise < FetchResult < T > > => {
230235 const gql = this . authMatchesServer && this . hub && this . hub . graphql ;
231236 if ( ! gql ) {
232237 Logger . debug ( `Not available for query: ${ mutation } ` , GRAPHQL_COMPONENT_ID ) ;
@@ -239,7 +244,21 @@ export class GitHubRepository implements vscode.Disposable {
239244 }
240245
241246 Logger . trace ( `Request: ${ JSON . stringify ( mutation , null , 2 ) } ` , GRAPHQL_COMPONENT_ID ) ;
242- const rsp = await gql . mutate < T > ( mutation ) ;
247+ let rsp ;
248+ try {
249+ rsp = await gql . mutate < T > ( mutation ) ;
250+ } catch {
251+ if ( legacyFallback ) {
252+ mutation . mutation = legacyFallback . mutation ;
253+ if ( mutation . variables ?. input ) {
254+ for ( const prop of legacyFallback . deleteProps ) {
255+ delete mutation . variables . input [ prop ] ;
256+ }
257+ }
258+ }
259+
260+ return this . mutate ( mutation ) ;
261+ }
243262 Logger . trace ( `Response: ${ JSON . stringify ( rsp , null , 2 ) } ` , GRAPHQL_COMPONENT_ID ) ;
244263 return rsp ;
245264 } ;
0 commit comments