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

@akumzy/gri-prisma-select

v0.0.4

Published

Generate Prisma select object from GraphQL Resolve Info.

Downloads

134

Readme

gri-prisma-select

Generate Prisma select object from GraphQL Resolve Info

GraphQL Resolve Info is a data structure that contains information about the execution of a GraphQL query, with this information you can generate Prisma select object which extends the benefits GraphQL provides to clients to your database by only fetching the fields that are needed and as well resolving the GraphQL n+1 query problem with the power of Prisma.

Demo:

// Turn this to a GraphQL query:
const query = /* GraphQL */ `
  {
    user {
      id
      name
      email
      createdAt
      photo
      posts {
        id
        title
        content
        createdAt
        comments {
          id
          content
          createdAt
          author {
            id
            name
            photo
          }
        }
      }
    }
  }
`
// Into this Prisma select object:
const prismaSelect = {
  select: {
    id: true,
    name: true,
    email: true,
    createdAt: true,
    photo: true,
    posts: {
      select: {
        id: true,
        title: true,
        content: true,
        createdAt: true,
        comments: {
          select: {
            id: true,
            content: true,
            createdAt: true,
            author: {
              select: {
                id: true,
                name: true,
                photo: true,
              },
            },
          },
        },
      },
    },
  },
}

Installation

# with npm
npm install @akumzy/gri-prisma-select
# with yarn
yarn add @akumzy/gri-prisma-select
# with pnpm
pnpm add @akumzy/gri-prisma-select

Usage

This can be used in two ways:

  • As a GraphQL directive
  • As a function in your GraphQL resolver

Using GraphQL directive:

Add directive to your GraphQL schema

directive @toPrismaSelect on FIELD_DEFINITION
...

Register directive

import { toPrismaSelectDirectiveTransformer } from "@akumzy/gri-prisma-select"

let schema = makeExecutableSchema({
  typeDefs,
  resolvers,
})
schema = toPrismaSelectDirectiveTransformer(schema, "toPrismaSelect")

and use the directive in your GraphQL schema to indicate which query you want to generate a Prisma select object for. e.g.

directive @toPrismaSelect on FIELD_DEFINITION
type Query {
  posts: [Post!]! @toPrismaSelect
}

and by doing so a property called prismaSelect will be added to your resolver context

async function posts(parent, args, context, info) {
  const prismaSelect = context.prismaSelect
  return await context.prisma.posts.findMany({
    where: {},
    ...prismaSelect,
  })
}

Using in your GraphQL resolver:

All you need to do is to pass the GraphQL resolve info to the function and it will return a Prisma select object.

import { toPrismaSelect } from "@akumzy/gri-prisma-select"

async function posts(parent, args, context, info) {
  const prismaSelect = toPrismaSelect(info)
  return await context.prisma.posts.findMany({
    where: {},
    ...prismaSelect,
  })
}

Notes

For this package to work as expected you need to make sure that your GraphQL schema has same field names as your Prisma model. Also note that you have given absolute power to your client to fetch any field that is available in GraphQL schema and exists in your Prisma model as well, so be careful not to expose any sensitive information fields in your GraphQL schema.

Another issue you'll face will be unkown type for your Prisma return value.

License

mit