Nexus-plugin-prisma: Forward plugin fields in t.crud and t.model

Created on 27 Jan 2020  路  11Comments  路  Source: graphql-nexus/nexus-plugin-prisma

Hey,

It would be nice to be able to access the "dynamic fields" created by the other plugins in t.model and t.crud.

Example with the Field Authorize Plugin:

makeSchema({
  // ...
  plugins: [fieldAuthorizePlugin(), nexusPrismaPlugin()],
  // ...
});

//...

t.model.email({
  authorize: (parent, args, ctx) => parent.id === ctx.userId,
});

t.crud.createOnePost({
  authorize: (parent, args, ctx) => isUserConntext(ctx),
});

I would be happy to contribute if you need me to. This is an important feature to me because I would like to get rid of Graphql Shield ASAP.

PS: I know there is an opened issue about the Field Authorize Plugin but I wanted to extend it to play nice with any other plugin.

efforhard impachigh needinvestigation scoparchitecture typfeat

Most helpful comment

Hey everyone, we've just made it possible to use external field plugins into Nexus Prisma thanks to #687. It'll be available in the next release of nexus-prisma.

All 11 comments

Hey @gabrielcolson thanks for this idea. Looks good to us too. Bit of an experiment to see if this could lead to unintuitive fields on model/crud.

We鈥檙e not sure how hard this issue will be to implement but we would like to see it land this quarter. If by the end of February we think this might not make it from our side your contribution would be more than welcome.

@jasonkuhrt was this issue removed from the This Quarter board in favor of something else, or is it just not a priority?

I'm not sure we really need to have the plugin attributes added to the generated type, though that would be nice. Personally i would settle for a the ability to pass an array of middleware that would wrap the generated resolver. Not having any ability to intercept the request is a massive pain point.

t.model.email({
  middleware: [(parent, args, ctx, info, next) => { /* continue or throw error */ }]
});

t.crud.createOnePost({
  middleware: [(parent, args, ctx, info, next) => { /* continue or throw error */ }]
});

This is a big bummer at the moment. You basically have to choose between using nexus prisma or any other plugin (authorize, query complexity, custom ones, etc). A lot of the value in code-first schema construction are these plugins that allow abstracting common schema structure and functionality. The fact that the prisma plugin cuts you off from using the rest of them is really limiting.

Hey everyone, we've just made it possible to use external field plugins into Nexus Prisma thanks to #687. It'll be available in the next release of nexus-prisma.

@Weakky this is great, thank you! Do you have an ETA on the release and what version it will be? This is directly impacting a project we have progress at the moment.

@jcloutz-radix canary today @next, stable in just under two weeks.

We've just released [email protected].
You can try the feature by installing nexus-prisma@next right now. Feel free to report any bug related to it in here. I'll re-open the issue if needed 馃檹

@Weakky thank you!

I am trying to give it a test run using the blog example and it won't generate the nexus schema types.

When i downgrade to [email protected] it generates without error.

/mnt/c/Users/jeremycloutier/develop/labs/nexus-schema-plugin-prisma/examples/blog/src/schema/Query.ts:5
    t.crud.blogs({
           ^
TypeError: Cannot read property 'blogs' of undefined
    at Object.definition (/mnt/c/Users/jeremycloutier/develop/labs/nexus-schema-plugin-prisma/examples/blog/src/schema/Query.ts:5:12)
    at SchemaBuilder.walkOutputType (/mnt/c/Users/jeremycloutier/develop/labs/nexus-schema-plugin-prisma/examples/blog/node_modules/@nexus/schema/src/builder.ts:1473:9)
    at SchemaBuilder.walkTypes (/mnt/c/Users/jeremycloutier/develop/labs/nexus-schema-plugin-prisma/examples/blog/node_modules/@nexus/schema/src/builder.ts:700:16)
    at SchemaBuilder.getFinalTypeMap (/mnt/c/Users/jeremycloutier/develop/labs/nexus-schema-plugin-prisma/examples/blog/node_modules/@nexus/schema/src/builder.ts:799:10)
    at makeSchemaInternal (/mnt/c/Users/jeremycloutier/develop/labs/nexus-schema-plugin-prisma/examples/blog/node_modules/@nexus/schema/src/builder.ts:1597:15)
    at Object.makeSchema (/mnt/c/Users/jeremycloutier/develop/labs/nexus-schema-plugin-prisma/examples/blog/node_modules/@nexus/schema/src/builder.ts:1642:49)
    at Object.<anonymous> (/mnt/c/Users/jeremycloutier/develop/labs/nexus-schema-plugin-prisma/examples/blog/src/schema/index.ts:12:28)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Module.m._compile (/mnt/c/Users/jeremycloutier/develop/labs/nexus-schema-plugin-prisma/examples/blog/node_modules/ts-node/src/index.ts:858:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:827:10)

package.json

{
  "name": "example",
  "private": true,
  "version": "0.0.0",
  "main": "dist/main.ts",
  "scripts": {
    "clean": "rm -rf dist",
    "build": "yarn -s clean && yarn -s generate && tsc",
    "style": "prettier --write 'src/**/*'",
    "dev": "ts-node-dev --tree-kill --no-notify --respawn --transpile-only src/main.ts",
    "dev:migrate": "prisma2 migrate save --experimental -c && prisma2 migrate up --experimental -c",
    "generate": "yarn -s generate:prisma && yarn -s generate:nexus",
    "generate:prisma": "prisma generate",
    "generate:nexus": "NODE_ENV=development ts-node --transpile-only src/schema",
    "data:inject": "ts-node prisma/fake-data.ts"
  },
  "dependencies": {
    "@nexus/schema": "^0.14.0",
    "@prisma/client": "2.0.0-beta.8",
    "graphql": "^14.5.0",
    "graphql-yoga": "^1.18.2",
    "nexus-prisma": "^0.15.0-next.1"
  },
  "devDependencies": {
    "@prisma/cli": "2.0.0-beta.8",
    "@types/faker": "^4.1.12",
    "@types/ws": "^7.2.5",
    "faker": "^4.1.0",
    "prettier": "^2.0.5",
    "ts-node": "^8.10.2",
    "ts-node-dev": "^1.0.0-pre.41",
    "typescript": "^3.9.5"
  },
  "prettier": {
    "semi": false,
    "singleQuote": true,
    "trailingComma": "all"
  }
}

Sorry about that I forgot to update the examples.

Crud is now behind a feature flag.
Add experimentalCRUD: true to the plug-in constructor, that'll fix it

@Weakky that did the trick!

Can I please to add experimentalCRUD to tutorial:

https://www.nexusjs.org/#/tutorial/chapter-5-persisting-data-via-prisma

Should I type:

import { prisma } from 'nexus-plugin-prisma'

use(prisma({
    experimentalCRUD: true
}))

?

Ok. I found example with:

use(prisma({ features: { crud: true } }))

https://github.com/graphql-nexus/examples/blob/master/plugin-prisma/api/app.ts

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ryands17 picture ryands17  路  5Comments

lterfloth picture lterfloth  路  5Comments

marticrespi picture marticrespi  路  4Comments

adarnon picture adarnon  路  3Comments

RAW4RMCS picture RAW4RMCS  路  3Comments