Skip to content

Commit d8a17e8

Browse files
committed
fix: schema fetching for custom endpoints
1 parent 543042c commit d8a17e8

File tree

2 files changed

+60
-50
lines changed

2 files changed

+60
-50
lines changed

packages/graphql-playground-react/src/components/Playground.tsx

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
getFile,
3838
getHeaders,
3939
getIsReloadingSchema,
40+
getEndpoint,
4041
} from '../state/sessions/selectors'
4142
import { getHistoryOpen } from '../state/general/selectors'
4243
import {
@@ -48,6 +49,7 @@ import { Session } from '../state/sessions/reducers'
4849
import { getWorkspaceId } from './Playground/util/getWorkspaceId'
4950
import { getSettings, getSettingsString } from '../state/workspace/reducers'
5051
import { Backoff } from './Playground/util/fibonacci-backoff'
52+
import { debounce } from 'lodash'
5153

5254
export interface Response {
5355
resultID: string
@@ -57,6 +59,7 @@ export interface Response {
5759

5860
export interface Props {
5961
endpoint: string
62+
sessionEndpoint: string
6063
subscriptionEndpoint?: string
6164
projectId?: string
6265
shareEnabled?: boolean
@@ -130,10 +133,34 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
130133
apolloLinks: { [sessionId: string]: any } = {}
131134
observers: { [sessionId: string]: any } = {}
132135
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+
133161
private backoff: Backoff
134162
private initialIndex: number = -1
135163
private mounted = false
136-
private fetchingSchema = false
137164

138165
constructor(props: Props & ReduxProps) {
139166
super(props)
@@ -168,15 +195,16 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
168195
this.mounted = true
169196
}
170197

171-
componentWillReceiveProps(nextProps) {
198+
componentWillReceiveProps(nextProps: Props & ReduxProps) {
172199
if (this.props.createApolloLink !== nextProps.createApolloLink) {
173200
setLinkCreator(nextProps.createApolloLink)
174201
}
175202
if (
176203
nextProps.headers !== this.props.headers ||
177204
nextProps.endpoint !== this.props.endpoint ||
178205
nextProps.workspaceName !== this.props.workspaceName ||
179-
nextProps.sessionHeaders !== this.props.sessionHeaders
206+
nextProps.sessionHeaders !== this.props.sessionHeaders ||
207+
nextProps.sessionEndpoint !== this.props.sessionEndpoint
180208
) {
181209
this.getSchema(nextProps)
182210
}
@@ -203,54 +231,35 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
203231
}
204232
}
205233

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),
220244
}
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 })
248252
}
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()
253258
}
259+
} catch (e) {
260+
// tslint:disable-next-line
261+
console.error(e)
262+
this.props.schemaFetchingError(endpoint, e.message)
254263
}
255264
}
256265

@@ -350,6 +359,7 @@ const mapStateToProps = createStructuredSelector({
350359
settings: getSettings,
351360
settingsString: getSettingsString,
352361
isReloadingSchema: getIsReloadingSchema,
362+
sessionEndpoint: getEndpoint,
353363
})
354364

355365
export default connect(mapStateToProps, {

packages/graphql-playground-react/src/components/Playground/util/fibonacci-backoff.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export class Backoff {
2020
const fn = async () => {
2121
await this.cb()
2222
this.count++
23-
// The first 15 attempts are fast, then fibonacci starts with n = 3
23+
// The first 5 attempts are fast, then fibonacci starts with n = 3
2424
if (this.running && this.count < this.maxRetries) {
2525
this.timeout = setTimeout(
2626
fn,
27-
(this.count < 3 ? 5 : fibonacci(this.count - 12)) * 1000,
27+
(this.count < 3 ? 5 : fibonacci(this.count - 5)) * 1000,
2828
)
2929
}
3030
}

0 commit comments

Comments
 (0)