prisma-labs / graphql-framework-experiment

Code-First Type-Safe GraphQL Framework
https://nexusjs.org
MIT License
673 stars 66 forks source link

Expose scalar mapping on the Prisma framework plugin #1111

Closed flamedmg closed 1 year ago

flamedmg commented 4 years ago

Nexus Report

{
  "node": "v14.4.0",
  "os": {
    "platform": "darwin",
    "release": "19.5.0"
  },
  "errorsWhileGatheringReport": {
    "gettingLayout": {
      "type": "MalformedPackageJson",
      "context": {
        "path": "/Users/dmg/Projects/tmp/graphql-auth/package.json",
        "reason": "Package.json version field is not a string"
      }
    },
    "gettingPluginManifests": null
  }
}

Description

Actual database is mysql and uses bigint for all ids. What i'm trying to achive is to make bigint ids to work. If numbers that are placed in id can be fitted in int32 - everything seems to work good, but if there are numbers that are greater then that, graphql refusing to work and complaining that it can't fit number into int32

Please consider the following prisma.schema

model Post {
  content String?
  id      Int     @default(autoincrement()) @id
  title   String
  user_id Int?
  User    User?   @relation(fields: [user_id], references: [id])
}
model User {
  id   Int     @default(autoincrement()) @id
  name String?
  Post Post[]
}

nexus definitions are straight forward. The only difference is that i added crud operations. To handle bigint values i created custom scalar type:

export const Long = scalarType({
  name: 'Long',
  description: 'Long custom scalar type',
  parseValue(value) {
    return value
  },
  serialize(value) {
    return value
  },
  parseLiteral(ast) {
    return parseInt(ast.value)
  },
})

User definition is as follows:

export const User = objectType({
  name: 'User',
  definition(t) {
    t.model.id({ type:"Long"})
    ...

Nexus properly generates User schema which looks like this:

type User {
  businessForms: [BusinessForm!]!
  createdAt: DateTime
  deskproUserId: Int
  deviceId: String
  email: String!
  firstName: String!
  id: Long!    <-------------- properly uses Long type
....

But crud types are generated using prisma.schema defined types instead of defined in users objectType

input UserWhereUniqueInput {
  id: Int                   <----------------------- Should be long, not int
  username: String
}
Weakky commented 4 years ago

Hey @flamedmg, this is technically not a bug. Fields of input types and object types are different fields. There's already a feature implemented under the hood to configure the scalar mapping globally. This feature is unfortunately not exposed on the framework plugin yet. We need to expose it to allow your use-case. Would the API below solve your issue?

import { prisma } from 'nexus-plugin-prisma'

prisma({
  scalars: {
    Int: scalarType({ name: 'Long', /* ... */ })
  }
})
flamedmg commented 4 years ago

Hello, @Weakky ! Thank you for you help. Your proposed solution does not work. I used it like this:

import { nexusSchemaPrisma } from 'nexus-plugin-prisma/schema'
import { prisma } from 'nexus-plugin-prisma'
import { makeSchema } from '@nexus/schema'
import * as types from './types'

prisma({
  scalars: {
    Int: scalarType({ name: 'Long', /* ... */ })
  }
})

export const schema = makeSchema({
  types:[ 
    types,
  ],
  plugins: [nexusSchemaPrisma({
    experimentalCRUD: true, // required!
    paginationStrategy: "prisma", // required!
  })],
  outputs: {
    schema: __dirname + '/../schema.graphql',
    typegen: __dirname + '/generated/nexus.ts',
  },
  typegenAutoConfig: {
    sources: [
      {
        source: '@prisma/client',
        alias: 'client',
      },
      {
        source: require.resolve('./context'),
        alias: 'Context',
      },
    ],
    contextType: 'Context.Context',
  },
})

I got the following error:

Using ts-node version 8.10.1, typescript version 3.9.5
ReferenceError: scalarType is not defined
    at Object.<anonymous> (/Users/dmg/Projects/tmp/graphql-auth/src/schema.ts:9:10)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Module._compile (/Users/dmg/Projects/tmp/graphql-auth/node_modules/source-map-support/source-map-support.js:547:25)
    at Module.m._compile (/private/var/folders/2d/rhs74gk94bv_cvb81jzhwq_40000gn/T/ts-node-dev-hook-4641486286105545.js:57:25)
    at Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at require.extensions.<computed> (/private/var/folders/2d/rhs74gk94bv_cvb81jzhwq_40000gn/T/ts-node-dev-hook-4641486286105545.js:59:14)
    at Object.nodeDevHook [as .ts] (/Users/dmg/Projects/tmp/graphql-auth/node_modules/ts-node-dev/lib/hook.js:61:7)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
[ERROR] 20:24:29 ReferenceError: scalarType is not defined
Weakky commented 4 years ago

The proposed solution is a proposal, not something already implemented. The question was:

Would the API below solve your issue?

flamedmg commented 4 years ago

Yes, it will!

On Mon, Jul 20, 2020 at 5:16 PM Flavian Desverne notifications@github.com wrote:

The proposed solution is a proposal, not something already implemented. The question was:

Would the API below solve your issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/graphql-nexus/nexus/issues/1111#issuecomment-661067394, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADFVCJOPJZPUF2I5N4GA73R4RGUDANCNFSM4OIN73LA .

-- Thanks & Regards Dmitry

cclaflin89 commented 4 years ago

@Weakky I'm also facing the same issue. Is it resolved now?