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

Use context in prefetching #671

Closed
Errorname opened this issue Jul 20, 2020 · 10 comments · Fixed by blitz-js/blitz#791
Closed

Use context in prefetching #671

Errorname opened this issue Jul 20, 2020 · 10 comments · Fixed by blitz-js/blitz#791

Comments

@Errorname
Copy link

What do you want and why?

I am currently writing a Blitz middleware to integrate Prisma-multi-tenant. (See package: https://github.com/Errorname/prisma-multi-tenant/tree/master/packages/blitz)


This is how it works:

  1. You add the middleware to your blitz.config.js file:
const { multiTenantMiddleware } = require('@prisma-multi-tenant/blitz')

module.exports = {
  // ...
  middleware: [
    multiTenantMiddleware((req, res) => {
      // The name can come from anywhere (headers, token, ...)
      return 'my_tenant_A'
    }),
  ],
}
  1. Then, in your queries and mutation, you access the tenant from the context:
export default async function getProjects(args, ctx) {
  const projects = await ctx.db.project.findMany(args)

  return projects
}

This code is currently working, and adds multi-tenancy to a Blitz application. However, it outputs long errors in the console when running blitz start.

Click to see error

(node:12661) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'project' of undefined
    at getProjects (webpack-internal:///./app/_resolvers/projects/queries/getProjects.ts:11:33)
    at queryFn (/Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/@blitzjs/core/dist/core.cjs.development.js:133:14)
    at /Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:801:26
    at _catch$1 (/Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:373:20)
    at /Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:799:14
    at /Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:402:34
    at /Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:972:40
    at _catch$1 (/Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:373:20)
    at /Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:966:18
    at /Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:402:34
    at Object.<anonymous> (/Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:1003:11)
    at Object.fetch (/Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:402:34)
    at handleSuspense (/Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:1587:31)
    at Object.useQuery (/Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/react-query/dist/react-query.development.js:1840:5)
    at useQuery (/Users/errorname/Documents/Vrac/hello-prisma2/blitz-app/node_modules/@blitzjs/core/dist/core.cjs.development.js:130:35)
    at ProjectsList (webpack-internal:///./pages/projects/index.tsx:14:76)
(node:12661) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 7)

This error is thrown because Blitz first try to prefetch the response, but doesn't include the context:

https://github.com/blitz-js/blitz/blob/canary/packages/core/src/use-query.ts#L62

queryFn: (_: string, params) => queryRpcFn(params, {fromQueryHook: true})

The prefetching features comes from this issue and was implemented in this PR

Possible implementation(s)

I tried to look into Blitz code, but I haven't figured out a way to make this possible.

@flybayer
Copy link
Member

Hey, that sounds awesome!

I can look more into this tomorrow, but the queryFn warm request is an empty HEAD request that doesn't actually call the query function. So that means if getProjects is running, then it's from the main request.

@Errorname
Copy link
Author

Thanks for your answer! I may be missing something, but when adding some logging:

app/projects/queries/getProjects.ts

export default async function getProjects(
  { where, orderBy, cursor, take, skip }: GetProjectsInput,
  ctx: Record<any, any> = {}
) {

  console.log(Object.keys(ctx)) // Prints the keys of the ctx argument

  const projects = await ctx.db.project.findMany({
    where,
    orderBy,
    cursor,
    take,
    skip,
  })

  return projects
}

Sometime I get ["db"] which looks like the actual request, but sometime I get ["fromQueryHook"] which, if I understand correctly, comes from the prefetching.

@flybayer
Copy link
Member

Hmm, that's odd! Can you share your code that has the useQuery hook?

useQuery should only ever run on the client, but here's it's running on the server apparently.

@Errorname
Copy link
Author

I used the generated code from blitz with the following steps:

blitz new context-issue
cd context-issue
blitz generate all project name:string
blitz db migrate

Then, add a console.log(ctx) in the queries/getProjects.ts file

Finally, run blitz start and go to http://localhost:3000/projects

You should now see { fromQueryHooks: true } in the logs

@Errorname
Copy link
Author

It looks like it's only at the start of blitz start, just after compiled successfully. Maybe the query is executed after the compilation ?

@flybayer flybayer self-assigned this Jul 21, 2020
@flybayer
Copy link
Member

Ok thanks, looking into it right now

@flybayer
Copy link
Member

Sorry, I got pulled to a higher priority bug, but I should be able to get back to this tomorrow

@flybayer
Copy link
Member

@Errorname I have a fix this in #671, but in the meantime you can safely ignore the error. The error is from that code running during pre-rendering when it shouldn't. Doesn't affect runtime code.

@Errorname
Copy link
Author

Errorname commented Jul 23, 2020

Thank you for fixing this issue quickly 👍
If you are interested on the multi-tenancy with Blitz, I've added a small documentation on how to do it with prisma-multi-tenant 🙂

@flybayer
Copy link
Member

@Errorname awesome, good work on that! :)

@dillondotzip dillondotzip transferred this issue from blitz-js/blitz Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants