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

Local typing error when running nexus build #1352

Open
scleriot opened this issue Aug 13, 2020 · 6 comments
Open

Local typing error when running nexus build #1352

scleriot opened this issue Aug 13, 2020 · 6 comments
Labels
type/bug Something is not working the way it should

Comments

@scleriot
Copy link

scleriot commented Aug 13, 2020

Description

When building with nexus build I get typing error that I don't get when running nexus dev:

> nexus build

1499 ● nexus:build get used plugins
● nexus:plugin:nexusPluginPrisma Running generators
● nexus:build starting reflection
● nexus:build building typescript program
● nexus:build compiling a production build
src/app.ts:38:9 - error TS2339: Property 'prisma' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.

38     req.prisma = new PrismaClient()
etc...

    at Object.emitTSProgram (/home/scleriot/Dev/perso/customer-success/backend/node_modules/nexus/src/lib/tsc.ts:108:11)
    at Object.buildNexusApp (/home/scleriot/Dev/perso/customer-success/backend/node_modules/nexus/src/lib/build/build.ts:97:3)
    at process._tickCallback (internal/process/next_tick.js:68:7)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `nexus build`
npm ERR! Exit status 1

Local typing is defined in:

  • types/
    • express/
      • index.d.ts
    • shims-nexuscontext.d.ts

express/index.d.ts looks like this:

declare namespace Express {
  interface Request {
      userId: string
      prisma: import('@prisma/client').PrismaClient
  }
}

Here is the corresponding tsconfig.json:

{
    "compilerOptions": {
      "target": "esnext",
      "module": "commonjs",
      "strict": true,
      "jsx": "preserve",
      "moduleResolution": "node",
      "experimentalDecorators": true,
      "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "noImplicitAny": false,
      "sourceMap": true,
      "strictNullChecks": false,
      "noEmit": true,
      "lib": [
        "esnext"
      ],
      "typeRoots" : ["node_modules/@types", "types"],
      "plugins": [{ "name": "nexus/typescript-language-service" }],
      "rootDir": "."
    },
    "include": [".", "types.d.ts"]
}

Nexus Report

I get the following error when doing nexus report:

TypeError: Object.fromEntries is not a function
    at Object.getNexusReport (/home/scleriot/Dev/perso/customer-success/backend/node_modules/nexus/dist/lib/report.js:27:30)
    at Report.parse (/home/scleriot/Dev/perso/customer-success/backend/node_modules/nexus/dist/cli/commands/report.js:23:39)
@scleriot scleriot added the type/bug Something is not working the way it should label Aug 13, 2020
@mohe2015
Copy link

mohe2015 commented Aug 13, 2020

Can you show the part of app.ts please? (You can remove the parts you don't want to show)

@scleriot
Copy link
Author

Sure:

import { server, settings, use } from 'nexus'
import { prisma } from 'nexus-plugin-prisma'
import { PrismaClient } from '@prisma/client'

// ...

server.express.use((req, res, next) => {
    req.prisma = new PrismaClient()
    next()
})

Right now my workaround for completing the build is commenting line 78 of node_modules/nexus/dist/lib/tsc.js:

if (allDiagnostics.length > 0) {
        console.log(project.formatDiagnosticsWithColorAndContext(allDiagnostics));
        // throw new Error(project.formatDiagnosticsWithColorAndContext(allDiagnostics));
    }

@mohe2015
Copy link

According to the tutorial you should either just do

use(prisma())

to make prisma available in the context (as db I think) or you can use:

const db = new PrismaClient();

use(
  prisma({
    client: {
      instance: db,
    },
    features: {
      crud: true, // only if you want that
    },
  })
);

if you need to do customizations.

@scleriot
Copy link
Author

@mohe2015 Thanks, but this issue is not about Prisma usage, but a bug report regarding TypeScript typechecking done by nexus build

@mohe2015
Copy link

mohe2015 commented Aug 19, 2020

@scleriot I know but the error Property 'prisma' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.looks like you are not supposed to manipulate the request object. Instead you are supposed to manipulate the context object e.g. using my method or using schema.addToContext
Edit:

schema.addToContext(async ({req, res}) => {
  return {
    db: new PrismaClient(), // If you really want to create a new one for every request
    userId: getItSomehow(req)
  };
});

@scleriot
Copy link
Author

Actually I'm trying no extend not only NexusContext type but also Express

I should be able to extend any typings, in types/ directory for example as explained here: https://nexusjs.org/guides/project-layout#local-package-typings.

Works well with nexus dev, error happens only at build time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is not working the way it should
Projects
None yet
Development

No branches or pull requests

2 participants