Skip to content

Instantly share code, notes, and snippets.

@Zeko369
Created September 1, 2023 13:03
Show Gist options
  • Select an option

  • Save Zeko369/0f4daa32de756e2067cba80b1f69d1e6 to your computer and use it in GitHub Desktop.

Select an option

Save Zeko369/0f4daa32de756e2067cba80b1f69d1e6 to your computer and use it in GitHub Desktop.
import prisma from "../dbClient.js";
import { getTasks } from "../ext-src/queries.js";
export default async function (args, context) {
return (getTasks as any)(args, {
...context,
entities: {
Task: prisma.task,
},
});
}
export type GetTasks = typeof getTasks;
type Context = string;
// const defineAction = <Args>(
// action: (args: Args, ctx: Context) => Promise<number>,
// ) => {
// return action;
// };
// server code
const myAction = defineAction({}, async (args, context) => {
return 10;
});
const out = await myAction({}, "context");
// client code
const myClientActions = async (args: number, context: any): Promise<string> => {
throw new Error("Pls call this with useaction or callSomthing");
};
myClientActions.type = { actionName: "myAction", _implementation: myAction };
const useMutation = <T>(act: { _implementation: T }): [T] =>
// @ts-ignore
[null as any] as const;
const [act] = useMutation(myClientActions);
// act(args);
const callAction = (act, args) => {
const context = somehowGetContext();
act(args, context);
};
callAction(action, {});
// sdk
const defineActions = (
actions: (args: string, ctx: number) => Promise<string>,
) => {
const newAction = (args) => actions(args, 10);
newAction.with = (args: string, ctx: number) => actions(args, ctx);
newAction.type = { actionName: "myAction", refetch: ["User", "Post"] };
return newAction;
};
// server code
const myAction2 = defineActions(async (args, context) => {
return "10";
});
// client code
myAction2("asd");
// server code importting client code
myAction2.with("asd", 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment