#GraphQL and Middleware

12 messages · Page 1 of 1 (latest)

fading trail
#

Hello, I have a general question about NestJS basics.

I am working on the project and some functionality (fetch data from DB) needs to be done before it hits the GraphQL resolver.

Reading the documentation middleware can help to accomplish this mission. However, it seems like middleware does not exactly support GraphQL (can't get a request body/middleware creating a different session rather than GraphQL/etc).

Then I thought to use Guards BUT, Guards is designed to prevent from resolver being executed if the data isn't match.

So what is the right way to accomplish this kind of task?

hearty tree
#

can't get a request body/middleware creating a different session rather than GraphQL

What is it you are trying to accomplish in general?

fading trail
hearty tree
#

"Fetch data from the DB" doesn't tell me anything. For what purposes do you need data from the DB?

fading trail
# hearty tree "Fetch data from the DB" doesn't tell me anything. For what purposes do you need...

Well, I tried to understand which is the best way to fetch some data from DB, append it to context, and hit the resolver after all data is collected. I tried to use middleware (which is the most popular way in most other frameworks) to accomplish this task. I need to use session and middleware uses different sessions rather than GraphQL - it just creates a new session and does not reuse the existing one even if the cookie with session ID is provided.
Then I tried to use guards but seems like it's for the other purpose.

The goal is:

upsert customer > if ok > do some calculations > if ok > create the session > if ok > hit the resolver

Then, the resolver can map some data to the OK response.

hearty tree
#

So, you are trying to build a process for sessions i.e. user auth?

#

Though, rereading the goal, that would be a strange path to create sessions.

#

Can you please explain the use case better? You are explaining what it is you want the system to do, not what process that you are facing.

fading trail
# hearty tree Can you please explain the use case better? You are explaining what it is you wa...

I'm trying to build a process that needs:

  1. Upsert the customer and return the customer.
  2. Check if the customer is blocked or not.
  3. Check if he already started the process previously, if yes, retrieve the data.
  4. Store in each step data in the SAME session.

This is what I'm trying to accomplish and not how. The how to do it (in the better way) - this is my question.

Currently, I understand that I need to build multiple services and just check procedurally each one.

I wondered if there is a more elegant way to make it: break it down to middlewares/guards/whatever.

hearty tree
#

Still not answering what I need to know.:) So I'll ask now specific questions.

Why does the customer need to be upserted? What process is formally being kicked off here? Is it a webshop's basket session we are speaking of?

How are you getting the customer's id?

Why would a customer be blocked?

Why would the customer have started the process previously?

Why would the previous process need to be stored in the "session"?

What is this "session" exactly?

Why did you think this should be in a middleware to begin with?

I know I'm probably digging deeper than I need to, but the answers will show me (and hopefully you too) where all this logic needs to be. My first impression is, it needs to be in a service, because it sounds complicated, but I'll wait for your responses.

fading trail
# hearty tree Still not answering what I need to know.:) So I'll ask now specific questions. ...

NVM. I just created a multiple services and in the resolver checked multiple conditions.

Since I understand that RestAPI and GraphQL using a completely different sessions context.req.session.

        const customer = await this.customerService.findOrCreateCustomer(initLoanRequestInput);
        if (condition1){ throw new BadRequestException()}
        if (condition2){ throw new BadRequestException()}
        if (condition3){ throw new BadRequestException()}
        if (condition4){ throw new BadRequestException()}
        
        const payload = {
            ...data,
            cost: data.x + data.y
        }
        
        return payload;

Other framework in other language:

  1. Middleware1: Upsert customer + store the customer ID in the session (server-side session/using Redis/using mysql/you name it) - if failed throw an error.
  2. Middleware2: Get the customer from session + if the customer is blocked - if yes, throw an error.
  3. Middleware3: If middleware1 + middleware2 is ok, then append additional data.
  4. The resolver has a single responsibility to parse and return data that is for sure exists (not need to do if...if...if...if...if).

In this case, if I try to do the same process/or part of it in any other place I can just reuse the middleware.

In Nest, I understand I'll need to create resolver2 and copy-paste the whole previous code again (not DRY) or create many services that can handle each case (Middleware1+Middleware2 or Middleware1+Middleware3 or Middleware1+Middleware2+Middleware3)

That why I ask if there is any elegant way to make the code more robust and reusable.

hearty tree
#

Since I understand that RestAPI and GraphQL using a completely different sessions context.req.session
Not that I am aware of.