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:
Joel Jacob Stephen
2023-09-28 21:57:07 +05:30
committed by GitHub
parent 65884293be
commit 544b045300
3 changed files with 11 additions and 3 deletions

View File

@@ -112,6 +112,7 @@
},
"authorization": {
"generate_token": "Generate Token",
"graphql_headers": "Authorization Headers are sent as part of the payload to connection_init",
"include_in_url": "Include in URL",
"learn": "Learn how",
"pass_key_by": "Pass by",

View File

@@ -136,6 +136,9 @@ const runQuery = async (
const duration = Date.now() - startTime
completePageProgress()
toast.success(`${t("state.finished_in", { duration })}`)
if (definition?.operation === "subscription" && request.value.auth) {
toast.success(t("authorization.graphql_headers"))
}
} catch (e: any) {
console.log(e)
// response.value = [`${e}`]

View File

@@ -268,7 +268,7 @@ export const runGQLOperation = async (options: RunQueryOptions) => {
}
if (operationType === "subscription") {
return runSubscription(options)
return runSubscription(options, finalHeaders)
}
const interceptorService = getService(InterceptorService)
@@ -299,7 +299,10 @@ export const runGQLOperation = async (options: RunQueryOptions) => {
return responseText
}
export const runSubscription = (options: RunQueryOptions) => {
export const runSubscription = (
options: RunQueryOptions,
headers?: Record<string, string>
) => {
const { url, query, operationName } = options
const wsUrl = url.replace(/^http/, "ws")
@@ -309,10 +312,11 @@ export const runSubscription = (options: RunQueryOptions) => {
connection.socket.onopen = (event) => {
console.log("WebSocket is open now.", event)
connection.socket?.send(
JSON.stringify({
type: GQL.CONNECTION_INIT,
payload: {},
payload: headers ?? {},
})
)