Skip to content

Commit 39acd6c

Browse files
matej21claude
andcommitted
refactor: expose graphQlClient in bindx context for S3 uploader
Replace hacky type assertion in useS3Client with proper context-based access. Introduces BindxGraphQlClient interface to avoid cross-package coupling to concrete GraphQlClient class. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 002896e commit 39acd6c

7 files changed

Lines changed: 22 additions & 12 deletions

File tree

packages/bindx-react/src/hooks/BackendAdapterContext.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import { SchemaRegistry } from '@contember/bindx'
88
import { UndoManager } from '@contember/bindx'
99
import { QueryBatcher } from '../batching/QueryBatcher.js'
1010

11+
/**
12+
* Minimal GraphQL client interface exposed through bindx context.
13+
* Avoids coupling to concrete GraphQlClient class across package boundaries.
14+
*/
15+
export interface BindxGraphQlClient {
16+
execute<T = unknown>(query: string, options?: { variables?: Record<string, unknown> }): Promise<T>
17+
}
18+
1119
/**
1220
* Context value containing all bindx services
1321
*/
@@ -26,6 +34,8 @@ export interface BindxContextValue {
2634
schema: SchemaRegistry
2735
/** Undo manager (if enabled) */
2836
undoManager: UndoManager | null
37+
/** GraphQL client (available when using ContemberBindxProvider) */
38+
graphQlClient: BindxGraphQlClient | null
2939
/** Whether debug logging is enabled */
3040
debug: boolean
3141
}
@@ -122,6 +132,7 @@ export function BindxProvider({
122132
batchPersister,
123133
schema: schemaRegistry,
124134
undoManager,
135+
graphQlClient: null,
125136
debug,
126137
}
127138
}, [adapter, customStore, schemaDefinition, customMutationCollector, enableUndo, undoConfig, defaultUpdateMode, debug])

packages/bindx-react/src/hooks/ContemberBindxProvider.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export const ContemberBindxProvider = memo(function ContemberBindxProvider({
140140
batchPersister,
141141
schema: schemaRegistry,
142142
undoManager,
143+
graphQlClient,
143144
debug,
144145
}
145146
}, [schema, customStore, undoManagerProp, undoConfig, defaultUpdateMode, debug])

packages/bindx-react/src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export {
88
useSchemaRegistry,
99
type BindxProviderProps,
1010
type BindxContextValue,
11+
type BindxGraphQlClient,
1112
} from './BackendAdapterContext.js'
1213

1314
export {

packages/bindx-react/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export {
184184
export type {
185185
BindxProviderProps,
186186
BindxContextValue,
187+
BindxGraphQlClient,
187188
UseEntityOptions,
188189
UseEntityListOptions,
189190
LoadingEntityResult,

packages/bindx-uploader/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"@contember/bindx": "workspace:*",
1919
"@contember/bindx-react": "workspace:*",
2020
"@contember/bindx-repeater": "workspace:*",
21-
"@contember/graphql-client": "*",
2221
"@radix-ui/react-slot": "^1.2.3",
2322
"react-dropzone": "^14.3.8"
2423
},

packages/bindx-uploader/src/hooks/useS3Client.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,20 @@ import { S3UploadClient, type S3UploadClientOptions } from '../uploadClient/S3Up
44
import { createContentApiS3Signer } from '../utils/urlSigner.js'
55

66
/**
7-
* Creates an S3 upload client using the current Contember GraphQL client.
8-
* Uses the bindx context to get the GraphQL client for URL signing.
7+
* Creates an S3 upload client using the GraphQL client from bindx context.
8+
* Requires ContemberBindxProvider (which provides graphQlClient).
99
*/
1010
export const useS3Client = (options: Partial<S3UploadClientOptions> = {}): S3UploadClient => {
11-
const { adapter } = useBindxContext()
11+
const { graphQlClient } = useBindxContext()
1212

1313
return useMemo(() => {
14-
// Get the GraphQL client from the adapter
15-
// ContemberAdapter has a graphQlClient property
16-
const graphQlClient = (adapter as { graphQlClient?: { execute: (query: string, options?: unknown) => Promise<unknown> } }).graphQlClient
1714
if (!graphQlClient) {
18-
throw new Error('useS3Client requires a Contember adapter with GraphQL client')
15+
throw new Error('useS3Client requires ContemberBindxProvider (graphQlClient not available in context)')
1916
}
2017

2118
return new S3UploadClient({
22-
signUrl: createContentApiS3Signer(graphQlClient as Parameters<typeof createContentApiS3Signer>[0]),
19+
signUrl: createContentApiS3Signer(graphQlClient),
2320
...options,
2421
})
25-
}, [adapter, options])
22+
}, [graphQlClient, options])
2623
}

packages/bindx-uploader/src/utils/urlSigner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { GraphQlClient } from '@contember/graphql-client'
1+
import type { BindxGraphQlClient } from '@contember/bindx-react'
22
import type { S3FileParameters, S3SignedUrlResponse } from '../uploadClient/types.js'
33

44
/**
55
* Creates an S3 URL signer that batches requests to reduce API calls.
66
* Uses microtask scheduling to batch concurrent signing requests into a single GraphQL mutation.
77
*/
88
export const createContentApiS3Signer = (
9-
client: GraphQlClient,
9+
client: BindxGraphQlClient,
1010
): ((parameters: S3FileParameters) => Promise<S3SignedUrlResponse>) => {
1111
let uploadUrlBatchParameters: S3FileParameters[] = []
1212
let uploadUrlBatchResult: null | Promise<Record<string, S3SignedUrlResponse>> = null

0 commit comments

Comments
 (0)