fix: authorisation headers not being sent along with subscriptions when using graphql (#3354)
* fix: send auth headers to the payload * refactor: alert user that headers are sent to connection_init * refactor: send headers only when headers are populated * chore: cleanup code
This commit is contained in:
committed by
GitHub
parent
65884293be
commit
544b045300
@@ -112,6 +112,7 @@
|
|||||||
},
|
},
|
||||||
"authorization": {
|
"authorization": {
|
||||||
"generate_token": "Generate Token",
|
"generate_token": "Generate Token",
|
||||||
|
"graphql_headers": "Authorization Headers are sent as part of the payload to connection_init",
|
||||||
"include_in_url": "Include in URL",
|
"include_in_url": "Include in URL",
|
||||||
"learn": "Learn how",
|
"learn": "Learn how",
|
||||||
"pass_key_by": "Pass by",
|
"pass_key_by": "Pass by",
|
||||||
|
|||||||
@@ -136,6 +136,9 @@ const runQuery = async (
|
|||||||
const duration = Date.now() - startTime
|
const duration = Date.now() - startTime
|
||||||
completePageProgress()
|
completePageProgress()
|
||||||
toast.success(`${t("state.finished_in", { duration })}`)
|
toast.success(`${t("state.finished_in", { duration })}`)
|
||||||
|
if (definition?.operation === "subscription" && request.value.auth) {
|
||||||
|
toast.success(t("authorization.graphql_headers"))
|
||||||
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
// response.value = [`${e}`]
|
// response.value = [`${e}`]
|
||||||
|
|||||||
@@ -268,7 +268,7 @@ export const runGQLOperation = async (options: RunQueryOptions) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (operationType === "subscription") {
|
if (operationType === "subscription") {
|
||||||
return runSubscription(options)
|
return runSubscription(options, finalHeaders)
|
||||||
}
|
}
|
||||||
|
|
||||||
const interceptorService = getService(InterceptorService)
|
const interceptorService = getService(InterceptorService)
|
||||||
@@ -299,7 +299,10 @@ export const runGQLOperation = async (options: RunQueryOptions) => {
|
|||||||
return responseText
|
return responseText
|
||||||
}
|
}
|
||||||
|
|
||||||
export const runSubscription = (options: RunQueryOptions) => {
|
export const runSubscription = (
|
||||||
|
options: RunQueryOptions,
|
||||||
|
headers?: Record<string, string>
|
||||||
|
) => {
|
||||||
const { url, query, operationName } = options
|
const { url, query, operationName } = options
|
||||||
const wsUrl = url.replace(/^http/, "ws")
|
const wsUrl = url.replace(/^http/, "ws")
|
||||||
|
|
||||||
@@ -309,10 +312,11 @@ export const runSubscription = (options: RunQueryOptions) => {
|
|||||||
|
|
||||||
connection.socket.onopen = (event) => {
|
connection.socket.onopen = (event) => {
|
||||||
console.log("WebSocket is open now.", event)
|
console.log("WebSocket is open now.", event)
|
||||||
|
|
||||||
connection.socket?.send(
|
connection.socket?.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: GQL.CONNECTION_INIT,
|
type: GQL.CONNECTION_INIT,
|
||||||
payload: {},
|
payload: headers ?? {},
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user