Skip to content

Commit 3bea9dd

Browse files
committed
fix schema fetching error handling
1 parent 37472d3 commit 3bea9dd

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
import { Session } from '../state/sessions/reducers'
4545
import { getWorkspaceId } from './Playground/util/getWorkspaceId'
4646
import { getSettings, getSettingsString } from '../state/workspace/reducers'
47+
import { schemaFetchingError, schemaFetchingSuccess } from '../lib'
4748

4849
export interface Response {
4950
resultID: string
@@ -93,6 +94,8 @@ export interface ReduxProps {
9394
setTracingSupported: (value: boolean) => void
9495
injectHeaders: (headers: string, endpoint: string) => void
9596
setConfigString: (str: string) => void
97+
schemaFetchingError: (endpoint: string) => void
98+
schemaFetchingSuccess: (endpoint: string, tracingSupported: boolean) => void
9699
isConfigTab: boolean
97100
isSettingsTab: boolean
98101
isFile: boolean
@@ -120,6 +123,8 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
120123
graphiqlComponents: any[] = []
121124
private initialIndex: number = -1
122125
private mounted = false
126+
private retries = 0
127+
private maxRetries = 10
123128

124129
constructor(props: Props & ReduxProps) {
125130
super(props)
@@ -187,15 +192,27 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
187192
if (this.mounted && this.state.schema) {
188193
this.setState({ schema: undefined })
189194
}
190-
const schema = await schemaFetcher.fetch({
191-
endpoint: props.endpoint,
192-
headers: props.headers
193-
? JSON.stringify(props.headers)
194-
: props.sessionHeaders,
195-
})
196-
if (schema) {
197-
this.setState({ schema: schema.schema })
198-
this.props.setTracingSupported(schema.tracingSupported)
195+
try {
196+
const schema = await schemaFetcher.fetch({
197+
endpoint: props.endpoint,
198+
headers: props.headers
199+
? JSON.stringify(props.headers)
200+
: props.sessionHeaders,
201+
})
202+
if (schema) {
203+
this.setState({ schema: schema.schema })
204+
this.props.schemaFetchingSuccess(
205+
props.endpoint,
206+
schema.tracingSupported,
207+
)
208+
}
209+
} catch (e) {
210+
this.props.schemaFetchingError(props.endpoint)
211+
if (this.retries < this.maxRetries) {
212+
await new Promise(r => setTimeout(r, 5000))
213+
this.retries++
214+
this.getSchema(props)
215+
}
199216
}
200217
}
201218

@@ -305,6 +322,8 @@ export default connect(mapStateToProps, {
305322
setTracingSupported,
306323
injectHeaders,
307324
setConfigString,
325+
schemaFetchingError,
326+
schemaFetchingSuccess,
308327
})(Playground)
309328

310329
const PlaygroundWrapper = styled.div`

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getEndpoint,
1414
getSelectedSession,
1515
getIsReloadingSchema,
16+
getEndpointUnreachable,
1617
} from '../../../state/sessions/selectors'
1718
import { connect } from 'react-redux'
1819
import { getFixedEndpoint } from '../../../state/general/selectors'
@@ -143,6 +144,7 @@ const mapStateToProps = createStructuredSelector({
143144
endpoint: getEndpoint,
144145
fixedEndpoint: getFixedEndpoint,
145146
isReloadingSchema: getIsReloadingSchema,
147+
endpointUnreachable: getEndpointUnreachable,
146148
})
147149

148150
export default connect(mapStateToProps, {

packages/graphql-playground-react/src/state/sessions/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const {
124124
+ tracingSupported
125125
})
126126
*/
127-
SCHEMA_FETCHING_ERROR: () => ({}),
127+
SCHEMA_FETCHING_ERROR: endpoint => ({ endpoint }),
128128
/*
129129
130130
this.setState({

0 commit comments

Comments
 (0)