const PrismaClientGenerator = require ( '@prisma/client/generator-build' ) ; import * as SDK from '@prisma/sdk' ; import * as GQL from 'graphql' ; import * as Nexus from 'nexus' ; import stripAnsi from 'strip-ansi' ; import * as NexusPrismaBuilder from '../src/builder' ; import { DmmfDocument } from '../src/dmmf' ; import { transform , TransformOptions } from '../src/dmmf/transformer' ; import { paginationStrategies } from '../src/pagination' ; import { render as renderTypegen } from '../src/typegen' ; export const createNexusPrismaInternal = ( options ) => Nexus . createPlugin ( { name : 'nexus-plugin-prisma-internal' , onInstall : ( nexusBuilder ) => { NexusPrismaBuilder . build ( { ... options , nexusBuilder } ) . types . forEach ( nexusBuilder . addType ) ; } , } ) ; export async function getDmmf ( datamodel , options ? ) { const originalDmmf = await SDK . getDMMF ( { datamodel , } ) ; const clientDmmf = PrismaClientGenerator . externalToInternalDmmf ( originalDmmf ) ; return new DmmfDocument ( transform ( clientDmmf , options ) ) ; } export async function getPinnedDmmfFromSchemaPath ( datamodelPath ) { return SDK . getDMMF ( { datamodelPath , } ) ; } export async function getPinnedDmmfFromSchema ( datamodel ) { return SDK . getDMMF ( { datamodel , } ) ; } export async function generateSchemaAndTypes ( datamodel , types , options ? ) { const dmmf = await getDmmf ( datamodel , options ) ; const nexusPrisma = createNexusPrismaInternal ( { dmmf , experimentalCRUD : options ?. experimentalCRUD === false ? false : true , scalars : options ?. scalars , paginationStrategy : options ?. paginationType ?? 'relay' , } ) ; const schema = Nexus . makeSchema ( { types , plugins : [ nexusPrisma , ... ( options ?. plugins ?? [ ] ) ] , outputs : false , } ) ; return { schemaString : GQL . printSchema ( schema ) , schema , typegen : renderTypegen ( { dmmf , prismaClientImportId : '@prisma/client' , paginationStrategy : options ?. paginationStrategy ?? paginationStrategies . relay , } ) , } ; } export async function generateSchemaAndTypesWithoutThrowing ( datamodel , types , options ? ) { const dmmf = await getDmmf ( datamodel , options ) ; const nexusPrisma = new NexusPrismaBuilder . SchemaBuilder ( { nexusBuilder : { addType : ( ) => false , hasType : ( ) => false , getConfigOption : ( ) => undefined , hasConfigOption : ( ) => false , setConfigOption : ( ) => false , } , dmmf , } ) . build ( ) ; const schemaAndMissingTypes = Nexus . core . makeSchemaInternal ( { types : [ types , nexusPrisma ] , outputs : false , } ) ; const typegen = renderTypegen ( { dmmf , prismaClientImportId : '@prisma/client' , paginationStrategy : paginationStrategies . relay , } ) ; return { schema : GQL . printSchema ( schemaAndMissingTypes . schema ) , missingTypes : schemaAndMissingTypes . missingTypes , typegen , } ; } type UnPromisify < T > = T extends Promise < infer U > ? U : T ; export async function mockConsoleLog < T extends ( ... args : any ) => any > ( func ) { const oldLog = console . log ; let outputData = '' ; const storeLog = ( inputs ) => ( outputData += '\n' + inputs ) ; console . log = jest . fn ( storeLog ) ; const ret = await func ( ) ; console . log = oldLog ; return { ... ret , $output : stripAnsi ( outputData ) , } ; }