This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from NicolasOmar/lesson/data-storage-with-prisma
Lesson #2 | Data handling using ith prisma
- Loading branch information
Showing
17 changed files
with
4,623 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-env" | ||
], | ||
"plugins": [ | ||
"@babel/plugin-transform-runtime" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
# Keep environment variables out of version control | ||
.env |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "graphql-practice-prisma", | ||
"version": "1.0.0", | ||
"author": "Nicolás Omar González Passerino", | ||
"license": "MIT", | ||
"private": false, | ||
"scripts": { | ||
"start": "nodemon src/index.js --ext js,graphql --exec babel-node", | ||
"setup": "npm i -g prisma && npm i && npm run generate-schema", | ||
"test": "jest", | ||
"generate-schema": "prisma db pull && prisma generate", | ||
"sync-schema": "prisma db push --accept-data-loss --schema=./src/prisma/schema.prisma" | ||
}, | ||
"dependencies": { | ||
"@babel/cli": "^7.16.8", | ||
"@babel/core": "^7.16.10", | ||
"@babel/node": "^7.16.8", | ||
"@babel/plugin-transform-runtime": "^7.16.10", | ||
"@babel/preset-env": "^7.16.11", | ||
"@prisma/client": "^3.8.1", | ||
"apollo-server": "^3.6.2", | ||
"bcrypt": "^5.0.1", | ||
"graphql": "^16.2.0", | ||
"jsonwebtoken": "^8.5.1", | ||
"prisma": "^3.8.1", | ||
"uuid": "^8.3.2" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "^2.0.15" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import bcrypt from 'bcrypt' | ||
// UTILS | ||
import { authUser } from '../utils/auth' | ||
import { prismaCreate, prismaDelete } from '../utils/prisma' | ||
|
||
const Mutation = { | ||
createUser: async (_, { userData }, { prisma }) => { | ||
const password = (await bcrypt.hash(userData.password, 5)) | ||
return await prismaCreate('user', { ...userData, password }, prisma) | ||
}, | ||
createBook: async (_, { bookData }, { prisma, request }) => authUser(request) && await prismaCreate('book', bookData, prisma), | ||
createReview: async(_, { reviewData }, { prisma, request }) => authUser(request) && await prismaCreate('review', reviewData, prisma), | ||
deleteUser: async (_, { id }, { prisma, request }) => authUser(request) && await prismaDelete('user', id, prisma), | ||
deleteBook: async (_, { id }, { prisma, request }) => authUser(request) && await prismaDelete('book', id, prisma), | ||
deleteReview: async(_, { id }, { prisma, request }) => authUser(request) && await prismaDelete('review', id, prisma) | ||
} | ||
|
||
export default Mutation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import bcrypt from 'bcrypt' | ||
// UTILS | ||
import { authUser } from '../utils/auth' | ||
import { prismaRead } from '../utils/prisma' | ||
|
||
const Queries = { | ||
getAllUsers: async (_, { query }, { prisma, request }) => authUser(request) && await prismaRead({ prisma, entity: 'user', ...query }), | ||
getAllBooks: async (_, { query }, { prisma, request }) => authUser(request) && await prismaRead({ prisma, entity: 'book', ...query }), | ||
getAllReviews: async (_, { query }, { prisma, request }) => authUser(request) && await prismaRead({ prisma, entity: 'review', ...query }), | ||
getUserById: async (_, { id }, { prisma, request }) => authUser(request) && await prismaRead({ prisma, entity: 'user', id }), | ||
getBookById: async (_, { id }, { prisma, request }) => authUser(request) && await prismaRead({ prisma, entity: 'book', id }), | ||
getReviewById: async (_, { id }, { prisma, request }) => authUser(request) && await prismaRead({ prisma, entity: 'review', id }), | ||
loginUser: async (_, { userData }, { prisma }) => { | ||
const user = await prisma.user.findUnique({ where: { username: userData.username }}) | ||
return (await bcrypt.compare(userData.password, user.password) ? user : null) | ||
} | ||
} | ||
|
||
export default Queries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { authUser } from "../utils/auth" | ||
|
||
const Relationships = { | ||
User: { | ||
password: ({ password }, __, { request }) => authUser(request) ? password : null, | ||
reviews: async ({ id }, _, { prisma }) => await prisma.review.findMany({ where: { userId: id }}) | ||
}, | ||
Book: { | ||
reviews: async ({ id }, _, { prisma }) => await prisma.review.findMany({ where: { bookId: id } }) | ||
}, | ||
Review: { | ||
author: async ({ userId }, _, { prisma }) => await prisma.user.findUnique({ where: { id: userId}}), | ||
book: async ({ bookId }, _, { prisma }) => await prisma.book.findUnique({ where: { id: bookId}}) | ||
} | ||
} | ||
|
||
export default Relationships |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { gql } from "apollo-server"; | ||
|
||
const typeDefs = gql` | ||
type Query { | ||
getAllUsers(query: queryInput): [User]! | ||
getAllBooks(query: queryInput): [Book]! | ||
getAllReviews(query: queryInput): [Review]! | ||
getUserById(id: String!): User! | ||
getBookById(id: String!): Book! | ||
getReviewById(id: String!): Review! | ||
loginUser(userData: CreateUserInput!): User! | ||
} | ||
type Mutation { | ||
createUser(userData: CreateUserInput!): User! | ||
createBook(bookData: CreateBookInput!): Book! | ||
createReview(reviewData: CreateReviewInput!): Review! | ||
deleteUser(id: String!): User! | ||
deleteBook(id: String!): Book! | ||
deleteReview(id: String!): Review! | ||
} | ||
type User { | ||
id: String! | ||
username: String! | ||
password: String! | ||
reviews: [Review]! | ||
} | ||
type Book { | ||
id: String! | ||
isbn: String | ||
title: String | ||
author: String! | ||
reviews: [Review]! | ||
} | ||
type Review { | ||
id: String! | ||
text: String | ||
rating: Int! | ||
userId: String! | ||
bookId: String! | ||
author: User! | ||
book: Book! | ||
} | ||
input queryInput { | ||
page: Int | ||
rows: Int | ||
orderBy: String | ||
order: String | ||
} | ||
input CreateUserInput { | ||
username: String! | ||
password: String! | ||
} | ||
input CreateBookInput { | ||
isbn: String | ||
title: String | ||
author: String! | ||
} | ||
input CreateReviewInput { | ||
text: String | ||
rating: Int! | ||
userId: String! | ||
bookId: String! | ||
} | ||
` | ||
|
||
export default typeDefs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import jwt from 'jsonwebtoken' | ||
import { ApolloError } from 'apollo-server' | ||
|
||
export const authUser = (request) => { | ||
const token = request.headers['auth'] | ||
? request.headers['auth'].replace('Bearer ', '') | ||
: null | ||
|
||
if (!token || !(jwt.verify(token, process.env.JWT_SECRET))) throw new ApolloError('Unauthenticated user') | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { v4 as uuid } from 'uuid' | ||
|
||
const constructConfig = ({ id = null, page = 1, rows = 1, orderBy = 'id', order = 'desc' }) => ( | ||
{ | ||
unique: { | ||
fn: 'findUnique', | ||
config: { | ||
where: { id } | ||
} | ||
}, | ||
all: { | ||
fn: 'findMany', | ||
config: { | ||
take: rows, | ||
skip: page === 1 ? 0 : ((page - 1) * rows), | ||
orderBy: { [orderBy]: order } | ||
} | ||
} | ||
} | ||
) | ||
|
||
export const prismaRead = ({ prisma, entity, id = null, page = 1, rows = 1, orderBy = 'id', order = 'desc' }) => { | ||
const { unique, all } = constructConfig({ id, page, rows, orderBy, order }) | ||
const { fn, config } = id ? unique : all | ||
|
||
return prisma[entity][fn]({ ...config }) | ||
} | ||
|
||
export const prismaCreate = (entity, payload, prisma) => prisma[entity].prismaCreate({ data: { ...payload, id: uuid() } }) | ||
|
||
export const prismaDelete = (entity, id, prisma) => prisma[entity].delete({ where: { id } }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ApolloServer } from 'apollo-server' | ||
import { PrismaClient } from '@prisma/client' | ||
// SCHEMA | ||
import typeDefs from './graphql/typeDefs' | ||
// RESOLVERS | ||
import Query from './graphql/resolvers/queries' | ||
import Mutation from './graphql/resolvers/mutations' | ||
import Relationships from './graphql/resolvers/relationships' | ||
|
||
const prisma = new PrismaClient() | ||
|
||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers: { | ||
Query, | ||
Mutation, | ||
...Relationships | ||
}, | ||
context: async ({ req }) => ( | ||
{ prisma, request: req } | ||
) | ||
}) | ||
|
||
server.listen().then(async ({ url }) => console.log(`Welcome to the page ${url}`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model User { | ||
id String @id @db.Uuid @unique | ||
username String @db.VarChar @unique | ||
password String | ||
reviews Review[] | ||
} | ||
|
||
model Book { | ||
id String @id @db.Uuid @unique | ||
isbn String @db.VarChar | ||
title String @db.VarChar | ||
author String @db.VarChar | ||
reviews Review[] | ||
} | ||
|
||
model Review { | ||
id String @id @db.Uuid @unique | ||
text String @db.VarChar | ||
rating Int @db.Integer | ||
userId String @db.Uuid | ||
bookId String @db.Uuid | ||
author User @relation(fields: [userId], references: [id], onDelete: Cascade) | ||
book Book @relation(fields: [bookId], references: [id], onDelete: Cascade) | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.