Skip to content

Commit 6078c74

Browse files
committed
Fix schema reloading. Closes #637
1 parent 46ddaa5 commit 6078c74

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
233233
: JSON.stringify(props.headers),
234234
}
235235
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+
})
236241
if (schema) {
237242
this.setState({ schema: schema.schema })
238243
this.props.schemaFetchingSuccess(

packages/graphql-playground-react/src/components/Playground/SchemaFetcher.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class SchemaFetcher {
2222
cache: Map<string, TracingSchemaTuple>
2323
linkGetter: LinkGetter
2424
fetching: Map<string, Promise<any>>
25+
subscriptions: Map<string, (schema: GraphQLSchema) => void> = Map()
2526
constructor(linkGetter: LinkGetter) {
2627
this.cache = Map()
2728
this.fetching = Map()
@@ -41,15 +42,20 @@ export class SchemaFetcher {
4142
this.fetching = this.fetching.set(hash, promise)
4243
return promise
4344
}
45+
subscribe(session: SchemaFetchProps, cb: (schema: GraphQLSchema) => void) {
46+
const hash = this.hash(session)
47+
this.subscriptions = this.subscriptions.set(hash, cb)
48+
}
4449
refetch(session: SchemaFetchProps) {
4550
return this.fetchSchema(session)
4651
}
4752
hash(session: SchemaFetchProps) {
48-
return `${session.endpoint}~${session.headers}`
53+
return `${session.endpoint}~${session.headers || ''}`
4954
}
5055
private fetchSchema(
5156
session: SchemaFetchProps,
5257
): Promise<{ schema: GraphQLSchema; tracingSupported: boolean } | null> {
58+
const hash = this.hash(session)
5359
const { endpoint } = session
5460
const headers = {
5561
...parseHeaders(session.headers),
@@ -81,7 +87,11 @@ export class SchemaFetcher {
8187
}
8288
this.cache = this.cache.set(this.hash(session), result)
8389
resolve(result)
84-
this.fetching = this.fetching.remove(this.hash(session))
90+
this.fetching = this.fetching.remove(hash)
91+
const subscription = this.subscriptions.get(hash)
92+
if (subscription) {
93+
subscription(result.schema)
94+
}
8595
},
8696
error: err => {
8797
reject(err)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ const config = {
5151
},
5252
endpoints: {
5353
dev2: {
54-
url: 'https://eu1.prisma.sh/lol/asdf/dev',
55-
headers: {
56-
Authorization:
57-
'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InNlcnZpY2UiOiJhc2RmQGRldiIsInJvbGVzIjpbImFkbWluIl19LCJpYXQiOjE1MjM1MTg3NTYsImV4cCI6MTUyNDEyMzU1Nn0.fzKhXa1JpN9M1UGTbS6p2KMUWDrKLxYD3i3a9eVfOQQ',
58-
},
54+
url: 'https://eu1.prisma.sh/public-asdf/session65/dev',
55+
// headers: {
56+
// Authorization:
57+
// 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InNlcnZpY2UiOiJhc2RmQGRldiIsInJvbGVzIjpbImFkbWluIl19LCJpYXQiOjE1MjM1MTg3NTYsImV4cCI6MTUyNDEyMzU1Nn0.fzKhXa1JpN9M1UGTbS6p2KMUWDrKLxYD3i3a9eVfOQQ',
58+
// },
5959
},
6060
},
6161
},

0 commit comments

Comments
 (0)