Created
September 1, 2023 13:03
-
-
Save Zeko369/0f4daa32de756e2067cba80b1f69d1e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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