8.0.0 • Published 6 months ago

@ra-data-prisma/backend v8.0.0

Weekly downloads
82
License
ISC
Repository
-
Last release
6 months ago

@ra-data-prisma/backend

Usage

yarn add @ra-data-prisma/backend

addCrudResolvers(modelName) will make your Model compatible with react-admin. It will become a Resource to react-admin:

import { addCrudResolvers } from '@ra-data-prisma/backend';
import { makeSchema } from "@nexus/schema";
import { nexusPrismaPlugin } from "nexus-prisma";

type User = objectType({
  name: "User",
  definition(t) {
    t.field("id") // be sure to expose id for all entities that you want to dit
    t.field("email")
    t.field("firstname")
    t.field("lastname")
  }
})

const schema = makeSchema({
  types: [
    User,
    addCrudResolvers("User") // 👈 this will expose all needed Query's and Mutation's.
  ],
  plugins: [
    nexusPrismaPlugin({
      experimentalCRUD: true, // required
      outputs: {
        typegen: typegenPath("./generated/nexus-prisma.ts")
      }
    })
  ],
  typegenAutoConfig: {
     // ...
  },
  outputs: {
      // ...
  }
});

addCrudResolvers will add all needed Mutation's and Query's. Make sure that you restrict access to these resolvers using something like graphql-shield

this could look like this:

import { rule, allow, shield } from "graphql-shield";

const isAdmin = rule({ cache: false })(
  async (parent, args, { user, prisma }: Context, info) => {
    if (!user) {
      return false;
    }

    return prisma.user
      .findOne({ where: { id: user.id } })
      .roles({
        where: {
          id: "admin"
        }
      })
      .then(roles => roles.length > 0);
  }
);


const permissions = shield(
  {
    Query: {
      "*": allow,
      users: isAdmin,
      user: isAdmin,

    },
    Mutation: {
      "*": isAdmin,
        // we highly recommend to whitelist the mutations that should be possible for non-admins
    }
  },
);

export default new ApolloServer({
  schema: applyMiddleware(schema, permissions),
  // ...
})

in larger projects its recommended that you simply expose a second graphql-endpoint that contains only the admin-CRUD queries and mutations. The permission handling will then be very simple:

const permissions = shield(
  {
  },
  {
      fallbackRule: isAdmin
  }
);

use addCrudResolvers for every Model that you want to manage in react-admin. Additionaly if you have a relation between two Models, call it for both Models even if you only want to show one in a list

9.0.0

6 months ago

8.0.0

1 year ago

8.0.0-beta.1

2 years ago

7.2.0

2 years ago

7.2.0-beta.1

3 years ago

7.1.0

3 years ago

7.0.0

3 years ago

6.0.0

3 years ago

5.0.0

3 years ago

4.0.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.4.0

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago