Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: graphql connection error handler #4523

Merged
Next Next commit
fix: graphql connection error handler
  • Loading branch information
anwarulislam committed Nov 7, 2024
commit 69d4f8778e85448f203e072abc2c6bdc441bc9e7
1 change: 1 addition & 0 deletions packages/hoppscotch-common/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@
},
"graphql": {
"connection_switch_confirm": "Do you want to connect with the latest GraphQL endpoint?",
"connection_error_http": "HTTP Error",
"connection_switch_new_url": "Switching to a tab will disconnected you from the active GraphQL connection. New connection URL is",
"connection_switch_url": "You're connected to a GraphQL endpoint the connection URL is",
"mutations": "Mutations",
Expand Down
39 changes: 28 additions & 11 deletions packages/hoppscotch-common/src/helpers/graphql/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export type GQLResponseEvent =
}
}

export type ConnectionState = "CONNECTING" | "CONNECTED" | "DISCONNECTED"
export type ConnectionState =
| "CONNECTING"
| "CONNECTED"
| "DISCONNECTED"
| "ERROR"
export type SubscriptionState = "SUBSCRIBING" | "SUBSCRIBED" | "UNSUBSCRIBED"

const GQL = {
Expand Down Expand Up @@ -100,10 +104,7 @@ export const gqlMessageEvent = ref<GQLResponseEvent | "reset">()

export const schemaString = computed(() => {
if (!connection.schema) return ""

return printSchema(connection.schema, {
commentDescriptions: true,
})
return printSchema(connection.schema)
})

export const queryFields = computed(() => {
Expand Down Expand Up @@ -166,14 +167,19 @@ export const connect = async (url: string, headers: GQLHeader[]) => {
)
}

// Polling
connection.state = "CONNECTED"
connection.state = "CONNECTING"

const poll = async () => {
await getSchema(url, headers)
timeoutSubscription = setTimeout(() => {
poll()
}, GQL_SCHEMA_POLL_INTERVAL)
try {
await getSchema(url, headers)
// polling for schema
if (connection.state !== "CONNECTED") connection.state = "CONNECTED"
timeoutSubscription = setTimeout(() => {
poll()
}, GQL_SCHEMA_POLL_INTERVAL)
} catch (error) {
console.error(error)
}
}

await poll()
Expand Down Expand Up @@ -237,6 +243,17 @@ const getSchema = async (url: string, headers: GQLHeader[]) => {
throw new Error(res.left.toString())
}

if (res.right.status !== 200) {
connection.state = "ERROR"
connection.error = {
type: "HTTP_ERROR",
message: (t: ReturnType<typeof getI18n>) =>
t("graphql.connection_error_http"),
component: undefined,
}
anwarulislam marked this conversation as resolved.
Show resolved Hide resolved
throw new Error("Failed to fetch schema. Status: " + res.right.status)
}

const data = res.right

// HACK : Temporary trailing null character issue from the extension fix
Expand Down