graphql-nexus / nexus

Code-First, Type-Safe, GraphQL Schema Construction
https://nexusjs.org
MIT License
3.4k stars 274 forks source link

New issue with makeSchema (getTypeMap of undefined) #523

Closed ZaneH closed 3 years ago

ZaneH commented 4 years ago

I honestly have no clue where this issue began. Yesterday, at some point, I started getting this error on yarn generate:

yarn run v1.22.5
warning package.json: No license field
$ npm -s run generate:prisma && npm -s run generate:nexus
Environment variables loaded from prisma/.env
Prisma Schema loaded from prisma/schema.prisma

✔ Generated Prisma Client (version: 2.7.0) to ./../../node_modules/@prisma/client in 216ms

You can now start using Prisma Client in your code:

import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')

const prisma = new PrismaClient()

Explore the full API: http://pris.ly/d/client
TypeError: Cannot read property 'getTypeMap' of undefined
    at generateMiddlewareFromSchemaAndRuleTree (/home/zaneh/Desktop/redacted/node_modules/graphql-shield/src/generator.ts:241:28)
    at MiddlewareGenerator.generator (/home/zaneh/Desktop/redacted/node_modules/graphql-shield/src/generator.ts:293:5)
    at MiddlewareGenerator.generate (/home/zaneh/Desktop/redacted/node_modules/graphql-middleware/src/generator.ts:19:17)
    at middlewares.map.middleware (/home/zaneh/Desktop/redacted/node_modules/graphql-middleware/src/middleware.ts:72:25)
    at Array.map (<anonymous>)
    at applyMiddlewareWithOptions (/home/zaneh/Desktop/redacted/node_modules/graphql-middleware/src/middleware.ts:70:45)
    at applyMiddleware (/home/zaneh/Desktop/redacted/node_modules/graphql-middleware/src/middleware.ts:129:10)
    at new GraphQLServer (/home/zaneh/Desktop/redacted/node_modules/graphql-yoga/src/index.ts:137:13)
    at Object.<anonymous> (/home/zaneh/Desktop/redacted/packages/backend/src/server.ts:28:16)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I can't think of anything that would've caused this, but I tried rolling back to a previous (and known-to-work) commit, deleting my node_modules, and reinstalling, but I never got it back to a working state 🥳. If anyone has any info/debugging tips for this, I'm all ears

My package.json:

{
  "name": "@redacted/backend",
  "version": "1.0.0",
  "scripts": {
    "generate": "npm -s run generate:prisma && npm -s run generate:nexus",
    "generate:prisma": "prisma generate",
    "generate:nexus": "ts-node --transpile-only src/schema",
    "dev": "ts-node-dev --no-notify --respawn --transpileOnly src/server",
    "codegen": "graphql-codegen --config codegen.yml",
    "seed": "ts-node prisma/seed.ts"
  },
  "dependencies": {
    "@nexus/schema": "0.16.0",
    "@paypal/checkout-server-sdk": "^1.0.2",
    "@prisma/client": "2.7.0",
    "@sendgrid/mail": "^7.2.6",
    "aws-sdk": "^2.743.0",
    "bcryptjs": "^2.4.3",
    "cors": "^2.8.5",
    "cuid": "^2.1.8",
    "dotenv-flow": "^3.2.0",
    "graphql": "15.3.0",
    "graphql-iso-date": "^3.6.1",
    "graphql-middleware": "4.0.1",
    "graphql-shield": "7.3.6",
    "graphql-yoga": "1.18.3",
    "lodash": "^4.17.20",
    "multer": "^1.4.2",
    "nexus-plugin-prisma": "0.19.0",
    "sanitize-html": "^2.0.0"
  },
  "devDependencies": {
    "@graphql-codegen/cli": "^1.17.8",
    "@graphql-codegen/typescript": "^1.17.9",
    "@graphql-codegen/typescript-operations": "^1.17.8",
    "@graphql-codegen/typescript-react-apollo": "^2.0.6",
    "@prisma/cli": "2.7.0",
    "@types/aws-sdk": "^2.7.0",
    "@types/bcryptjs": "^2.4.2",
    "@types/cors": "^2.8.7",
    "@types/cuid": "^1.3.1",
    "@types/dotenv-flow": "^3.1.0",
    "@types/graphql-iso-date": "^3.4.0",
    "@types/multer": "^1.4.4",
    "@types/node": "12.12.54",
    "@types/sanitize-html": "^1.27.0",
    "@types/ws": "^7.2.7",
    "ts-node": "9.0.0",
    "ts-node-dev": "1.0.0-pre.50",
    "typescript": "4.0.3"
  }
}

I've tried:

ZaneH commented 4 years ago
Old news I'm still investigating this, I added some code to debug. Here is `server.ts`: ```ts import { schema } from './schema' ... const server = new GraphQLServer({ schema: schema(), context: createContext, middlewares: [permissions], }) ... ``` and then in `./schema.ts`: ```ts import { makeSchema } from '@nexus/schema' import { nexusSchemaPrisma } from 'nexus-plugin-prisma/dist/schema' import * as types from './types' export const schema = () => { console.log('Making schema?') const _schema = makeSchema({ types, plugins: [ nexusSchemaPrisma({ experimentalCRUD: true, }), ], outputs: { schema: __dirname + '/generated/schema.graphql', typegen: __dirname + '/generated/nexus.ts', }, typegenAutoConfig: { contextType: 'Context.Context', sources: [ { source: '@prisma/client', alias: 'client', }, { source: require.resolve('./context'), alias: 'Context', }, ], }, }) console.log('Schema: ', _schema) return _schema } ``` When I `yarn generate` now: ``` TypeError: schema_1.schema is not a function at Object. (/home/zaneh/Desktop/redacted/packages/backend/src/server.ts:40:11) at Module._compile (internal/modules/cjs/loader.js:778:30) at Module.m._compile (/home/zaneh/Desktop/redacted/node_modules/ts-node/src/index.ts:1043:23) at Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Object.require.extensions.(anonymous function) [as .ts] (/home/zaneh/Desktop/redacted/node_modules/ts-node/src/index.ts:1046:12) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Module.require (internal/modules/cjs/loader.js:692:17) at require (internal/modules/cjs/helpers.js:25:18) error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. ``` Does it seem like the compiler is missing my schema.ts file? I'm not sure how to confirm this

I added makeSchema directly to my server.ts (check "Old news" for my motivation) and ran yarn generate:

yarn run v1.22.5
warning package.json: No license field
$ npm -s run generate:prisma && npm -s run generate:nexus
Environment variables loaded from prisma/.env
Prisma Schema loaded from prisma/schema.prisma

✔ Generated Prisma Client (version: 2.7.0) to ./../../node_modules/@prisma/client in 718ms

You can now start using Prisma Client in your code:

import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')

const prisma = new PrismaClient()

Explore the full API: http://pris.ly/d/client
Error: It seems like you have applied rules to Mutation types but Shield cannot find them in your schema.
    at generateMiddlewareFromSchemaAndRuleTree (/home/zaneh/Desktop/redacted/node_modules/graphql-shield/src/generator.ts:250:13)
    at MiddlewareGenerator.generator (/home/zaneh/Desktop/redacted/node_modules/graphql-shield/src/generator.ts:293:5)
    at MiddlewareGenerator.generate (/home/zaneh/Desktop/redacted/node_modules/graphql-middleware/src/generator.ts:19:17)
    at middlewares.map.middleware (/home/zaneh/Desktop/redacted/node_modules/graphql-middleware/src/middleware.ts:72:25)
    at Array.map (<anonymous>)
    at applyMiddlewareWithOptions (/home/zaneh/Desktop/redacted/node_modules/graphql-middleware/src/middleware.ts:70:45)
    at applyMiddleware (/home/zaneh/Desktop/redacted/node_modules/graphql-middleware/src/middleware.ts:129:10)
    at new GraphQLServer (/home/zaneh/Desktop/redacted/node_modules/graphql-yoga/src/index.ts:137:13)
    at Object.<anonymous> (/home/zaneh/Desktop/redacted/packages/backend/src/server.ts:42:16)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

and the generated schema.graphql:

### This file was generated by Nexus Schema
### Do not make changes to this file directly

type Query {
  ok: Boolean!
}

What in the world is happening?

ZaneH commented 4 years ago
Old news I'm making progress, but nothing is making sense yet: ``` yarn run v1.22.5 warning package.json: No license field $ npm -s run generate:prisma && npm -s run generate:nexus Environment variables loaded from prisma/.env Prisma Schema loaded from prisma/schema.prisma ✔ Generated Prisma Client (version: 2.7.0) to ./../../node_modules/@prisma/client in 214ms You can now start using Prisma Client in your code: import { PrismaClient } from '@prisma/client' // or const { PrismaClient } = require('@prisma/client') const prisma = new PrismaClient() Explore the full API: http://pris.ly/d/client # My console.log (logs all of my included types, all of the ones after "index.ts" in the file directory are undefined...): NexusObjectTypeDef { name: 'AuthPayload', config: { name: 'AuthPayload', definition: [Function: definition] } } NexusObjectTypeDef { name: 'File', config: { name: 'File', definition: [Function: definition] } } NexusObjectTypeDef { name: 'Image', config: { name: 'Image', definition: [Function: definition] } } undefined undefined undefined undefined undefined undefined undefined undefined undefined Warning: Your GraphQL `File` object definition is projecting a field `product` with `Product` as output type, but `Product` is not defined in your GraphQL Schema Warning: Your GraphQL `Image` object definition is projecting a field `product` with `Product` as output type, but `Product` is not defined in your GraphQL Schema Error: - Missing type User, did you forget to import a type to the root query? at Object.assertNoMissingTypes (/home/zaneh/Desktop/redacted/node_modules/@nexus/schema/src/utils.ts:323:11) at Object.makeSchema (/home/zaneh/Desktop/redacted/node_modules/@nexus/schema/src/builder.ts:1524:3) at Object. (/home/zaneh/Desktop/redacted/packages/backend/src/server.ts:71:11) at Module._compile (internal/modules/cjs/loader.js:778:30) at Module.m._compile (/home/zaneh/Desktop/redacted/node_modules/ts-node/src/index.ts:1043:23) at Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Object.require.extensions.(anonymous function) [as .ts] (/home/zaneh/Desktop/redacted/node_modules/ts-node/src/index.ts:1046:12) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. ```

So, I removed ./types/index.ts and things started working a little bit better. Now I'm only missing 1 type. It's the Mutation type. However, I think the issue is rooted deeper than "The Mutation file" because in all of my types, the relational t.model.<other_model>() fields are error'ing.

In addition, I know the code in my type files is correct. It hasn't been changed since my last working commit.

I'm going to pause here. If anyone can help, I'd appreciate it

ZaneH commented 4 years ago

I had this issue earlier: https://github.com/graphql-nexus/nexus-plugin-prisma/issues/872

It transformed into this one

ZaneH commented 3 years ago
Old news Found the fix! In my Query.ts file, I have references to objects in my generated client. The issue being: Prisma can't generate the schema, because those objects don't exist. But they did exist in the past. That's what was tripping me up. I simply commented out the import, ran `yarn generate` then `yarn codegen` (to generate my imported references) and everything is back to a working condition 👍 --- In addition, the newest version of ts-node-dev causes an issue, I had to use `ts-node-dev@1.0.0-pre.44`.
ZaneH commented 3 years ago

I thought I had fixed this: I didn't

I believed this to be a case of circular imports (it couldn't generate @prisma/client because a generated type couldn't be imported),

But days after that "fix" I still stumble on this error. Reopened and praying

ZaneH commented 3 years ago

I think...I maybe found the fix? I had 2 PrismaClient instances. I exported the 1 in my context.ts file, and removed the one in server.ts. Seems to have fixed it.

zakthedev commented 3 years ago

How did you fix this at the end? I got the same issue out of a sudden and couldn't get things to work again, I tried different prisma clients from 2.9 up to lastest, please share your package.json if you can

hyochan commented 3 years ago

How did you fix this at the end? I got the same issue out of a sudden and couldn't get things to work again, I tried different prisma clients from 2.9 up to lastest, please share your package.json if you can

Try not to import prisma from the context.ts. Pass it through the args. I had this problem earlier.