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

@txo/nexus-prisma

v2.0.3

Published

Technology Studio - Nexus prisma

Downloads

10

Readme

npm codecov

Nexus prisma

Collection of utils for integration between nexus and prisma.

prismify

Will deeply remove null types on attributes that are both undefined or null. That ensures a temporary workaround until graphql differentiates between optional and nullable types.

it supports specify

  • key renaming - if the key in your outer schema is different from in your internal prisma schema,
  • value mapping - if the value in your outer schema needs to be mapped to a different structure required by internal prisma schema
Example

It shows how to do mapping if you internally support soft deletions and all unique keys are composed of the deleted attribute.

prisma/schema.prisma

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model Bar {
  id    String  @id @default(uuid())
  foo   Foo?    @relation(fields: [fooId], references: [id])
  fooId String?
}

model Foo {
  id      String @id @default(uuid())
  key     String
  deleted Boolean

  barList Bar[]

  @@unique([key, deleted])
}

schema.ts

import {
  arg, inputObjectType, makeSchema, nonNull, objectType,
} from 'nexus'
import { join } from 'path'
import {
  atLeastOne,
} from '@txo/types'

import {
  prismify,
} from '../src'

const BarWhereUniqueInput = inputObjectType({
  name: 'BarWhereUniqueInput',
  definition (t) {
    t.nullable.id('id')
  },
})

const BarUpdateOneWithoutBarListNestedInput = inputObjectType({
  name: 'BarUpdateOneWithoutBarListNestedInput',
  definition (t) {
    t.field('connect', { type: 'FooWhereUniqueInput' })
  },
})
const BarUpdateInput = inputObjectType({
  name: 'BarUpdateInput',
  definition (t) {
    t.field('foo', { type: 'BarUpdateOneWithoutBarListNestedInput' })
  },
})

const FooWhereUniqueInput = inputObjectType({
  name: 'FooWhereUniqueInput',
  definition (t) {
    t.nullable.id('id')
    t.nullable.id('key')
  },
})

const whereUniqueInputMapper = {
  key: {
    key: 'key_deleted' as const,
    value: (key: string | undefined | null) => key == null
      ? undefined
      : ({
          key,
          deleted: false,
        }),
  },
}

const Bar = objectType({
  name: 'Bar',
  definition (t) {
    t.id('id')
    t.field('update', {
      type: 'Bar',
      args: {
        data: nonNull(arg({ type: 'BarUpdateInput' })),
      },
      resolve: async (_parent, args, ctx, _info) => {
        const prismifiedArgs = prismify(args, {
          data: {
            foo: {
              connect: {
                value: (connect) => atLeastOne(prismify(connect, whereUniqueInputMapper)),
              },
            },
          },
        })
        const updatedBar = await ctx.prisma.bar.update({
          ...prismifiedArgs,
          where: {
            id: '1',
          },
        })
        return updatedBar
      },
    })
  },
})

const Foo = objectType({
  name: 'Foo',
  definition (t) {
    t.list.field('barList', {
      type: 'Bar',
      args: {
        where: nonNull(arg({
          type: 'BarWhereUniqueInput',
        })),
        cursor: arg({
          type: 'BarWhereUniqueInput',
        }),
      },
      resolve: async (_parent, args, ctx, _info) => {
        const prismifiedArgs = prismify(args, {
          cursor: {
            value: (cursor) => atLeastOne(cursor),
          },
        })
        const barList = await ctx.prisma.bar.findMany({
          ...prismifiedArgs,
        })
        return barList
      },
    })
  },
})

export const schema = makeSchema({
  nonNullDefaults: {
    output: true,
  },
  types: [
    Bar,
    BarWhereUniqueInput,
    BarUpdateOneWithoutBarListNestedInput,
    BarUpdateInput,
    Foo,
    FooWhereUniqueInput,
  ],
  outputs: {
    schema: join(__dirname, '../generated', 'schema.graphql'),
    typegen: join(__dirname, '../node_modules/@types/typegen-nexus/index.d.ts'),
  },
  contextType: {
    module: join(__dirname, './ContextType.ts'),
    export: 'Context',
  },
})