GraphQL Basics: Part 1 - Prisma (The Why)
Tags: beginners, tutorial, graphql, webdev
Published: 2019-03-09 04:10:05

---
title: GraphQL Basics: Part 1 - Prisma (The Why)
published: true
description: 
tags: beginners, tutorial, graphQL, webdev
cover_image: https://www.prisma.io/static/posts/introducing-prisma.png
series: GraphQL Basics
---

## Let's Get Started
After my last post about why I am working with GraphQL, I dove in setting up a little tutorial project with my GraphQL backend set up.  I had set up a backend before in tutorials, but this was the first time doing everything from scratch and I have to say it was the best thing I have done for myself. I said this in my last post, but I have found the best way to know that you understand something (or think that you understand it at least) is to try and teach it to someone else. In this post, I hope to give the groundwork for getting started with Prisma.

## Why/(What is) Prisma?
Ok, so you don't need to use Prisma with GraphQL, but when I first got to see what Prisma could do, it was a huge time saver. Essentially Prisma creates ALL of the possible CRUD operations for your database. That doesn't mean it creates all of the logic, just the actual methods to interact with the database.

__How does this witchcraft work?!__
Well, it's not total witchcraft.  Basically, you tell Prisma what your data model is, and it goes from there to create all your basic CRUD operations. Another way of thinking of Prisma is that you are providing your data model or the structure of your objects you want to keep in your database, and Prisma makes an API for you to interact with the database.

__For Example:__
Imagine reading from your database. Let's say you are storing "posts" in your database. Those could have a structure like this:
```
type Post {
  id: ID! @unique
  title: String!
  published: Boolean!
  author: User
}
```
When you set up Prisma, you would write this in a file (datamodel.prisma) and you deploy that to Prisma.  You can then download a generated file that provides all sorts of queries(reading) and mutations(create, edit, deleting) for those objects. So, for your data model you provide, you would get something like this:
```
type Query {
  post(where: PostWhereUniqueInput!): Post
  posts(where: PostWhereInput, orderBy: PostOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [Post]!
  postsConnection(where: PostWhereInput, orderBy: PostOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): PostConnection!
  node(id: ID!): Node
}

type Mutation {
  createPost(data: PostCreateInput!): Post!
  updatePost(data: PostUpdateInput!, where: PostWhereUniqueInput!): Post
  updateManyPosts(data: PostUpdateManyMutationInput!, where: PostWhereInput): BatchPayload!
  upsertPost(where: PostWhereUniqueInput!, create: PostCreateInput!, update: PostUpdateInput!): Post!
  deletePost(where: PostWhereUniqueInput!): Post
  deleteManyPosts(where: PostWhereInput): BatchPayload!
}
```
Take a look at deleteManyPosts, in the Mutations.  One of the things I first wondered about was...so I can see it accepts this "where" argument, but what is the PostWhereInput supposed to be?  Luckily, all that is provided as well! Check it out below:
```
input PostWhereInput {
  id: ID
  id_not: ID
  id_in: [ID!]
  id_not_in: [ID!]
  id_lt: ID
  id_lte: ID
  id_gt: ID
  id_gte: ID
  id_contains: ID
  id_not_contains: ID
  id_starts_with: ID
  id_not_starts_with: ID
  id_ends_with: ID
  id_not_ends_with: ID
  title: String
  title_not: String
  title_in: [String!]
  title_not_in: [String!]
  title_lt: String
  title_lte: String
  title_gt: String
  title_gte: String
  title_contains: String
  title_not_contains: String
  title_starts_with: String
  title_not_starts_with: String
  title_ends_with: String
  title_not_ends_with: String
  published: Boolean
  published_not: Boolean
  author: UserWhereInput
  AND: [PostWhereInput!]
  OR: [PostWhereInput!]
  NOT: [PostWhereInput!]
}
```
That a serious amount of options! You can see if I simply wanted to delete a bunch of posts, I could us 
`deleteManyPosts(where: { author: thatCrazyGuyThatGotBanned })`.
The crazy user posting weird things would have all their posts deleted! Easy as that! 

## So that's it?
Well, not entirely.  The beauty of Prisma is it gives you all the possible methods you might want for your CRUD operations. You do not need to worry about creating a way with, say items in a store, to figure out how to query them based on their price. Prisma does that for you by giving you the greater than, less than, id, all that stuff. It's also database agnostic so you can use many different types of databases like MySQL or Mongo. 

What Prisma does not give you is the logic, or the actual controller methods you would need in your application to actually implement this Prisma "API."  So, if you are making an application where someone could make a post, you would still need to write logic to make sure, for example, they are logged in or have the right permissions. But once you've worked that out, you can easily reach for 
`createPost(data: putYourDataHere )`.

## Setting Up Prisma
In my [next post](https://dev.to/ryanmdoyle/graphql-basics-part-2---prisma-the-how-1356), I will be working through setting up Prisma using their demo database.  It's a great way to get everything up and running for a personal project or to learn what it's all about. If you're impatient, head on over to [Prisma.io](https://www.prisma.io/) and check out their tutorial. In mine, I will try to break it down a little more simply, as well as get it set up to automatically generate and download the prisma.graphql file you can check out what Prisma generates for you. [Check out Part 2 now! ->](https://dev.to/ryanmdoyle/graphql-basics-part-2---prisma-the-how-1356)

{% link ryanmdoyle/graphql-basics-part-2---prisma-the-how-1356 %}