paljs / prisma-tools

Prisma tools to help you generate CRUD system for GraphQL servers
https://paljs.com
MIT License
681 stars 55 forks source link

"Cannot read property 'dmmf' of undefined" #224

Closed BaptisteCayrier closed 3 years ago

BaptisteCayrier commented 3 years ago

Hi, I tried to use you tool PrismaSelect (which looks so usefull!) but I faced a little issue:

Here is my code:

import { PrismaSelect } from '@paljs/plugins';

[...]

const select = new PrismaSelect(info, { dmmf: undefined }).value;
return prisma.user.findMany({
     ...select.value,
});
```javascript
which lead to the error "Cannot read property 'dmmf' of undefined".

After some investigation I found the origine of the problem.
In the file `node_modules/@paljs/plugins/dist/plugins.cjs.development.js` on the declaration line 365:
```javascript
{
    key: "dataModel",
    get: function get() {
      [...]

      if (...) {
        [...]
      } else {
        var _require = require('@prisma/client'),         // <- this line
            Prisma = _require.Prisma;                            // <- and this line

        if (Prisma.dmmf && Prisma.dmmf.datamodel) {
          models.push.apply(models, Prisma.dmmf.datamodel.models);
        }
      }

      return models;
    }
  }

I printed _require and _require.Prisma is undefined:

_require =  {
  prismaVersion: {
    client: '2.5.1',
    engine: 'c88925ce44a9b89b4351aec85ba6a28979d2658e'
  },
  PrismaClientKnownRequestError: [class PrismaClientKnownRequestError extends Error],
  PrismaClientUnknownRequestError: [class PrismaClientUnknownRequestError extends Error],
  PrismaClientRustPanicError: [class PrismaClientRustPanicError extends Error],
  PrismaClientInitializationError: [class PrismaClientInitializationError extends Error],
  PrismaClientValidationError: [class PrismaClientValidationError extends Error],
  sql: [Function: sqltag],
  empty: { text: '', sql: '', values: [] },
  join: [Function: join],
  raw: [Function: raw],
  SortOrder: { asc: 'asc', desc: 'desc' },
  RoomDistinctFieldEnum: {
    id: 'id',
    createdAt: 'createdAt',
    isPrivate: 'isPrivate',
    name: 'name',
    password: 'password',
    image: 'image',
    roomSize: 'roomSize',
    roomSizeMax: 'roomSizeMax',
    leader: 'leader'
  },
  UserDistinctFieldEnum: {
    id: 'id',
    createdAt: 'createdAt',
    name: 'name',
    email: 'email',
    password: 'password',
    imageId: 'imageId',
    roomId: 'roomId',
    permission: 'permission',
    userId: 'userId',
    verified: 'verified'
  },
  OrganizationDistinctFieldEnum: {
    id: 'id',
    createdAt: 'createdAt',
    name: 'name',
    password: 'password',
    image: 'image',
    leader: 'leader'
  },
  WidgetDistinctFieldEnum: {
    id: 'id',
    createdAt: 'createdAt',
    name: 'name',
    image: 'image',
    organizationId: 'organizationId',
    url: 'url',
    masterWidget: 'masterWidget'
  },
  WidgetSavesDistinctFieldEnum: {
    id: 'id',
    createdAt: 'createdAt',
    label: 'label',
    data: 'data',
    boardSavesId: 'boardSavesId',
    associatedWidgetId: 'associatedWidgetId',
    deprecated_associatedWidgetId: 'deprecated_associatedWidgetId'
  },
  BundlesDistinctFieldEnum: { id: 'id', name: 'name' },
  BoardSavesDistinctFieldEnum: { id: 'id', userId: 'userId', label: 'label' },
  MediasDistinctFieldEnum: {
    id: 'id',
    createdAt: 'createdAt',
    name: 'name',
    type: 'type',
    url: 'url'
  },
  dmmf: {
    datamodel: { enums: [], models: [Array] },
    mappings: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ],
    schema: { enums: [Array], outputTypes: [Array], inputTypes: [Array] }
  },
  PrismaClient: [class NewPrismaClient]
}

One possible fix could in this same file could be:

[...]
else {
        var Prisma = require('@prisma/client');        // <- this line

        if (Prisma.dmmf && Prisma.dmmf.datamodel) {
          models.push.apply(models, Prisma.dmmf.datamodel.models);
        }
} 
[...]

However, I think this is generated code and the correct fix is probably to be done in https://github.com/paljs/prisma-tools/blob/main/packages/plugins/src/select.ts#L78

From

      const { Prisma } = require('@prisma/client');

To

      const Prisma = require('@prisma/client');

Obviously this fix might be specific to my stack, for more information about it, here is my package.json:

{
  "name": "api",
  "private": true,
  "version": "1.0.0",
  "license": "MIT",
  "repository": "",
  "author": "",
  "description": "",
  "main": "main.js",
  "engines": {
    "node": "~14"
  },
  "scripts": {
    "start": "npx ts-node src/main.ts",
    "lint": "eslint ./src --ext .ts",
    "migrate-prisma": "./scripts/migratePrisma.sh",
    "lint-and-fix": "eslint . --ext .ts --fix",
    "prisma-studio": "prisma studio --experimental",
    "nodemon": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' src/main.ts",
    "tests": "env TS_NODE_PROJECT=\"tsconfig.json\" mocha --require ts-node/register '__tests__/**/*.test.ts' --exit",
    "generate-schema": "ts-node scripts/generateSchema.ts > schema.graphql"
  },
  "dependencies": {
    "@paljs/plugins": "^3.4.0",
    "@prisma/cli": "2.5.1",
    "@prisma/client": "2.5.1",
    "@types/bcryptjs": "^2.4.2",
    "@types/jsonwebtoken": "7.2.3",
    "@types/mocha": "^8.2.1",
    "apollo-server": "2.5.0",
    "apollo-server-testing": "^2.21.0",
    "aws-sdk": "^2.871.0",
    "axios": "^0.21.1",
    "bcryptjs": "^2.4.3",
    "chai": "^4.2.0",
    "easygraphql-tester": "^5.1.6",
    "graphql": "14.3.0",
    "graphql-resolvers": "^0.4.2",
    "jsonwebtoken": "^8.5.1",
    "mocha": "^8.2.1",
    "moment": "^2.29.1",
    "nodemailer": "^6.5.0",
    "nodemon": "^2.0.6",
    "ts-node": "^9.1.0",
    "tsconfig-paths": "^3.9.0"
  },
  "devDependencies": {
    "@graphql-codegen/cli": "1.17.8",
    "@graphql-codegen/typescript": "1.17.8",
    "@graphql-codegen/typescript-resolvers": "1.17.8",
    "@types/node": "12.0.1",
    "@types/webpack-env": "1.13.9",
    "@typescript-eslint/eslint-plugin": "^3.10.1",
    "@typescript-eslint/parser": "^3.10.1",
    "clean-webpack-plugin": "2.0.2",
    "dotenv": "8.0.0",
    "eslint": "^7.7.0",
    "npm-run-all": "4.1.5",
    "start-server-webpack-plugin": "^2.2.5",
    "ts-loader": "6.0.0",
    "typescript": "3.4.5",
    "webpack": "^4.44.1",
    "webpack-cli": "3.3.2",
    "webpack-merge": "4.2.1",
    "webpack-node-externals": "1.7.2"
  }
}

I hope everything is well explained and thanks in advance for you to look at it :)

AhmedElywa commented 3 years ago

you need to upgrade prisma to the last version or down our plugin to version 2

BaptisteCayrier commented 3 years ago

Hi, thanks for your answer! Everything working as expected now! :)

I didn't saw this information anywhere on any Readme, maybe you could add it to make sure nobody face this issue again. :)

AhmedElywa commented 3 years ago

you will get this info on the release notes