npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@davidmwhynot/ra-data-prisma-backend

v1.0.0

Published

> TODO: description

Downloads

72

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 (@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
  }
);