Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

`req` not persisted in the local API

default discord avatar
ssyberg2 years ago
4

I'm trying to set some "request" session data for use throughout a single express request (graphql query in this case). I'm setting some data on the

req

object in my custom query but I find this data is no longer available in "nested" API calls / hooks. For a more concrete example:



1. Set

req.foo

in custom query resolver


2. Run a local API call in the resolver


3. The returned result contains another local API call in a afterRead hook ("virtual field") - in this hook

req.foo

is no longer set



I know I can set this on the payload object itself and it persists, but it also persists across sessions. What's the proper pattern for persisting req data throughout a single request available throughout the system?



Should I just be handling this in some pre/post middleware somehow? Even that route it's not clear to me the best approach...

  • default discord avatar
    notchr2 years ago
    @778799229988110337

    Out of curiosity, are you calling save on the session on req?



    I cant get the anchor link, but check out

    https://www.npmjs.com/package/express-session

    The session data is in memory until .save is explicitly called



    Though it says in the docs that the method typically does NOT need to be called, hmmmmm



    Maybe it will be helpful to share some code examples of how you're attempting to update the session data

  • default discord avatar
    ssyberg2 years ago

    Session was probably a poor use of words here, I'm not actually using a session/cookie at all, I'm just trying to persist some data through a single express request.



    What I think is happening is because the hook in question is nested via a relationship on the top level

    find

    call, the

    req

    is not persisted in whatever is happening under the hood when resolving relationships in a

    find
  • default discord avatar
    notchr2 years ago

    It's just odd because the session should stay consistent as it is used for auth. So if you update the session, the data should persist right?



    Oh persist data through a request?



    The req object only holds data per-request unless it is bound to a session, no?

  • default discord avatar
    ssyberg2 years ago

    This is a single request, I make an api call, I add an attribute to the

    req

    object and then I have console.logs scattered thoughout to see when it disappears.



    This appears to occur on any local API calls - so for example I do a request for a blog post, the

    req.foo

    attribute is set in a pre middleware, I can see

    req.foo

    in the hooks on the blog post, but if those hooks have subsequent API calls,

    req.foo

    disappears



    I suspect this is related to this question

    https://discord.com/channels/967097582721572934/1109113967034511413/1109113967034511413

    Ok after some more experimenting I've confirmed that

    req

    is not persisted in local API calls



    req

    not persisted in the local API



    I'm going to submit this as a bug unless someone thinks this is intended behavior

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.