# @ra-data-prisma/backend

> > TODO: description

Latest version **8.0.0** (published 2023-05-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install @ra-data-prisma/backend
pnpm add @ra-data-prisma/backend
yarn add @ra-data-prisma/backend
bun add @ra-data-prisma/backend
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 8.0.0 |
| Published | 2023-05-05 |
| First published | 2020-05-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 35.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Marco Wettstein |
| Maintainers | macrozone, claudiocro |

## Links

- npm: https://www.npmjs.com/package/@ra-data-prisma/backend
- npm.io page: https://npm.io/package/@ra-data-prisma/backend

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [pluralize](https://npm.io/package/pluralize.md) ^7.0.0

## Recent versions

- 8.0.0 (latest) — 2023-05-05
- 9.0.0 (beta) — 2023-11-08
- 2.0.0 (next) — 2020-06-11
- 8.0.0-beta.1 — 2022-08-04
- 7.2.0 — 2021-11-22
- 7.2.0-beta.1 — 2021-10-12
- 7.1.0 — 2021-09-27
- 7.0.0 — 2021-04-16
- 6.0.0 — 2021-02-19
- 5.0.0 — 2020-12-01
- 4.0.0 — 2020-10-03
- 3.0.1 — 2020-08-13
- 3.0.0 — 2020-08-13
- 2.4.0 — 2020-08-13
- 2.3.0 — 2020-08-12
- … 8 more at https://npm.io/package/@ra-data-prisma/backend/versions

## README

# `@ra-data-prisma/backend`

this package makes your graphql-nexus api compatible with the dataprovider by exposing all needed Queries and Mutations.

## Usage with @nexus/schema

`yarn add @ra-data-prisma/backend`

make sure that you use `@nexus/schema` version `^0.15.0` and `nexus-plugin-prisma` version `^0.18.2`.

**important: set `paginationStrategy: "prisma"` and `experimentalCRUD: true` as options for nexusPrismaPlugin`**

`addCrudResolvers(modelName, options)` 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 { nexusSchemaPrisma } from 'nexus-plugin-prisma/schema'

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 required Query's and Mutation's.
  ],
  plugins: [
    nexusSchemaPrisma({
      experimentalCRUD: true, // required!
      paginationStrategy: "prisma", // required!
      outputs: {
        typegen: typegenPath("./generated/nexus-prisma.ts")
      }
    })
  ],
  typegenAutoConfig: {
     // ...
  },
  outputs: {
      // ...
  }
});


```

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

## Enable sort by relation

You can sort by relations. It's currently a preview feature in prisma: https://www.prisma.io/docs/concepts/components/prisma-client/filtering-and-sorting#sort-by-relation-preview

1. make sure you use a more resent prisma version. notice: nexus-plugin-prisma will complain about incompatibility, but it still works with most versions
2. its enabled by default now ~enable it in `addCrudResolvers`: `addCrudResolvers(modelName, {enableOrderByRelation: true})`~
3. in react-admin edit the `<ReferenceField />` for this column:

```
<ReferenceField

  label="<your label>"
  source="<field>"
  reference="<resource name of the relation>"
  sortBy="<field>.<related field>"
>
```

e.g. if you have a list of blog posts each with an author and you want to sort by the author's lastname:

```
<ReferenceField

  label="Author"
  source="author"
  reference="User"
  sortBy="author.lastname"
>
```

## Security

Make sure that you restrict access to these resolvers using [graphql-shield](https://github.com/maticzav/graphql-shield) (@nexus/schema)

this could look like this (for @nexus/schema)

```
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),
  // ...
})


```

### prefix all queries and mutations

To make it more obvious which resolvers are for the admin area (and therefore need access control), we recommend to set `aliasPrefix`:

_nexus schema_

```

addCrudResolvers("User", {aliasPrefix: "admin"})

```

\*\*Make sure that your dataprovider uses the same `aliasPrefix` as well.

### alternative approach

in larger projects it could be a good idea 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
  }
);
```

---
_Source: https://npm.io/package/@ra-data-prisma/backend · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
