Skip to content

Instantly share code, notes, and snippets.

@djm
Created January 4, 2021 23:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djm/a7e29eea33b636d0d839be9e56118b15 to your computer and use it in GitHub Desktop.
Save djm/a7e29eea33b636d0d839be9e56118b15 to your computer and use it in GitHub Desktop.
nexus-plugin-prisma fix: property 'model'/'crud' does not exist on type ObjectDefinitionBlock

Just a quick fix dump for anyone not using ts-node-dev who is getting the Property 'model' does not exist on type ObjectDefinitionBlock or Property 'crud' does not exist on type ObjectDefinitionBlock: ensure your type generations are getting output where you think they are.

ts-node-dev does not transpile to a dist folder whereas other typescript watchers (like tsc-watch do). This is critical because if you output the generated types into your dist folder, VSCode and TypeScript won't find them, and you'll get the above error.

Also be aware that nexus & nexus-plugin-prisma have their own type generations and their own config definition for where to output each.

This is a commented example of makeSchema, with versions, so you can work out what's what:

nexus: v1.0.0 nexus-plugin-prisma: v0.27.0 @prisma/client: v2.13.1

import * as path from 'path';

import { nexusPrisma } from 'nexus-plugin-prisma';
import { makeSchema } from 'nexus';

import * as types from './types';
import * as queries from './queries';
import * as mutations from './mutations';

export const schema = makeSchema({
  // It is unnecessary to split the types
  // like this but I prefer it.
  types: [queries, mutations, types],
  plugins: [
    nexusPrisma({
      // Enable or disable auto-generation of the `crud` abilities.
      experimentalCRUD: false,
      // These are the generation paths for `nexus-plugin-prisma`,
      // it currently generates one typegen file that is unique
      // to your prisma schema. NB: This gets read at RUNTIME, so
      // if your outputting js to a ./dist folder you'll want to
      // take that into account when making the path so that you
      // can instead output into something like `./src/generated`
      // or any other folder that you have your tsconfig.json set
      // up to look into it (rarely is this dist!)
      outputs: {
        typegen: path.join(
          __dirname,
          '..',
          'generated',
          'nexus-plugin-prisma-typegen.d.ts',
        ),
      },
    }),
  ],
  // By default this is true when your NODE_ENV is development
  // but you can control this however you like. It controls all
  // file generation, including the 2 typegen files & the one
  // schema file.
  shouldGenerateArtifacts: true,
  // Tell Nexus where to find the type used for the context.
  // Your GraphQL server probably accepts some form of context
  // instance, this is where you can point Nexus to the correct
  // type for it.
  contextType: {
    module: require.resolve('./context'),
    export: 'NexusContext',
  },
  // Tell Nexus where to find our Prisma client types.
  // This will depend on your setup, but by default
  // Prisma v2 outputs to your node_modules directory.
  // This is where Nexus looks for the client types.
  sourceTypes: {
    modules: [
      {
        module: require.resolve('../../node_modules/.prisma/client/index.d.ts'),
        alias: 'PrismaClient',
      },
    ],
  },
  // Auto-generate the GraphQL schema (in SDL format) &
  // TypeScript types for our Nexus-defined GraphQL types.
  //
   // These are the generation paths for `nexus` itself. The
   // schema is for the generated GraphQL schema - this is helpful
   // to commit so you can see overtime how changes to the Nexus code
   // affect your public GraphQL schema. The types are what you need
   // to make VSCode + TypeScript happy.
   //
   // NB: Same rule as above. This gets read at RUNTIME, so if you're
   // outputting js to a ./dist folder you'll want to take that into
   // account when making the path so that you can instead output into
   // something like `./src/generated` or any other folder that you have
   // your tsconfig.json set up to look into it (rarely is this dist!)
  outputs: {
    schema: path.join(__dirname, '..', 'generated', 'schema.graphql'),
    typegen: path.join(__dirname, '..', 'generated', 'nexus-typegen.d.ts'),
  },
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment