Skip to content

Commit

Permalink
computed view endpoint for api client
Browse files Browse the repository at this point in the history
  • Loading branch information
mkobetic committed Nov 4, 2024
1 parent 798fe22 commit 62ac24f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/api-client-core/src/GadgetFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FieldSelection } from "tiny-graphql-query-compiler";
import type { GadgetRecord, RecordShape } from "./GadgetRecord.js";
import type { GadgetRecordList } from "./GadgetRecordList.js";
import type { LimitToKnownKeys, VariablesOptions } from "./types.js";
Expand Down Expand Up @@ -206,3 +207,13 @@ export interface GlobalActionFunction<VariablesT> {

export type AnyActionFunction = ActionFunctionMetadata<any, any, any, any, any, any> | GlobalActionFunction<any>;
export type AnyBulkActionFunction = ActionFunctionMetadata<any, any, any, any, any, true>;

export interface ComputedViewFunction<VariablesT, ResultT> {
(variables: VariablesT): Promise<ResultT>;
type: "computedView";
operationName: string;
namespace: string | string[] | null;
variables: VariablesOptions;
variablesType: VariablesT;
selections: FieldSelection;
}
21 changes: 21 additions & 0 deletions packages/api-client-core/src/operationBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,27 @@ export const globalActionOperation = (
});
};

export const computedViewOperation = (
operation: string,
variables: VariablesOptions,
selections: FieldSelection,
namespace?: string | string[] | null
) => {
let fields = {
[operation]: Call(variableOptionsToVariables(variables), fieldSelectionToQueryCompilerFields(selections, true)),
};

if (namespace) {
fields = namespacify(namespace, fields);
}

return compileWithVariableValues({
type: "query",
name: operation,
fields,
});
};

export interface GraphQLBackgroundActionOptions {
retries?: { retryCount: number };
queue?: { name: string; maxConcurrency?: number };
Expand Down
14 changes: 14 additions & 0 deletions packages/api-client-core/src/operationRunners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { AnyModelManager } from "./ModelManager.js";
import {
actionOperation,
backgroundActionResultOperation,
computedViewOperation,
enqueueActionOperation,
findManyOperation,
findOneByFieldOperation,
Expand Down Expand Up @@ -384,6 +385,19 @@ export const globalActionRunner = async (
return assertMutationSuccess(response, dataPath).result;
};

export const computedViewRunner = async (
connection: GadgetConnection,
operation: string,
variables: VariablesOptions,
selections: FieldSelection,
namespace?: string | string[] | null
) => {
const plan = computedViewOperation(operation, variables, selections, namespace);
const response = await connection.currentClient.query(plan.query, plan.variables);
const dataPath = namespaceDataPath([operation], namespace);
return assertOperationSuccess(response, dataPath);
};

export async function enqueueActionRunner<SchemaT, Action extends AnyBulkActionFunction, Result = BackgroundActionHandle<SchemaT, Action>>(
connection: GadgetConnection,
action: Action,
Expand Down

0 comments on commit 62ac24f

Please sign in to comment.