Features

  • Auto generate CRUD system from your schema.prisma file.

Every model will have folder contain 2 files

  • typeDefs.ts contain graphql types for this model
  • resolvers.ts contain 3 queries and 6 mutations:
    • findUnique
    • findFirst
    • findMany
    • findCount
    • aggregate
    • createOne
    • updateOne
    • upsertOne
    • deleteOne
    • updateMany
    • deleteMany

Example Usage

For more information about Prisma look at they Docs

schema.prisma

datasource postgresql {
  url      = env("DATABASE_URL")
  provider = "postgresql"
}
generator client {
  provider = "prisma-client-js"
}
model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  email     String   @unique
  name      String?
  role      Role     @default(USER)
  posts     Post[]
}
model Post {
  id         Int        @id @default(autoincrement())
  createdAt  DateTime   @default(now())
  updatedAt  DateTime   @updatedAt
  published  Boolean    @default(false)
  title      String
  author     User?      @relation(fields:  [authorId], references: [id])
  authorId   Int?
}
enum Role {
  USER
  ADMIN
}

After build your schema.prisma file all you need to run

prisma generate
pal g
  • build prisma client
  • auto generate your crud system for more information about pal g command configurations click here

Output For User Model

    import gql from 'graphql-tag';
    
    export default gql`
      type User {
        id: Int!
        email: String!
        name: String
        role: Role!
        createdAt: DateTime!
        posts(
          where: PostWhereInput
          orderBy: PostOrderByInput
          cursor: PostWhereUniqueInput
          take: Int
          skip: Int
        ): [Post!]!
      }
    
      type Query {
        findUniqueUser(where: UserWhereUniqueInput!): User
        findManyUser(
          where: UserWhereInput
          orderBy: UserOrderByInput
          cursor: UserWhereUniqueInput
          skip: Int
          take: Int
        ): [User!]
        findManyUserCount(
          where: UserWhereInput
          orderBy: UserOrderByInput
          cursor: UserWhereUniqueInput
          skip: Int
          take: Int
        ): Int!
      }
      type Mutation {
        createOneUser(data: UserCreateInput!): User!
        updateOneUser(where: UserWhereUniqueInput!, data: UserUpdateInput!): User!
        deleteOneUser(where: UserWhereUniqueInput!): User
        deleteManyUser(where: UserWhereInput): BatchPayload
        updateManyUser(where: UserWhereInput, data: UserUpdateManyMutationInput): BatchPayload
      }
    `;

    Add select function

    It's a small tool to convert info: GraphQLResolveInfo to select object accepted by prisma client this will give you the best performance because you will just query exactly what you want

    This middleware is take info and convert it to Prisma select object and add to resolve args

    server.ts

    import { ApolloServer } from 'apollo-server';
    import { applyMiddleware } from 'graphql-middleware';
    import { makeExecutableSchema } from 'graphql-tools';
    import { createContext, Context } from './context';
    import typeDefs from './graphql/typeDefs';
    import resolvers from './graphql/resolvers';
    import { GraphQLResolveInfo } from 'graphql';
    import { generateGraphQlSDLFile, PrismaSelect } from '@paljs/plugins';
    
    let schema = makeExecutableSchema({ typeDefs, resolvers });
    
    // Build one sdl file have all types you can delete if you not need
    generateGraphQlSDLFile(schema);
    
    const middleware = async (resolve, root, args, context: Context, info: GraphQLResolveInfo) => {
      const result = new PrismaSelect(info).value;
      if (Object.keys(result.select).length > 0) {
        args = {
          ...args,
          ...result,
        };
      }
      return resolve(root, args, context, info);
    };
    
    schema = applyMiddleware(schema, middleware);
    
    const server = new ApolloServer({
      schema,
      context: createContext,
    });
    
    server.listen().then(({ url }) => {
      console.log(`🚀  Server ready at ${url}`);
    });