-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Improve server-side operations API #2044
base: main
Are you sure you want to change the base?
Conversation
40775e4
to
006085c
Compare
|
||
export * from "./taggedEntities" | ||
export * from "./serialization" | ||
|
||
export type Query<Entities extends _Entity[], Input extends Payload, Output extends Payload> = | ||
Operation<Entities, Input, Output> | ||
export type UnauthenticatedQueryDefinition< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Definition
suffix is appropriate here, as we're using these generics to construct the types users assign to their operation definitions.
We want to use Query
, Action
, etc. for typing the user-facing API for calling the operations (see wrappers.ts
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the machinery. I've added comments to help understanding because it's pretty complex.
Please comment if something is still unclear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll find a better way to test/specify this behavior before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice stuff! This will be great now. You get my approval, but you do need to finish stuff from the TODO list + also add to ChangeLog.
waspc/data/Generator/templates/sdk/wasp/server/operations/wrappers.ts
Outdated
Show resolved
Hide resolved
waspc/data/Generator/templates/sdk/wasp/server/operations/wrappers.ts
Outdated
Show resolved
Hide resolved
waspc/data/Generator/templates/sdk/wasp/server/operations/wrappers.ts
Outdated
Show resolved
Hide resolved
- Make sure you pass in a context object with the user to authenticated Queries. | ||
|
||
<Tabs groupId="js-ts"> | ||
<TabItem value="js" label="JavaScript"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These examples are "floating" without a function in which they are called. Is it an another action or query? Is it API? Also, how do I acquire an AuthUser
?
Making the example more practical will be more ... practical for the users since they will want to do this in operations anyways and for that use case we have a good answer for them how to get the AuthUser
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You got me.
I left it out on purpose because I didn't want to admit the API is still crippled.
Anyway, I'll improve what the comment says and make sure we tackle #2066 as soon as possible.
// https://github.com/wasp-lang/wasp/issues/2050 | ||
if (args.length < 1) { | ||
// No arguments sent -> no user and no payload specified -> there's no way this was called correctly. | ||
throw new Error('Invalid number of arguments') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd maybe expand on the error and help the users to move forward. "You called an operation without arguments. Since this operation requires auth..."
const result1 = await waspVoidToNumberNoAuth() | ||
const result2 = await waspBoolToNumberNoAuth(true) | ||
|
||
const user = context.user! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are keeping these tests, I'd recommend using a conditional statement here so the action can work even without the user 🤷
Good job! It seems like the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think one or two comments are ongoing, but they should be clear enough to finish from here. So approval from me! But @infomiho will be able to cover the TS ground more thoroughly.
Closes #1909 using the approach described in the last comment on the issue.
Note
I've only implemented the API for
AuthUser
for now. I think I'll make another issue that deals with addingUser
to the mix because it's probably not important enough to justify the time it would take.Tip
I recommend generating the files by running
wasp start
and looking at the SDK. It's much easier to understand what's going on this way. Choose an app with many operations (e.g.,waspc/todoApp
)Warning
I love these admonitions and have started overusing them.
Todos:
User
.