MichalLytek / typegraphql-prisma

Prisma generator to emit TypeGraphQL types and CRUD resolvers from your Prisma schema
https://prisma.typegraphql.com
MIT License
885 stars 114 forks source link

Error: Unable to find type metadata for input type or object type named #240

Closed alacret closed 2 years ago

alacret commented 2 years ago

Describe the Bug when creating relationship types, type-graphql error out because of missing type metadata

To Reproduce schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator typegraphql {
  provider = "typegraphql-prisma"
  output   = "../prisma/generated/type-graphql"
}

model User {
  id          Int  @id @default(autoincrement())
  profiles    Profile[]
}

model Profile {
  id             Int  @id @default(autoincrement())
  user           User @relation(fields: [userId], references: [id])
  userId         Int
}
{
  "name": "medone-backend",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.ts",
  "engines": {
    "node": ">=16.0.0"
  },
  "scripts": {
    "test": "jest",
    "eslint": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
    "prepare": "husky install",
    "prettier": "prettier --write '**/*.{ts,tsx,js,jsx}'",
    "type-check": "tsc --noEmit --project tsconfig.json",
    "start": "nodemon ./src/index.ts",
    "build": "rm -rf dist && tsc",
    "prisma-pull": "prisma db pull",
    "prisma-migrate": "prisma migrate dev",
    "prisma-generate": "prisma generate"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/medonecorp/backend.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/medonecorp/backend/issues"
  },
  "homepage": "https://github.com/medonecorp/backend#readme",
  "devDependencies": {
    "@babel/preset-env": "^7.16.8",
    "@babel/preset-typescript": "^7.16.7",
    "@types/jest": "^27.4.0",
    "@typescript-eslint/eslint-plugin": "^5.9.1",
    "@typescript-eslint/parser": "^5.9.1",
    "eslint": "^8.6.0",
    "eslint-config-airbnb-typescript": "^16.1.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-config-standard-with-typescript": "^21.0.1",
    "eslint-plugin-import": "^2.25.4",
    "eslint-plugin-jest": "^25.3.4",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^6.0.0",
    "graphql-scalars": "^1.14.1",
    "husky": "^7.0.4",
    "jest": "^27.4.7",
    "jira-prepare-commit-msg": "^1.6.2",
    "lint-staged": "^12.1.7",
    "nodemon": "^2.0.15",
    "prettier": "2.5.1",
    "prisma": "^3.8.1",
    "ts-node": "^10.4.0",
    "tslib": "^2.3.1",
    "typegraphql-prisma": "^0.18.3",
    "typescript": "^4.5.4",
    "@types/graphql": "14.5.0",
    "@types/micro-cors": "0.1.2",
    "@types/node": "16.11.20"
  },
  "lint-staged": {
    "**/*": [
      "npm run prettier ",
      "npm run eslint "
    ]
  },
  "dependencies": {
    "@prisma/client": "^3.8.1",
    "@types/express": "^4.17.13",
    "apollo-server-core": "^3.6.1",
    "apollo-server-express": "^3.6.1",
    "aws-sdk": "^2.1058.0",
    "class-validator": "^0.13.2",
    "express": "^4.17.2",
    "express-oauth2-jwt-bearer": "^1.1.0",
    "graphql": "15.8.0",
    "graphql-scalars": "1.14.1",
    "graphql-tag": "2.12.6",
    "graphql-fields": "^2.0.3",
    "jsonwebtoken": "^8.5.1",
    "pg": "^8.7.1",
    "reflect-metadata": "^0.1.13",
    "type-graphql": "^1.1.1",
    "winston": "^3.4.0",
    "winston-cloudwatch": "^3.1.1"
  }
}

Expected Behavior Able to related fields and models

Logs

[nodemon] starting `ts-node ./src/index.ts`
Error: Unable to find type metadata for input type or object type named ''
    at /Users/alacret/workspace/medone-backend/node_modules/type-graphql/dist/metadata/metadata-storage.js:184:27
    at Array.forEach (<anonymous>)
    at MetadataStorage.buildFieldResolverMetadata (/Users/alacret/workspace/medone-backend/node_modules/type-graphql/dist/metadata/metadata-storage.js:168:21)
    at MetadataStorage.build (/Users/alacret/workspace/medone-backend/node_modules/type-graphql/dist/metadata/metadata-storage.js:101:14)
    at Function.generateFromMetadataSync (/Users/alacret/workspace/medone-backend/node_modules/type-graphql/dist/schema/schema-generator.js:28:51)
    at Function.generateFromMetadata (/Users/alacret/workspace/medone-backend/node_modules/type-graphql/dist/schema/schema-generator.js:16:29)
    at buildSchema (/Users/alacret/workspace/medone-backend/node_modules/type-graphql/dist/utils/buildSchema.js:10:61)
    at /Users/alacret/workspace/medone-backend/src/index.ts:23:35
    at step (/Users/alacret/workspace/medone-backend/src/index.ts:33:23)
    at Object.next (/Users/alacret/workspace/medone-backend/src/index.ts:14:53)
[nodemon] clean exit - waiting for changes before restart

Environment (please complete the following information):

Additional Context Without relations it works perfectly

murunwas commented 2 years ago

I'm also getting the same error.

MichalLytek commented 2 years ago

Please read the installation docs more carefully and properly set target in your tsconfig.json.

alacret commented 2 years ago

I fixed this by using the tsconfig.json provided in one of the examples:

1) This was my original tsconfig.json file that was causing this issue:

{
  "compilerOptions": {
    "lib": ["es5", "es6", "esnext", "esnext.asynciterable"],
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "./dist",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "esModuleInterop": true,
    "strictNullChecks": true
  },
  "include": ["src"]
}

2) This was the one that fixed this problem:

{
  "compilerOptions": {
    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "es2019" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
    "lib": ["es2019"] /* Specify library files to be included in the compilation. */,
    // "allowJs": true,                       /* Allow javascript files to be compiled. */
    // "checkJs": true,                       /* Report errors in .js files. */
    // "jsx": "preserve",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    "declaration": true /* Generates corresponding '.d.ts' file. */,
    // "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
    "sourceMap": true /* Generates corresponding '.map' file. */,
    // "outFile": "./",                       /* Concatenate and emit output to single file. */
    "outDir": "./lib" /* Redirect output structure to the directory. */,
    "rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
    // "composite": true,                     /* Enable project compilation */
    // "tsBuildInfoFile": "./",               /* Specify file to store incremental compilation information */
    // "removeComments": true,                /* Do not emit comments to output. */
    // "noEmit": true,                        /* Do not emit outputs. */
    "importHelpers": true /* Import emit helpers from 'tslib'. */,
    // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
    // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

    /* Strict Type-Checking Options */
    "strict": true /* Enable all strict type-checking options. */,
    // "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    // "strictNullChecks": true,              /* Enable strict null checks. */
    // "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    // "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */

    /* Additional Checks */
    // "noUnusedLocals": true,                /* Report errors on unused locals. */
    // "noUnusedParameters": true,            /* Report errors on unused parameters. */
    // "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
    // "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */

    /* Module Resolution Options */
    "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
    // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
    // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    // "typeRoots": [],                       /* List of folders to include type definitions from. */
    // "types": [],                           /* Type declaration files to be included in compilation. */
    // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
    // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */
    // "allowUmdGlobalAccess": true,          /* Allow accessing UMD globals from modules. */

    /* Source Map Options */
    // "sourceRoot": "",                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "",                         /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
    // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

    /* Experimental Options */
    "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
    "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */
  },
  "include": ["./src/**/*.ts"]
}