Skip to content

Commit

Permalink
Fix schema reloading. Closes #637
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed May 5, 2018
1 parent 46ddaa5 commit 6078c74
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ export class Playground extends React.PureComponent<Props & ReduxProps, State> {
: JSON.stringify(props.headers),
}
const schema = await schemaFetcher.fetch(data)
schemaFetcher.subscribe(data, newSchema => {
if (data.endpoint === this.props.endpoint) {
this.setState({ schema: newSchema })
}
})
if (schema) {
this.setState({ schema: schema.schema })
this.props.schemaFetchingSuccess(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class SchemaFetcher {
cache: Map<string, TracingSchemaTuple>
linkGetter: LinkGetter
fetching: Map<string, Promise<any>>
subscriptions: Map<string, (schema: GraphQLSchema) => void> = Map()
constructor(linkGetter: LinkGetter) {
this.cache = Map()
this.fetching = Map()
Expand All @@ -41,15 +42,20 @@ export class SchemaFetcher {
this.fetching = this.fetching.set(hash, promise)
return promise
}
subscribe(session: SchemaFetchProps, cb: (schema: GraphQLSchema) => void) {
const hash = this.hash(session)
this.subscriptions = this.subscriptions.set(hash, cb)
}
refetch(session: SchemaFetchProps) {
return this.fetchSchema(session)
}
hash(session: SchemaFetchProps) {
return `${session.endpoint}~${session.headers}`
return `${session.endpoint}~${session.headers || ''}`
}
private fetchSchema(
session: SchemaFetchProps,
): Promise<{ schema: GraphQLSchema; tracingSupported: boolean } | null> {
const hash = this.hash(session)
const { endpoint } = session
const headers = {
...parseHeaders(session.headers),
Expand Down Expand Up @@ -81,7 +87,11 @@ export class SchemaFetcher {
}
this.cache = this.cache.set(this.hash(session), result)
resolve(result)
this.fetching = this.fetching.remove(this.hash(session))
this.fetching = this.fetching.remove(hash)
const subscription = this.subscriptions.get(hash)
if (subscription) {
subscription(result.schema)
}
},
error: err => {
reject(err)
Expand Down
10 changes: 5 additions & 5 deletions packages/graphql-playground-react/src/localDevIndex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ const config = {
},
endpoints: {
dev2: {
url: 'https://eu1.prisma.sh/lol/asdf/dev',
headers: {
Authorization:
'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InNlcnZpY2UiOiJhc2RmQGRldiIsInJvbGVzIjpbImFkbWluIl19LCJpYXQiOjE1MjM1MTg3NTYsImV4cCI6MTUyNDEyMzU1Nn0.fzKhXa1JpN9M1UGTbS6p2KMUWDrKLxYD3i3a9eVfOQQ',
},
url: 'https://eu1.prisma.sh/public-asdf/session65/dev',
// headers: {
// Authorization:
// 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InNlcnZpY2UiOiJhc2RmQGRldiIsInJvbGVzIjpbImFkbWluIl19LCJpYXQiOjE1MjM1MTg3NTYsImV4cCI6MTUyNDEyMzU1Nn0.fzKhXa1JpN9M1UGTbS6p2KMUWDrKLxYD3i3a9eVfOQQ',
// },
},
},
},
Expand Down

0 comments on commit 6078c74

Please sign in to comment.