Skip to content

Commit

Permalink
feat(tssdk): expose GraphQL client (#7294)
Browse files Browse the repository at this point in the history
Fixes #7159

Signed-off-by: Tom Chauveau <[email protected]>
  • Loading branch information
TomChv committed May 7, 2024
1 parent 37172fc commit 74437a2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Added-20240506-172454.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Added
body: Expose GraphQL client with getGQLClient in TypescriptSDK
time: 2024-05-06T17:24:54.589851+02:00
custom:
Author: TomChv
PR: "7294"
11 changes: 11 additions & 0 deletions cmd/codegen/generator/typescript/templates/src/object.ts.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ export class {{ .Name | QueryToClient | FormatName }} extends BaseClient {
{{- end }}
}

{{- /* Add custom method to main Client */ -}}
{{- if .Name | QueryToClient | FormatName | eq "Client" }}

/**
* Get the Raw GraphQL client.
*/
public getGQLClient() {
return this._ctx.getGQLClient()
}
{{- end }}

{{- /* Write methods. */ -}}
{{- "" }}{{ range $field := .Fields }}
{{- if Solve . }}
Expand Down
7 changes: 7 additions & 0 deletions sdk/typescript/api/client.gen.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions sdk/typescript/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ export class Context {
return this._client
}

public getGQLClient(): GraphQLClient {
if (!this._client) {
throw new Error(
"graphQL connection not established yet, please use it inside a connect or connection function.",
)
}

return this._client
}

/**
* Close the connection and the engine if this one was started by the node
* SDK.
Expand Down
21 changes: 21 additions & 0 deletions sdk/typescript/test/connect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ import * as bin from "../provisioning/bin.js"
import { CLI_VERSION } from "../provisioning/default.js"

describe("TypeScript default client", function () {
it("Should allow using the GQL client", async function () {
this.timeout(60000)

await connection(async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = await dag.getGQLClient().request<any>(`
query {
container {
from(address: "alpine") {
withExec(args: ["echo", "hello", "world"]) {
stdout
}
}
}
}
`)

assert.equal(result.container.from.withExec.stdout, "hello world\n")
})
})

it("Should use the default client and close connection on call to close", async function () {
this.timeout(60000)

Expand Down

0 comments on commit 74437a2

Please sign in to comment.