Skip to content

Commit

Permalink
feat(contact-server-graphql): Added full Prisma GraphQL schema code g…
Browse files Browse the repository at this point in the history
…enerators
  • Loading branch information
sullivanpj committed Aug 5, 2023
1 parent 3135949 commit e924832
Show file tree
Hide file tree
Showing 25 changed files with 328 additions and 151 deletions.
4 changes: 2 additions & 2 deletions libs/contact/typescript/server/graphql/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
},
"module": {
"type": "commonjs",
"strict": true,
"noInterop": false
"strict": false,
"noInterop": false,
},
"sourceMaps": true,
"exclude": [
Expand Down
67 changes: 54 additions & 13 deletions libs/contact/typescript/server/graphql/codegen.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,63 @@
/** @type {import('prisma-generator-pothos-codegen').Config} */
module.exports = {
inputs: {
// builderImporter: "import { builder } from '../builder';",
prismaImporter: `import { prisma } from '../prisma';`,
outputFilePath: "./src/__generated__/crud/inputs.ts",
},
crud: {
outputDir: "./src/__generated__/crud",
prismaCaller: "prisma",
disabled: false,
generateAutocrud: true,
inputsImporter: "import * as Inputs from '../inputs'",
builderImporter: "import { builder } from '../../builder';",
inputsImporter: "import * as Inputs from '../inputs';",
deleteOutputDirBeforeGenerate: true,
exportEverythingInObjectsDotTs: true,
prismaImporter: `import { prisma } from '../prisma';`,
prismaCaller: "client",
resolverImports: `\nimport { client } from '../../builder';`,
prismaImporter: `import { Prisma } from '@prisma/client/contact';`,
resolverImports: `\nimport { prisma } from '../../../../client';`,
replacer(generated, position) {
// replacer(generated, position) {
// return `// THIS CONTENT WAS INSERTED AT REPLACE. THE POSITION IS ${position}\n${generated}`
// },

//export const ContactAttachmentObject = definePrismaObject('ContactAttachment',
generated = generated.replace(
"type Types = typeof builder extends PothosSchemaTypes.SchemaBuilder<infer T> ? T : unknown;",
"export type Types = typeof builder extends PothosSchemaTypes.SchemaBuilder<infer T> ? T : unknown;"
);

const regex = /export const (\w+)Object = definePrismaObject\('(\w+)',/g;
const match = regex.exec(generated);
if (match && match.length > 1) {
generated = generated.replace(
`export const ${match[1]}Object = definePrismaObject('${match[1]}'`,
`import { InterfaceParam } from "@pothos/core";
import { PrismaObjectTypeOptions } from "@pothos/plugin-prisma";
import { Types } from "../utils";
export const ${match[1]}Object: PrismaObjectTypeOptions<
Types,
Types["PrismaTypes"]["${match[1]}"],
InterfaceParam<Types>[],
any,
Types["PrismaTypes"]["${match[1]}"]["Include"],
Types["PrismaTypes"]["${match[1]}"]["Select"],
Types["PrismaTypes"]["${match[1]}"]["Shape"]
> = definePrismaObject("${match[1]}"`
);
}

return generated
.replace(
"resolve: async (query, _root, args, _context, _info)",
"resolve: async (query, _root, args: any, _context, _info)"
)
.replace(
"resolve: async (_root, args, _context, _info)",
"resolve: async (_root, args: any, _context, _info)"
)
.replace("query: undefined,", "")
.replace("query: (args) =>", "query: (args: any) =>");
},
},
global: {
builderImporter: `import { builder } from '../../builder';`,
inputs: {
builderImporter: "import { builder } from '../../builder';",
prismaImporter: `import { Prisma } from '@prisma/client/contact';`,
outputFilePath: "./src/__generated__/crud/inputs.ts",
},
global: {},
};
100 changes: 46 additions & 54 deletions libs/contact/typescript/server/graphql/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
datasource db {
provider = "mongodb"
url = env("CONTACT_DB_CONNECTION_STRING")
url = env("CONTACT_DATABASE_URL") // uses connection pooling
// directUrl = env("CONTACT_DATABASE_DIRECT_URL") // uses a direct connection
// shadowDatabaseUrl = env("CONTACT_DATABASE_DIRECT_URL") // used for migrations
}

generator client {
provider = "prisma-client-js"
// Generate into custom location because this repo has multiple prisma schemas
output = "../src/__generated__/prisma"
output = "../../../../../../../node_modules/.prisma/client/contact"
}

generator pothos {
provider = "prisma-pothos-types"
// Match client output location from above
clientOutput = "./prisma"
clientOutput = "@prisma/client/contact"
prismaUtils = true
output = "../src/__generated__/types.ts"
output = "../src/__generated__/types.d.ts"
}

generator pothosCrud {
provider = "prisma-generator-pothos-codegen"
generatorConfigPath = "../codegen.config.js"
}

generator zod {
provider = "zod-prisma-types"
output = "../src/__generated__/zod"
useMultipleFiles = true // default is false
writeBarrelFiles = true // default is true
createInputTypes = true // default is true
createModelTypes = false // default is true
addInputTypeValidation = true // default is true
addIncludeType = true // default is true
addSelectType = true // default is true
validateWhereUniqueInput = true // default is false
createOptionalDefaultValuesTypes = true // default is false
createRelationValuesTypes = true // default is false
createPartialTypes = true // default is false
useDefaultValidators = true // default is true
coerceDate = true // default is true
writeNullishInModelTypes = true // default is false
prismaClientPath = "../prisma"
}
//generator zod {
// provider = "zod-prisma-types"
// output = "../src/__generated__/zod"
// useMultipleFiles = false // default is false
// writeBarrelFiles = true // default is true
// createInputTypes = true // default is true
// createModelTypes = false // default is true
// addInputTypeValidation = true // default is true
// addIncludeType = false // default is true
// addSelectType = false // default is true
// validateWhereUniqueInput = true // default is false
// createOptionalDefaultValuesTypes = true // default is false
// createRelationValuesTypes = false // default is false
// createPartialTypes = false // default is false
// useDefaultValidators = true // default is true
// coerceDate = true // default is true
// writeNullishInModelTypes = true // default is false
// prismaClientPath = "@prisma/client/contact"
//}

enum ContactReason {
Business
Expand All @@ -51,26 +53,28 @@ enum ContactReason {
Subscription
}

enum AttachmentStatus {
Pending
Approved
Rejected
Removed
}

/// The data fields related to a specific Contact request instance
model Contact {
/// The unique identifier for the Contact
/// @Pothos.omit(create)
/// @Pothos.omit(create) The unique identifier for the Contact
id String @id @default(auto()) @map("_id") @db.ObjectId
/// A timestamp of when the Contact was created
/// @Pothos.omit(create, update)
/// @Pothos.omit(create, update) A timestamp of when the Contact was created
createdOn DateTime @default(now())
/// The user who created the Contact
/// @Pothos.omit(create, update)
/// @Pothos.omit(create, update) The user who created the Contact
createdBy String @db.ObjectId
/// A timestamp of when the Contact was last updated
/// @Pothos.omit(create, update)
/// @Pothos.omit(create, update) A timestamp of when the Contact was last updated
updatedOn DateTime? @updatedAt
/// The user who last updated the Contact
/// @Pothos.omit(create, update)
/// @Pothos.omit(create, update) The user who last updated the Contact
updatedBy String? @db.ObjectId
/// The reason of the Contact
Expand All @@ -80,10 +84,10 @@ model Contact {
details String?
/// The provided email to respond back to the Contact request
email String /// @zod.string.email()
email String
/// The provided phone number to respond back to the Contact request
phoneNumber String? /// @zod.string.max(15)
phoneNumber String?
/// The provided first name of the Contact
firstName String?
Expand All @@ -98,7 +102,7 @@ model Contact {
addressLine2 String?
/// The provided postal code of the Contact
postalCode String? /// @zod.string.min(5).max(12)
postalCode String?
/// The provided city of the Contact
city String?
Expand All @@ -116,39 +120,27 @@ model Contact {
companyName String?
/// The provided URL of the Contact
url String? /// @zod.string.url()
url String?
/// A list of file attachments included by the Contact
attachments ContactAttachment[]
}

enum AttachmentStatus {
Pending
Approved
Rejected
Removed
}

/// A file attachment included by a Contact
model ContactAttachment {
/// The unique identifier for the ContactAttachment
/// @Pothos.omit(create)
/// @Pothos.omit(create) The unique identifier for the ContactAttachment
id String @id @default(auto()) @map("_id") @db.ObjectId
/// A timestamp of when the ContactAttachment was created
/// @Pothos.omit(create, update)
/// @Pothos.omit(create, update) A timestamp of when the ContactAttachment was created
createdOn DateTime @default(now())
/// The user who created the ContactAttachment
/// @Pothos.omit(create, update)
/// @Pothos.omit(create, update) The user who created the ContactAttachment
createdBy String @db.ObjectId
/// A timestamp of when the ContactAttachment was last updated
/// @Pothos.omit(create, update)
/// @Pothos.omit(create, update) A timestamp of when the ContactAttachment was last updated
updatedOn DateTime? @updatedAt
/// The user who last updated the ContactAttachment
/// @Pothos.omit(create, update)
/// @Pothos.omit(create, update) The user who last updated the ContactAttachment
updatedBy String? @db.ObjectId
/// The name of the file
Expand Down
66 changes: 57 additions & 9 deletions libs/contact/typescript/server/graphql/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,83 @@
"packageJson": "libs/contact/typescript/server/graphql/package.json",
"main": "libs/contact/typescript/server/graphql/src/index.ts",
"assets": [
"libs/contact/typescript/server/graphql/*.md"
{
"glob": "libs/contact/typescript/server/graphql/README.md",
"input": ".",
"output": "."
}
]
}
},
"prisma-init": {
"command": "prisma init",
"prisma-db-pull": {
"executor": "nx:run-commands",
"options": {
"command": "npx prisma db pull",
"cwd": "libs/contact/typescript/server/graphql"
}
},
"prisma-db-push": {
"executor": "nx:run-commands",
"options": {
"command": "npx prisma db push",
"cwd": "libs/contact/typescript/server/graphql"
}
},
"prisma-migrate": {
"command": "prisma migrate dev",
"executor": "nx:run-commands",
"options": {
"command": "npx prisma migrate dev",
"cwd": "libs/contact/typescript/server/graphql"
}
},
"prisma-format": {
"executor": "nx:run-commands",
"options": {
"command": "npx prisma format",
"cwd": "libs/contact/typescript/server/graphql"
}
},
"generate": {
"command": "prisma generate --data-proxy",
"prisma-generate": {
"executor": "nx:run-commands",
"defaultConfiguration": "direct",
"options": {
"command": "npx prisma generate",
"cwd": "libs/contact/typescript/server/graphql"
},
"configurations": {
"direct": {
"options": {
"command": "npx prisma generate",
"cwd": "libs/contact/typescript/server/graphql"
}
},
"data-proxy": {
"options": {
"command": "npx prisma generate --data-proxy",
"cwd": "libs/contact/typescript/server/graphql"
}
}
}
},
"prisma-full": {
"executor": "nx:run-commands",
"options": {
"commands": [
"npx nx run contact-server-graphql:prisma-db-pull",
"npx nx run contact-server-graphql:prisma-generate",
"npx nx run contact-server-graphql:prisma-format",
"npx nx run contact-server-graphql:bundle",
"npx nx run contact-server-graphql:prisma-migrate",
"npx nx run contact-server-graphql:prisma-db-push"
],
"cwd": "libs/contact/typescript/server/graphql"
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"libs/contact/typescript/server/graphql/**/*.ts"
]
"lintFilePatterns": ["libs/contact/typescript/server/graphql/**/*.ts"]
}
},
"test": {
Expand Down
31 changes: 25 additions & 6 deletions libs/contact/typescript/server/graphql/src/builder.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { createSchemaBuilder } from "@open-system/core-server-graphql";
import { PrismaClient } from "./__generated__/prisma";
import { Prisma } from "@prisma/client/contact";
import PrismaTypes from "./__generated__/types";
import { prisma } from "./client";
import { ContactApiServerContext } from "./types";

export const client = new PrismaClient({
log: ["error", "info", "query", "warn"],
});

export const builder = createSchemaBuilder<
PrismaTypes,
ContactApiServerContext
>(client);
>(prisma, Prisma.dmmf);

/*export const builder = new SchemaBuilder<{
Context: any;
PrismaTypes: PrismaTypes;
Scalars: any;
}>({
plugins: [PrismaPlugin],
prisma: {
client: prisma,
},
notStrict:
"Pothos may not work correctly when strict mode is not enabled in tsconfig.json",
relayOptions: {
clientMutationId: "optional",
cursorType: "ID",
idFieldName: "id",
brandLoadedObjects: true,
nodesOnConnection: true,
encodeGlobalID,
decodeGlobalID,
},
});*/
Loading

0 comments on commit e924832

Please sign in to comment.