@@ -37,6 +37,7 @@ import {
37
37
getFile ,
38
38
getHeaders ,
39
39
getIsReloadingSchema ,
40
+ getEndpoint ,
40
41
} from '../state/sessions/selectors'
41
42
import { getHistoryOpen } from '../state/general/selectors'
42
43
import {
@@ -48,6 +49,7 @@ import { Session } from '../state/sessions/reducers'
48
49
import { getWorkspaceId } from './Playground/util/getWorkspaceId'
49
50
import { getSettings , getSettingsString } from '../state/workspace/reducers'
50
51
import { Backoff } from './Playground/util/fibonacci-backoff'
52
+ import { debounce } from 'lodash'
51
53
52
54
export interface Response {
53
55
resultID : string
@@ -57,6 +59,7 @@ export interface Response {
57
59
58
60
export interface Props {
59
61
endpoint : string
62
+ sessionEndpoint : string
60
63
subscriptionEndpoint ?: string
61
64
projectId ?: string
62
65
shareEnabled ?: boolean
@@ -130,10 +133,34 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
130
133
apolloLinks : { [ sessionId : string ] : any } = { }
131
134
observers : { [ sessionId : string ] : any } = { }
132
135
graphiqlComponents : any [ ] = [ ]
136
+
137
+ // debounce as we call this on each http header or endpoint edit
138
+ getSchema = debounce (
139
+ async ( props : Props & ReduxProps = this . props ) => {
140
+ if ( this . mounted && this . state . schema ) {
141
+ this . setState ( { schema : undefined } )
142
+ }
143
+ let first = true
144
+ if ( this . backoff ) {
145
+ this . backoff . stop ( )
146
+ }
147
+ this . backoff = new Backoff ( async ( ) => {
148
+ if ( first ) {
149
+ await this . schemaGetter ( props )
150
+ first = false
151
+ } else {
152
+ await this . schemaGetter ( )
153
+ }
154
+ } )
155
+ this . backoff . start ( )
156
+ } ,
157
+ 300 ,
158
+ { trailing : true } , // important to not miss the last call
159
+ ) as any
160
+
133
161
private backoff : Backoff
134
162
private initialIndex : number = - 1
135
163
private mounted = false
136
- private fetchingSchema = false
137
164
138
165
constructor ( props : Props & ReduxProps ) {
139
166
super ( props )
@@ -168,15 +195,16 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
168
195
this . mounted = true
169
196
}
170
197
171
- componentWillReceiveProps ( nextProps ) {
198
+ componentWillReceiveProps ( nextProps : Props & ReduxProps ) {
172
199
if ( this . props . createApolloLink !== nextProps . createApolloLink ) {
173
200
setLinkCreator ( nextProps . createApolloLink )
174
201
}
175
202
if (
176
203
nextProps . headers !== this . props . headers ||
177
204
nextProps . endpoint !== this . props . endpoint ||
178
205
nextProps . workspaceName !== this . props . workspaceName ||
179
- nextProps . sessionHeaders !== this . props . sessionHeaders
206
+ nextProps . sessionHeaders !== this . props . sessionHeaders ||
207
+ nextProps . sessionEndpoint !== this . props . sessionEndpoint
180
208
) {
181
209
this . getSchema ( nextProps )
182
210
}
@@ -203,54 +231,35 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
203
231
}
204
232
}
205
233
206
- async getSchema ( props = this . props ) {
207
- if ( this . mounted && this . state . schema ) {
208
- this . setState ( { schema : undefined } )
209
- }
210
- let first = true
211
- if ( this . backoff ) {
212
- this . backoff . stop ( )
213
- }
214
- this . backoff = new Backoff ( async ( ) => {
215
- if ( first ) {
216
- await this . schemaGetter ( props )
217
- first = false
218
- } else {
219
- await this . schemaGetter ( )
234
+ async schemaGetter ( propsInput ?: Props & ReduxProps ) {
235
+ const props = this . props || propsInput
236
+ const endpoint = props . sessionEndpoint || props . endpoint
237
+ try {
238
+ const data = {
239
+ endpoint,
240
+ headers :
241
+ props . sessionHeaders && props . sessionHeaders . length > 0
242
+ ? props . sessionHeaders
243
+ : JSON . stringify ( props . headers ) ,
220
244
}
221
- } )
222
- this . backoff . start ( )
223
- }
224
-
225
- async schemaGetter ( props = this . props ) {
226
- if ( ! this . fetchingSchema ) {
227
- try {
228
- const data = {
229
- endpoint : props . endpoint ,
230
- headers :
231
- props . sessionHeaders && props . sessionHeaders . length > 0
232
- ? props . sessionHeaders
233
- : JSON . stringify ( props . headers ) ,
234
- }
235
- const schema = await schemaFetcher . fetch ( data )
236
- schemaFetcher . subscribe ( data , newSchema => {
237
- if ( data . endpoint === this . props . endpoint ) {
238
- this . setState ( { schema : newSchema } )
239
- }
240
- } )
241
- if ( schema ) {
242
- this . setState ( { schema : schema . schema } )
243
- this . props . schemaFetchingSuccess (
244
- props . endpoint ,
245
- schema . tracingSupported ,
246
- )
247
- this . backoff . stop ( )
245
+ const schema = await schemaFetcher . fetch ( data )
246
+ schemaFetcher . subscribe ( data , newSchema => {
247
+ if (
248
+ data . endpoint === this . props . endpoint ||
249
+ data . endpoint === this . props . sessionEndpoint
250
+ ) {
251
+ this . setState ( { schema : newSchema } )
248
252
}
249
- } catch ( e ) {
250
- // tslint:disable-next-line
251
- console . error ( e )
252
- this . props . schemaFetchingError ( props . endpoint , e . message )
253
+ } )
254
+ if ( schema ) {
255
+ this . setState ( { schema : schema . schema } )
256
+ this . props . schemaFetchingSuccess ( data . endpoint , schema . tracingSupported )
257
+ this . backoff . stop ( )
253
258
}
259
+ } catch ( e ) {
260
+ // tslint:disable-next-line
261
+ console . error ( e )
262
+ this . props . schemaFetchingError ( endpoint , e . message )
254
263
}
255
264
}
256
265
@@ -350,6 +359,7 @@ const mapStateToProps = createStructuredSelector({
350
359
settings : getSettings ,
351
360
settingsString : getSettingsString ,
352
361
isReloadingSchema : getIsReloadingSchema ,
362
+ sessionEndpoint : getEndpoint ,
353
363
} )
354
364
355
365
export default connect ( mapStateToProps , {
0 commit comments