Blitz: Potential bug in `ctx: {session?: SessionContext} = {}` and then its `ctx.session!` usage

Created on 31 Jul 2020  Â·  6Comments  Â·  Source: blitz-js/blitz

What is the problem?

Why is the following argument used for the context when the session middleware is in place?

function someQuery(input: any, ctx: {session?: SessionContext} = {}) { /* … */ }

This holds the potential for unexpected errors when using like ctx.session!, as shown in the auth example docs. I would suggest the following syntax if the session is always available:

function someQuery(input: any, ctx: {session: SessionContext}) { /* … */ }

Otherwise, if the ctx argument may be missing, then check for its existence in runtime instead of falsely asserting something with TypeScript.

kinbad-experience needs investigation statudone

All 6 comments

I realized that the initial method signature is like that to make it callable without explicitly specifying a second argument:

That didn't seem to be intuitive at first. I think a wrapper like ssrQuery should always be present even on the server, and for both queries and mutations.

cc @flybayer

@kripod so sorry for the long delay responding! I was traveling this past week and focused on getting the auth release out the door.

You are totally right about this being weird. This is the one remaining sore spot in Blitz apps that we need to find a solution for.

We could definitely add a higher order function to solve this, but if possible I'd love to keep the default export for queries and mutations as a plain, unwrapped function. This needs some exploration and probably some discussion with TS experts.

If anyone has ideas on this, please share!

The other thing here is how to add types for global middleware. Like if you add custom global middleware, what's the best way to get that fully typed in all your queries and mutations.

Here's an RFC for a solution to this!

Closed in #1160

Was this page helpful?
0 / 5 - 0 ratings