Skip to content

Commit

Permalink
Merge pull request #656 from contember/refactor/client
Browse files Browse the repository at this point in the history
content client minor improvements
  • Loading branch information
matej21 authored Dec 21, 2023
2 parents 506986b + b4215d1 commit 3608cd2
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 165 deletions.
2 changes: 1 addition & 1 deletion build/api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"enabled": false
},
"apiReport": {
"enabled": true,
"enabled": true,
"reportFolder": "./api"
},
"newlineKind": "lf"
Expand Down
6 changes: 2 additions & 4 deletions build/api/client-content.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
```ts

import { GraphQlClient } from '@contember/graphql-client';
import { GraphQlClientRequestOptions } from '@contember/graphql-client';
import { GraphQlFieldTypedArgs } from '@contember/graphql-builder';
import { GraphQlSelectionSet } from '@contember/graphql-builder';
Expand All @@ -13,7 +14,7 @@ import { Result } from '@contember/schema';

// @public (undocumented)
export class ContentClient {
constructor(executor: QueryExecutor);
constructor(client: Pick<GraphQlClient, 'execute'>);
// (undocumented)
mutate<Value>(mutation: ContentMutation<Value>, options?: QueryExecutorOptions): Promise<Value>;
// (undocumented)
Expand Down Expand Up @@ -377,9 +378,6 @@ export type MutationTransactionOptions = {
// @public (undocumented)
export type Path = Array<FieldPath | IndexPath>;

// @public (undocumented)
export type QueryExecutor = <T = unknown>(query: string, options: GraphQlClientRequestOptions) => Promise<T>;

// @public (undocumented)
export type QueryExecutorOptions = GraphQlClientRequestOptions;

Expand Down
2 changes: 1 addition & 1 deletion packages/binding/src/core/DataBinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class DataBinding<Node> {
persist: async options => await this.persist(options),
})
this.queryBuilder = createQueryBuilder(this.environment.getSchema())
this.contentClient = new ContentClient(contentApiClient.execute.bind(contentApiClient))
this.contentClient = new ContentClient(contentApiClient)
}

private async persist({ onPersistError, onPersistSuccess, signal }: PersistOptions = {}) {
Expand Down
26 changes: 24 additions & 2 deletions packages/client-content-generator/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,33 @@ import { ContemberClientGenerator } from './ContemberClientGenerator';
const outDir = process.argv[3]

if (!schemaPath || !outDir) {
console.error(`Usage: yarn contember-client-generator <schema.json> <out-dir>`)
console.error(`Usage:
From file:
yarn contember-client-generator <schema.json> <out-dir>
From stdin:
yarn run --silent contember project:print-schema --format=schema | yarn contember-client-generator - <out-dir>
`)
process.exit(1)
}

const source = JSON.parse(await fs.readFile(resolve(process.cwd(), process.argv[2]), 'utf8'))
const sourceData = await (async () => {
if (schemaPath === '-') {
if (process.stdin.isTTY) {
throw new Error('Cannot read from stdin in TTY mode')
}
const buffer = []
for await (const chunk of process.stdin) {
buffer.push(chunk)
}
return Buffer.concat(buffer).toString('utf8')
}
return await fs.readFile(resolve(process.cwd(), process.argv[2]), 'utf8')
})()

const source = JSON.parse(sourceData)

const dir = resolve(process.cwd(), process.argv[3])
const generator = new ContemberClientGenerator()
const result = generator.generate(source.model)
Expand Down
13 changes: 6 additions & 7 deletions packages/client-content/src/ContentClient.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { mutationFragments } from './utils/mutationFragments'
import { GraphQlClientRequestOptions } from '@contember/graphql-client'
import { GraphQlClient, GraphQlClientRequestOptions } from '@contember/graphql-client'
import { GraphQlQueryPrinter } from '@contember/graphql-builder'
import { ContentMutation, ContentQuery } from './nodes'
import { createMutationOperationSet } from './utils/createMutationOperationSet'
import { createQueryOperationSet } from './utils/createQueryOperationSet'


export type QueryExecutorOptions = GraphQlClientRequestOptions

export type QueryExecutor = <T = unknown>(query: string, options: GraphQlClientRequestOptions) => Promise<T>
export type QueryExecutorOptions =
& GraphQlClientRequestOptions

export class ContentClient {
constructor(
private readonly executor: QueryExecutor,
private readonly client: Pick<GraphQlClient, 'execute'>,
) {
}

Expand All @@ -23,7 +22,7 @@ export class ContentClient {
const printer = new GraphQlQueryPrinter()
const operationSet = createQueryOperationSet(queries)
const { query, variables } = printer.printDocument('query', operationSet.selection, {})
const result = await this.executor(query, { variables, ...options })
const result = await this.client.execute(query, { variables, ...options })
return operationSet.parse(result)
}

Expand All @@ -35,7 +34,7 @@ export class ContentClient {

const printer = new GraphQlQueryPrinter()
const { query, variables } = printer.printDocument('mutation', operationSet.selection, mutationFragments)
const result = await this.executor(query, { variables, ...options })
const result = await this.client.execute(query, { variables, ...options })

return operationSet.parse(result)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/client-content/tests/cases/unit/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,14 @@ export const qb = new ContentQueryBuilder({

export const createClient = (result?: any) => {
const calls: { query: string, variables: Record<string, unknown> }[] = []
const client = new ContentClient(<T>(query: string, options: any): Promise<T> => {
const client = new ContentClient({
execute: <T>(query: string, options: any): Promise<T> => {
calls.push({
query,
...options,
})
return Promise.resolve(result ?? {})
},
)
})
return [client, calls] as const
}
Loading

0 comments on commit 3608cd2

Please sign in to comment.