Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add action result type #352

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions packages/api-client-core/src/BackgroundActionHandle.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import type { GadgetConnection } from "./GadgetConnection.js";
import type { AnyActionFunction } from "./GadgetFunctions.js";
import type { ActionFunction, AnyActionFunction } from "./GadgetFunctions.js";
import type { GadgetRecord } from "./GadgetRecord.js";
import { actionResultRunner } from "./operationRunners.js";
import type { ActionFunctionOptions } from "./types.js";
import type { ActionFunctionOptions, DefaultSelection, Select } from "./types.js";

export type BackgroundActionResult<R = any> = {
export type BackgroundActionResultData<
Action extends AnyActionFunction,
Options extends ActionFunctionOptions<Action>
> = Action extends ActionFunction<any, any, any, any, any>
? Action["hasReturnType"] extends true
? any
: GadgetRecord<
Select<
Exclude<Action["schemaType"], null | undefined>,
DefaultSelection<Action["selectionType"], Options, Action["defaultSelection"]>
>
>
: any;

export type BackgroundActionResult<Action extends AnyActionFunction, Options extends ActionFunctionOptions<Action>> = {
id: string;
outcome: string | null;
result: R | null;
result: BackgroundActionResultData<Action, Options>;
};

/** Represents a handle to a background action which has been enqueued */
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client-core/src/operationRunners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export const actionResultRunner = async <Action extends AnyActionFunction, Optio
id: string,
action: Action,
options?: Options
): Promise<BackgroundActionResult> => {
): Promise<BackgroundActionResult<Action, Options>> => {
const plan = actionResultOperation(id, action, options);
const subscription = connection.currentClient.subscription(plan.query, plan.variables);

Expand Down
4 changes: 2 additions & 2 deletions packages/api-client-core/src/support.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { OperationResult } from "@urql/core";
import { CombinedError } from "@urql/core";
import { DataHydrator } from "./DataHydrator.js";
import type { ActionFunctionMetadata, AnyActionFunction } from "./GadgetFunctions.js";
import type { ActionFunctionMetadata, AnyActionFunction, GlobalActionFunction } from "./GadgetFunctions.js";
import type { RecordShape } from "./GadgetRecord.js";
import { GadgetRecord } from "./GadgetRecord.js";
import type { VariablesOptions } from "./types.js";
Expand Down Expand Up @@ -602,7 +602,7 @@ export const disambiguateActionVariables = (action: AnyActionFunction, variables
* Normalizes incoming params from JS land into the variable format the GraphQL API is expecting
**/
export const disambiguateBulkActionVariables = (
action: ActionFunctionMetadata<any, any, any, any, any, true>,
action: ActionFunctionMetadata<any, any, any, any, any, true> | GlobalActionFunction<any>,
inputs: Record<string, any> = {}
) => {
let variables;
Expand Down