GQLConnection.ts (hoppscotch-2.2.1) | : | GQLConnection.ts (hoppscotch-3.0.0) | ||
---|---|---|---|---|
skipping to change at line 13 | skipping to change at line 13 | |||
getIntrospectionQuery, | getIntrospectionQuery, | |||
buildClientSchema, | buildClientSchema, | |||
GraphQLSchema, | GraphQLSchema, | |||
printSchema, | printSchema, | |||
GraphQLObjectType, | GraphQLObjectType, | |||
GraphQLInputObjectType, | GraphQLInputObjectType, | |||
GraphQLEnumType, | GraphQLEnumType, | |||
GraphQLInterfaceType, | GraphQLInterfaceType, | |||
} from "graphql" | } from "graphql" | |||
import { distinctUntilChanged, map } from "rxjs/operators" | import { distinctUntilChanged, map } from "rxjs/operators" | |||
import { GQLHeader } from "@hoppscotch/data" | import { GQLHeader, HoppGQLAuth } from "@hoppscotch/data" | |||
import { sendNetworkRequest } from "./network" | import { sendNetworkRequest } from "./network" | |||
const GQL_SCHEMA_POLL_INTERVAL = 7000 | const GQL_SCHEMA_POLL_INTERVAL = 7000 | |||
/** | /** | |||
GQLConnection deals with all the operations (like polling, schema extraction) that runs | GQLConnection deals with all the operations (like polling, schema extraction) that runs | |||
when a connection is made to a GraphQL server. | when a connection is made to a GraphQL server. | |||
*/ | */ | |||
export class GQLConnection { | export class GQLConnection { | |||
public isLoading$ = new BehaviorSubject<boolean>(false) | public isLoading$ = new BehaviorSubject<boolean>(false) | |||
skipping to change at line 152 | skipping to change at line 152 | |||
const introspectionQuery = JSON.stringify({ | const introspectionQuery = JSON.stringify({ | |||
query: getIntrospectionQuery(), | query: getIntrospectionQuery(), | |||
}) | }) | |||
const finalHeaders: Record<string, string> = {} | const finalHeaders: Record<string, string> = {} | |||
headers | headers | |||
.filter((x) => x.active && x.key !== "") | .filter((x) => x.active && x.key !== "") | |||
.forEach((x) => (finalHeaders[x.key] = x.value)) | .forEach((x) => (finalHeaders[x.key] = x.value)) | |||
const reqOptions = { | const reqOptions = { | |||
method: "post", | method: "POST", | |||
url, | url, | |||
headers: { | headers: { | |||
...finalHeaders, | ...finalHeaders, | |||
"content-type": "application/json", | "content-type": "application/json", | |||
}, | }, | |||
data: introspectionQuery, | data: introspectionQuery, | |||
} | } | |||
const data = await sendNetworkRequest(reqOptions) | const data = await sendNetworkRequest(reqOptions) | |||
skipping to change at line 185 | skipping to change at line 185 | |||
} catch (e: any) { | } catch (e: any) { | |||
console.error(e) | console.error(e) | |||
this.disconnect() | this.disconnect() | |||
} | } | |||
} | } | |||
public async runQuery( | public async runQuery( | |||
url: string, | url: string, | |||
headers: GQLHeader[], | headers: GQLHeader[], | |||
query: string, | query: string, | |||
variables: string | variables: string, | |||
auth: HoppGQLAuth | ||||
) { | ) { | |||
const finalHeaders: Record<string, string> = {} | const finalHeaders: Record<string, string> = {} | |||
const parsedVariables = JSON.parse(variables || "{}") | ||||
const params: Record<string, string> = {} | ||||
if (auth.authActive) { | ||||
if (auth.authType === "basic") { | ||||
const username = auth.username | ||||
const password = auth.password | ||||
finalHeaders.Authorization = `Basic ${btoa(`${username}:${password}`)}` | ||||
} else if (auth.authType === "bearer" || auth.authType === "oauth-2") { | ||||
finalHeaders.Authorization = `Bearer ${auth.token}` | ||||
} else if (auth.authType === "api-key") { | ||||
const { key, value, addTo } = auth | ||||
if (addTo === "Headers") { | ||||
finalHeaders[key] = value | ||||
} else if (addTo === "Query params") { | ||||
params[key] = value | ||||
} | ||||
} | ||||
} | ||||
headers | headers | |||
.filter((item) => item.active && item.key !== "") | .filter((item) => item.active && item.key !== "") | |||
.forEach(({ key, value }) => (finalHeaders[key] = value)) | .forEach(({ key, value }) => (finalHeaders[key] = value)) | |||
const parsedVariables = JSON.parse(variables || "{}") | ||||
const reqOptions = { | const reqOptions = { | |||
method: "post", | method: "POST", | |||
url, | url, | |||
headers: { | headers: { | |||
...finalHeaders, | ...finalHeaders, | |||
"content-type": "application/json", | "content-type": "application/json", | |||
}, | }, | |||
data: JSON.stringify({ | data: JSON.stringify({ | |||
query, | query, | |||
variables: parsedVariables, | variables: parsedVariables, | |||
}), | }), | |||
params: { | ||||
...params, | ||||
}, | ||||
} | } | |||
const res = await sendNetworkRequest(reqOptions) | const res = await sendNetworkRequest(reqOptions) | |||
// HACK: Temporary trailing null character issue from the extension fix | // HACK: Temporary trailing null character issue from the extension fix | |||
const responseText = new TextDecoder("utf-8") | const responseText = new TextDecoder("utf-8") | |||
.decode(res.data) | .decode(res.data) | |||
.replace(/\0+$/, "") | .replace(/\0+$/, "") | |||
return responseText | return responseText | |||
End of changes. 7 change blocks. | ||||
6 lines changed or deleted | 30 lines changed or added |