generated from include-davis/Apollo-Starter
-
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 #18 from include-davis/7-api-product
Feat/API-Product
- Loading branch information
Showing
8 changed files
with
169 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- CreateTable | ||
CREATE TABLE "Products" ( | ||
"id" TEXT NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"price" INTEGER NOT NULL, | ||
"description" TEXT NOT NULL, | ||
"details" TEXT NOT NULL, | ||
"weight" INTEGER NOT NULL, | ||
"height" INTEGER NOT NULL, | ||
"width" INTEGER NOT NULL, | ||
"depth" INTEGER NOT NULL, | ||
"special_label_needed" BOOLEAN NOT NULL, | ||
|
||
CONSTRAINT "Products_pkey" PRIMARY KEY ("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,12 @@ | ||
model Products { | ||
id String @id @default(uuid()) | ||
name String | ||
price Int | ||
description String | ||
details String | ||
weight Int | ||
height Int | ||
width Int | ||
depth Int | ||
special_label_needed Boolean | ||
} |
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,15 @@ | ||
import Products from "../services/Products"; | ||
|
||
const resolvers = { | ||
Query: { | ||
products: (_, { id }) => Products.find({ id }), | ||
manyProducts: (_, { ids }) => Products.findAll({ ids }), | ||
}, | ||
Mutation: { | ||
createProducts: (_, { input }) => Products.create({ input }), | ||
updateProducts: (_, { id, input }) => Products.update({ id, input }), | ||
deleteProducts: (_, { id }) => Products.delete({ id }), | ||
}, | ||
}; | ||
|
||
export default resolvers; |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import User from "./User"; | ||
import Playlist from "./Playlist"; | ||
import Song from "./Song"; | ||
import Products from "./Products"; | ||
|
||
export default [User, Playlist, Song]; | ||
export default [User, Playlist, Products, Song]; |
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,58 @@ | ||
import prisma from "../prisma/client"; | ||
|
||
export default class Products { | ||
// CREATE | ||
static async create({ input }) { | ||
const products = await prisma.products.create({ | ||
data: input, | ||
}); | ||
return products; | ||
} | ||
|
||
// READ | ||
static async find({ id }) { | ||
return prisma.products.findUnique({ where: { id } }); | ||
} | ||
|
||
static async findAll({ ids }) { | ||
if (!ids) { | ||
return prisma.products.findMany(); | ||
} | ||
return prisma.products.findMany({ | ||
where: { | ||
id: { | ||
in: ids, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
// UPDATE | ||
static async update({ id, input }) { | ||
try { | ||
const user = await prisma.user.update({ | ||
where: { | ||
id, | ||
}, | ||
data: input, | ||
}); | ||
return user; | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
// DELETE | ||
static async delete({ id }) { | ||
try { | ||
await prisma.products.delete({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
} |
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,52 @@ | ||
import gql from "graphql-tag"; | ||
|
||
const typeDefs = gql` | ||
type Products { | ||
id: ID! | ||
name: String! | ||
price: Int! | ||
description: String! | ||
details: String! | ||
weight: Int! | ||
height: Int! | ||
width: Int! | ||
depth: Int! | ||
special_label_needed: Boolean! | ||
} | ||
input ProductsInput { | ||
name: String! | ||
price: Int! | ||
description: String! | ||
details: String! | ||
weight: Int! | ||
height: Int! | ||
width: Int! | ||
depth: Int! | ||
special_label_needed: Boolean! | ||
} | ||
input ProductsInputOptional { | ||
name: String | ||
price: Int | ||
description: String | ||
details: String | ||
weight: Int | ||
height: Int | ||
width: Int | ||
depth: Int | ||
special_label_needed: Boolean | ||
} | ||
type Query { | ||
products(id: ID!): Products | ||
manyProducts(id: [ID]): [Products] | ||
} | ||
type Mutation { | ||
createProducts(input: ProductsInput!): Products | ||
updateProducts(id: ID!, input: ProductsInputOptional!): Products | ||
deleteProducts(id: ID!): Boolean | ||
} | ||
`; | ||
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import User from "./User.js"; | ||
import Playlist from "./Playlist.js"; | ||
import Song from "./Song.js"; | ||
import Products from "./Products.js"; | ||
|
||
export default [User, Playlist, Song]; | ||
export default [User, Playlist, Song, Products]; |