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

Improve server-side operations API #2044

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open

Conversation

sodic
Copy link
Contributor

@sodic sodic commented May 20, 2024

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 adding User 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:

  • Make an issue for supporting User.
  • Test thoroughly one more time.
  • Update public API table.
  • Update docs.
  • Update changelog.

@sodic sodic marked this pull request as draft May 20, 2024 17:52
@sodic sodic force-pushed the filip-1909-server-side-ops branch from 40775e4 to 006085c Compare May 21, 2024 14:34

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<
Copy link
Contributor Author

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).

Copy link
Contributor Author

@sodic sodic May 22, 2024

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.

Copy link
Contributor Author

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.

@sodic sodic marked this pull request as ready for review May 22, 2024 15:42
@sodic sodic requested review from infomiho and Martinsos May 22, 2024 15:53
Copy link
Member

@Martinsos Martinsos left a 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.

- Make sure you pass in a context object with the user to authenticated Queries.

<Tabs groupId="js-ts">
<TabItem value="js" label="JavaScript">
Copy link
Contributor

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.

Copy link
Contributor Author

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')
Copy link
Contributor

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!
Copy link
Contributor

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 🤷

@infomiho
Copy link
Contributor

Good job!

It seems like the AuthUser is causing some trouble for us here 😢 they can simply coerce some object like { id: 5 } as AuthUser as an extreme measure?

Copy link
Member

@Martinsos Martinsos left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rethink server-side Operations API
3 participants