Hi! I decided to try using Prisma for the first ti...
# orm-help
i
Hi! I decided to try using Prisma for the first time and encountered a problem regarding adding business logic to resolvers. Suppose I have a mutation
createOrder
. A user can only create order if they have less than 5 active orders
v
I think you misunderstood the purpose of Prisma Data Access Layer (DAL). After you design your data models, it's exposing a graph that enables u to query/mutate the data with his fluent API or directly through GraphQL Decoupling the underlying database related complexity/logic (SQL, NoSQL, syntax, aggregations, etc). Instead "adding business logic" is not in the scope of the DAL, you must handle this need in different ways: - on Client Apps: client apps (business-logics) => Prisma => Database you can directly call Prisma from your client apps that are in charge to handle the business-logics: before creating the order, you've to count the number of already placed orders and decide if the user can or can't place the order. - on Application Server client apps => https://myapplicationserver.io/ (business-logics) => Prisma => Database you need to create another service in order to expose a REST-API or a Graph with your business logic.
Copy code
const resolvers = {
    Mutation: {
      createOrder: async (_root, args, ctx) => {
        const { prisma } = ctx
        const { order } = args

          const ordersCountPerUser = await prisma
            .ordersConnection({ where: { user: order.userId } })
            .aggregate()
            .count()

          if (ordersCountPerUser >= 5) {
            // handle your fail case
            throw new Error('Can't place more than 5 orders!')
          }

          return prisma.createOrder(order)
      }
  }
}
some examples HowTo: - https://github.com/prisma/prisma-examples/tree/master/node/graphql-schema-delegation - https://github.com/prisma/prisma-examples/tree/master/node/rest-express