nexus-plugin-prisma: Property 'model' does not exist on type 'ObjectDefinitionBlock..., Property 'crud' does not exist on type 'OutputDefinitionBlock...
I know this is not a new issue, but even after reading through related issues and studying the adoption guides, I cannot figure out how to successfully build a simple @nexus/schema project.
I have setup a project as described in the current tutorial docs with following relevant files:
// api/schema.ts
import { makeSchema } from '@nexus/schema'
import { nexusPrisma } from 'nexus-plugin-prisma'
import { join } from 'path'
import * as typeDefs from './graphql'
export const schema = makeSchema({
types: typeDefs,
plugins: [
nexusPrisma({
experimentalCRUD: true
}),
],
outputs: {
typegen: join(__dirname, '..', 'nexus-typegen.ts'),
schema: join(__dirname, '..', 'schema.graphql')
},
typegenAutoConfig: {
sources: [
{
source: require.resolve('.prisma/client/index.d.ts'),
alias: "prisma",
},
{
source: require.resolve("./context"),
alias: "ContextModule",
},
],
contextType: "ContextModule.Context",
},
})
// api/graphql/index.ts
export * from './User'
// api/graphql/User.ts
import { objectType, extendType } from '@nexus/schema'
export const User = objectType({
name: 'User',
definition(t) {
t.model.email()
t.model.name()
}
})
export const UserQuery = extendType({
type: 'Query',
definition(t) {
t.crud.users()
},
})
When running ts-node-dev api/schema.ts those errors are thrown:
Property 'model' does not exist on type 'ObjectDefinitionBlock
Property 'crud' does not exist on type 'OutputDefinitionBlock
Following dependencies are in use: “@nexus/schema”: “0.16.0”, “@prisma/client”: “2.7.0”, “@prisma/cli”: “2.7.0”, “nexus-plugin-prisma”: “0.20.0”, “ts-node-dev”: “^1.0.0-pre.63”
Can anyone please help?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 16
- Comments: 17
i had the same problem (
modelandcrudproperties do not exist on types) and @djm’s solution while wasn’t perfect for me pointed me to the right direction (thanks!).my versions are:
in my understanding there are 3 different type generations in play:
type generation
prisma types
yarn prisma generateprovides this and the file’s location isnode_modules/.prisma/client/index.ts(it took some time while i figured out that.prismais not a typo 😄). this is strictly prisma related and it’s useful when you manually call prisma in your resolver (e.g.prisma.yourModel.findMany()).nexus plugin prisma types
the
nexusPrisma()plugin imported from thenexus-plugin-prismapackage generates this. you can set the path by adding anoutputsoption to the plugin (e.g.nexusPrisma({ outputs: 'yourPath' })) but since the default path isnode_modules/@types/typegen-nexus-plugin-prisma/index.d.tstypescript already knows about this so it’s not necessary to set this value. the file contains the types for the graphqlized prisma schema, like interfaces for the models, inputs, etc.nexus types
the
makeSchema()function imported from thenexuspackage generates this. this is where the missing properties are. you can set the path by addingoutputsoroutputs/typegenoption to the config (e.g.makeSchema({ outputs: true })). there are a few different scenarios now:1.
{ outputs: true }i went with this originally because it says
truemeans default paths and the default path for generated types isnode_modules/@types/nexus-typegen/index.d.ts. unfortunately there is no generated file innode_modulesbecause the typegen file path is not set, it remainsnull.2.
{ outputs: { typegen: true } }same goes for this, while the interface allows using a
booleanhere, it gets completely ignored.3.
{ outputs: { typegen: 'yourPath' } }in my experience this is the only working setup to make it work. with this you can set the file name of the missing generated types.
solutions
now that we finally know how to generate those types we have different ways to make typescript discover them.
leverage the
node_modules/@typesdirectorysince typescript is aware of the content of this directory and this is what nexus would do by default according to the tsdoc tag i added this to my
makeSchemaconfig:i added the
-customprefix to prevent collision with the hopefully-soon-to-be-fixed default path.add the generated file to
tsconfig.jsoni guess some people uses the official guide to make this work so here is another way to solve it:
add
typeRootstotsconfig.jsonundercompilerOptions(your path to the generated directory can be different):edit: now that i checked all of this again it seems like generating the
nexus.d.tsoutside ofnode_modules/@typesworks without addingtypeRootsto mytsconfig.json. it did not work before and i spent some time with this so i could have added something to my config (can it be therootDir?). i’m somewhat new to typescript so please fix me if i said something stupid above.workaround At the first start, the types have not yet been defined, and then the
crudand themodelwill haveanytype. But after generating the types (“nexus:reflect”: “NEXUS_SHOULD_EXIT_AFTER_REFLECTION=true ts-node src”), they will get the desired type.@jawosis I think the doc is a bit unclear. My problem was that
ctx.dbis not passed to apollo server. I solve is by passing context:I tried the fix from this PR: https://github.com/graphql-nexus/nexus-plugin-prisma/pull/876
and everything seems to be working well now. seems to be a typo in a function in the transformer.
might be related to this error I see when running:
ts-node-dev --transpile-only src/app.ts