Skip to content

Commit 6ea5832

Browse files
committed
Merge branch 'backoff'
2 parents 27cf7cd + 4af733e commit 6ea5832

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
getIsFile,
3737
getFile,
3838
getHeaders,
39+
getIsReloadingSchema,
3940
} from '../state/sessions/selectors'
4041
import { getHistoryOpen } from '../state/general/selectors'
4142
import {
@@ -82,6 +83,7 @@ export interface Props {
8283
configPath?: string
8384
createApolloLink?: (session: Session) => ApolloLink
8485
workspaceName?: string
86+
shouldInjectHeaders: boolean
8587
}
8688

8789
export interface ReduxProps {
@@ -98,6 +100,7 @@ export interface ReduxProps {
98100
setConfigString: (str: string) => void
99101
schemaFetchingError: (endpoint: string, error: string) => void
100102
schemaFetchingSuccess: (endpoint: string, tracingSupported: boolean) => void
103+
isReloadingSchema: boolean
101104
isConfigTab: boolean
102105
isSettingsTab: boolean
103106
isFile: boolean
@@ -148,8 +151,10 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
148151
componentWillMount() {
149152
// init redux
150153
this.props.initState(getWorkspaceId(this.props), this.props.endpoint)
151-
this.props.injectHeaders(this.props.headers, this.props.endpoint)
152154
this.props.setConfigString(this.props.configString)
155+
if (this.props.shouldInjectHeaders) {
156+
this.props.injectHeaders(this.props.headers, this.props.endpoint)
157+
}
153158
}
154159

155160
componentDidMount() {
@@ -168,10 +173,16 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
168173
if (
169174
nextProps.headers !== this.props.headers ||
170175
nextProps.endpoint !== this.props.endpoint ||
171-
nextProps.workspaceName !== this.props.workspaceName
176+
nextProps.workspaceName !== this.props.workspaceName ||
177+
nextProps.sessionHeaders !== this.props.sessionHeaders
172178
) {
173179
this.getSchema(nextProps)
174180
}
181+
if (this.props.isReloadingSchema && !nextProps.isReloadingSchema) {
182+
setTimeout(() => {
183+
this.getSchema(nextProps)
184+
})
185+
}
175186
if (
176187
this.props.endpoint !== nextProps.endpoint ||
177188
this.props.configPath !== nextProps.configPath ||
@@ -182,7 +193,10 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
182193
if (this.props.subscriptionEndpoint !== nextProps.subscriptionEndpoint) {
183194
setSubscriptionEndpoint(nextProps.subscriptionEndpoint)
184195
}
185-
if (nextProps.headers !== this.props.headers) {
196+
if (
197+
nextProps.headers !== this.props.headers &&
198+
this.props.shouldInjectHeaders
199+
) {
186200
this.props.injectHeaders(nextProps.headers, nextProps.endpoint)
187201
}
188202
if (nextProps.configString !== this.props.configString) {
@@ -195,6 +209,9 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
195209
this.setState({ schema: undefined })
196210
}
197211
let first = true
212+
if (this.backoff) {
213+
this.backoff.stop()
214+
}
198215
this.backoff = new Backoff(async () => {
199216
if (first) {
200217
await this.schemaGetter(props)
@@ -211,9 +228,10 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
211228
try {
212229
const data = {
213230
endpoint: props.endpoint,
214-
headers: props.headers
215-
? JSON.stringify(props.headers)
216-
: props.sessionHeaders,
231+
headers:
232+
props.sessionHeaders && props.sessionHeaders.length > 0
233+
? props.sessionHeaders
234+
: JSON.stringify(props.headers),
217235
}
218236
const schema = await schemaFetcher.fetch(data)
219237
if (schema) {
@@ -225,6 +243,8 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
225243
this.backoff.stop()
226244
}
227245
} catch (e) {
246+
// tslint:disable-next-line
247+
console.error(e)
228248
this.props.schemaFetchingError(props.endpoint, e.message)
229249
}
230250
}
@@ -322,6 +342,7 @@ const mapStateToProps = createStructuredSelector({
322342
sessionHeaders: getHeaders,
323343
settings: getSettings,
324344
settingsString: getSettingsString,
345+
isReloadingSchema: getIsReloadingSchema,
325346
})
326347

327348
export default connect(mapStateToProps, {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@ export class Backoff {
2222
this.count++
2323
// The first 15 attempts are fast, then fibonacci starts with n = 3
2424
if (this.running && this.count < this.maxRetries) {
25-
setTimeout(
25+
this.timeout = setTimeout(
2626
fn,
27-
(this.count < 15 ? 3 : fibonacci(this.count - 12)) * 1000,
27+
(this.count < 3 ? 5 : fibonacci(this.count - 12)) * 1000,
2828
)
2929
}
3030
}
3131
fn()
3232
}
33-
stop() {
33+
stop = () => {
3434
this.running = false
35-
clearTimeout(this.timeout)
3635
}
3736
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export interface State {
6464
activeProjectName?: string
6565
activeEnv?: string
6666
headers?: any
67+
shouldInjectHeaders: boolean
6768
}
6869

6970
class PlaygroundWrapper extends React.Component<
@@ -118,6 +119,7 @@ class PlaygroundWrapper extends React.Component<
118119
activeEnv,
119120
activeProjectName: projectName,
120121
headers,
122+
shouldInjectHeaders: false,
121123
}
122124
}
123125

@@ -129,6 +131,7 @@ class PlaygroundWrapper extends React.Component<
129131
try {
130132
const headers = getParameterByName('headers', endpoint)
131133
if (headers) {
134+
this.setState({ shouldInjectHeaders: true })
132135
return { headers: JSON.parse(headers), endpoint: splitted[0] }
133136
}
134137
} catch (e) {
@@ -322,6 +325,7 @@ class PlaygroundWrapper extends React.Component<
322325
headers={this.state.headers}
323326
configPath={this.props.configPath}
324327
workspaceName={this.state.activeProjectName}
328+
shouldInjectHeaders={this.state.shouldInjectHeaders}
325329
/>
326330
</App>
327331
</OldThemeProvider>

packages/graphql-playground-react/src/localDevIndex.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const config = {
6060
},
6161
endpoints: {
6262
dev: {
63-
url: 'https://eu1.prisma.sh/tim2/prisma-airbnb-example/dev',
63+
url: 'https://eu1.prisma.sh/lol/asdf/dev',
6464
headers: {
6565
Authorization:
6666
'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InNlcnZpY2UiOiJwcmlzbWEtYWlyYm5iLWV4YW1wbGVAZGV2Iiwicm9sZXMiOlsiYWRtaW4iXX0sImlhdCI6MTUyMjY4NDkzNiwiZXhwIjoxNTIzMjg5NzM2fQ.1wnkIJrAplDOznjEzLj8sQERL6dcAM0zjqxOGQBXGn0',

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ export function formatError(error, fetchingSchema: boolean = false) {
207207
return { error: `${message}${schemaMessage}. Please check your connection` }
208208
}
209209

210+
try {
211+
const ee = JSON.parse(message)
212+
return ee
213+
} catch (e) {
214+
//
215+
}
216+
210217
return { error: message }
211218
}
212219

0 commit comments

Comments
 (0)