tailcall icon indicating copy to clipboard operation
tailcall copied to clipboard

feat: ability to configure request handler

Open tusharmath opened this issue 1 year ago • 21 comments

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 avatar Mar 02 '24 08:03 tusharmath

/bounty 250

tusharmath avatar Mar 02 '24 08:03 tusharmath

💎 $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

algora-pbc[bot] avatar Mar 02 '24 08:03 algora-pbc[bot]

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

algora-pbc[bot] avatar Mar 02 '24 09:03 algora-pbc[bot]

@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?

wesleymatosdev avatar Mar 02 '24 14:03 wesleymatosdev

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

wesleymatosdev avatar Mar 02 '24 14:03 wesleymatosdev

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

tusharmath avatar Mar 04 '24 07:03 tusharmath

@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 avatar Mar 10 '24 15:03 webbdays

@tusharmath is this still in plan?

webbdays avatar Mar 17 '24 04:03 webbdays

Yes.

tusharmath avatar Mar 17 '24 04:03 tusharmath

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

webbdays avatar Mar 17 '24 05:03 webbdays

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

tusharmath avatar Mar 17 '24 05:03 tusharmath

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 avatar Mar 17 '24 15:03 webbdays

/attempt #1271

Options

webbdays avatar Mar 17 '24 15:03 webbdays

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

webbdays avatar Mar 18 '24 05:03 webbdays

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

webbdays avatar Mar 18 '24 15:03 webbdays

/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

rutikthakre avatar Apr 18 '24 05:04 rutikthakre

[!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.

algora-pbc[bot] avatar Apr 18 '24 05:04 algora-pbc[bot]

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

webbdays avatar Apr 18 '24 05:04 webbdays

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

algora-pbc[bot] avatar Apr 19 '24 05:04 algora-pbc[bot]

/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

varshith257 avatar May 15 '24 19:05 varshith257

[!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.

algora-pbc[bot] avatar May 15 '24 19:05 algora-pbc[bot]

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

algora-pbc[bot] avatar May 16 '24 19:05 algora-pbc[bot]

💡 @webbdays submitted a pull request that claims the bounty. You can visit your bounty board to reward.

algora-pbc[bot] avatar Jun 15 '24 03:06 algora-pbc[bot]

Hi @tusharmath, Please process this bounty. Still in pending.

webbdays avatar Jun 28 '24 05:06 webbdays

🎉🎈 @webbdays has been awarded $250! 🎈🎊

algora-pbc[bot] avatar Jul 17 '24 10:07 algora-pbc[bot]

@tusharmath algora sent bounty now. I have got 2 now. tip and this one PLease look into it.

webbdays avatar Jul 17 '24 16:07 webbdays