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

feat: ability to configure request handler #1271

Open
tusharmath opened this issue Mar 2, 2024 · 22 comments · May be fixed by #1863
Open

feat: ability to configure request handler #1271

tusharmath opened this issue Mar 2, 2024 · 22 comments · May be fixed by #1863

Comments

@tusharmath
Copy link
Contributor

Current
We can only setup one request handler in a JS worker file. This proposal intends to break that restriction

Proposal

type schema @upstream(onRequest: "onRequest") {
  # ... 
}

type Query {
  foo: Foo @http(path: "/foo", onRequest: "onRequestFoo")
  bar: Bar @http(path: "/bar", onRequest: "onRequestBar")
  baz: Bar @http(path: "/baz")
}
function onRequest (request) {
  // 
}

function onRequestFoo (request) {
  // 
}

function onRequestBar (request) {
  // 
}

In the above implementation when:

  1. We query foo, onRequestFoo is called with the request params.
  2. We query bar, onRequestBar is called with the request params.
  3. We query baz, onRequest (default handler) is called with the request params.
@tusharmath tusharmath changed the title ability to configure request handler feat: ability to configure request handler Mar 2, 2024
@tusharmath
Copy link
Contributor Author

/bounty 250

Copy link

algora-pbc bot commented Mar 2, 2024

💎 $250 bounty • Tailcall Inc.

Steps to solve:

  1. Start working: Comment /attempt #1271 with your implementation plan
  2. Submit work: Create a pull request including /claim #1271 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Additional opportunities:

🙏 Thank you for contributing to tailcallhq/tailcall!
🧐 Checkout our guidelines before you get started.

Attempt Started (GMT+0) Solution
🔴 @Bhavyajain21 Mar 2, 2024, 9:00:13 AM WIP
🟢 @webbdays Mar 17, 2024, 3:51:28 PM WIP
🔴 @Rutik7066 Apr 18, 2024, 5:31:33 AM WIP
🔴 @varshith257 May 15, 2024, 7:48:16 PM WIP

Copy link

algora-pbc bot commented Mar 2, 2024

@Bhavyajain21: The Tailcall Inc. team prefers to assign a single contributor to the issue rather than let anyone attempt it right away. We recommend waiting for a confirmation from a member before getting started.

@ologbonowiwi
Copy link
Contributor

@tusharmath why not add a new file instead of new functions on the single file? For large applications, it'd make it more maintainable for the users, and it follows the idea of SRP (as serverless for example, that you have one single entry point to each file implementation).
What do you think?

@ologbonowiwi
Copy link
Contributor

Just as suggestions, but anyways, I'll try to implement as it is.
/attempt #1271

@tusharmath
Copy link
Contributor Author

@ologbonowiwi We could build that into tailcall. But I would leave that up to the developer to figure out how they would like to bundle their js file. They write code in smaller files but ultimately need to expose one file that can be imported and executed.

@webbdays
Copy link
Contributor

webbdays commented Mar 10, 2024

@tusharmath
I think the user can have a logic like this in js worker.
pseudo code :

// onRequest being a generic handler/interceptor.
function onRequest ({request}) {
     // some logic to get path
     ......
    // switch case for path
    switch(request_path) {
        case "/foo":
            return onRequestFoo(request);
            break;
        case "/bar":
            return onRequestBar(request);
            break;
        default:
            // do nothing for others like Baz, just return request back to caller.
            return {request} ;
    }
}

function onRequestFoo ({request}) {
  //
}

function onRequestBar ({request}) {
  // 
}

This is very easy to user. Making onrequest a generic handler/interceptor.
No changes required in rust code.

What is the advantage the user gets if supposed to configure it in .graphql format?

Thanks.

@webbdays
Copy link
Contributor

@tusharmath
is this still in plan?

@tusharmath
Copy link
Contributor Author

Yes.

@webbdays
Copy link
Contributor

webbdays commented Mar 17, 2024

@tusharmath
all the http requests will go through the on_request method of RequestFilter struct
right?
Please confirm.

@tusharmath
Copy link
Contributor Author

Yes. The global one defined in upstream. This is happening right now also.

@webbdays
Copy link
Contributor

Hi @tusharmath,
I made it work by passing the onRequest as a header in the request to reach the on_request func in RequestFilter.
Once it reach the on_request func, i remove those headers and collect values so that we can use them there.
we should make sure we pass as headers and remove them before actual request is made.
we should handle like pass as headers only when js worker file is linked so that we can remove them in on_request func and use there.
which we can handle.
is this approach ok?
Thank you.

@webbdays
Copy link
Contributor

webbdays commented Mar 17, 2024

/attempt #1271

@webbdays
Copy link
Contributor

webbdays commented Mar 18, 2024

Hi @tusharmath,
Please comment on this.
Thank you.

@webbdays
Copy link
Contributor

webbdays commented Mar 18, 2024

Hi @tusharmath,
I have done with requested changes.
Please review.
Thank you.

@Rutik7066
Copy link
Contributor

Rutik7066 commented Apr 18, 2024

/attempt #1271

Algora profile Completed bounties Tech Active attempts Options
@Rutik7066    1 tailcallhq bounty
+ 4 bounties from 3 projects
Go, Rust,
TypeScript & more
Cancel attempt

Copy link

algora-pbc bot commented Apr 18, 2024

Note

The user @webbdays is already attempting to complete issue #1271 and claim the bounty. We recommend checking in on @webbdays's progress, and potentially collaborating, before starting a new solution.

@webbdays
Copy link
Contributor

Hi @Rutik7066,
its done.
merging is blocked due to #1570
try #1570

Copy link

algora-pbc bot commented Apr 19, 2024

@Rutik7066: Reminder that in 1 days the bounty will become up for grabs, so please submit a pull request before then 🙏

@webbdays webbdays linked a pull request May 5, 2024 that will close this issue
5 tasks
@varshith257
Copy link
Contributor

varshith257 commented May 15, 2024

/attempt #1271

I will take up on this important feature to be implemented as quickly as possible.

Algora profile Completed bounties Tech Active attempts Options
@varshith257 1 tailcallhq bounty
TypeScript, Go
Cancel attempt

Copy link

algora-pbc bot commented May 15, 2024

Note

The user @webbdays is already attempting to complete issue #1271 and claim the bounty. We recommend checking in on @webbdays's progress, and potentially collaborating, before starting a new solution.

Copy link

algora-pbc bot commented May 16, 2024

@varshith257: Reminder that in 1 days the bounty will become up for grabs, so please submit a pull request before then 🙏

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