From 2ebb56de4f136d7be55b830fe214af63ba65de80 Mon Sep 17 00:00:00 2001 From: Ryan Chenkie Date: Mon, 5 Oct 2020 16:59:19 -0400 Subject: [PATCH] add new prisma model --- package-lock.json | 73 ++++++++++++++++++++++++++++++++++ server/package.json | 8 ++-- server/prisma/datamodel.prisma | 23 ----------- server/prisma/dev.db | 0 server/prisma/prisma.yml | 15 ------- server/prisma/schema.prisma | 38 ++++++++++++++++++ server/src/index.js | 38 +++++++----------- server/src/resolvers/index.js | 6 +++ 8 files changed, 136 insertions(+), 65 deletions(-) create mode 100644 package-lock.json delete mode 100644 server/prisma/datamodel.prisma create mode 100644 server/prisma/dev.db delete mode 100644 server/prisma/prisma.yml create mode 100644 server/prisma/schema.prisma create mode 100644 server/src/resolvers/index.js diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..3b77f2e --- /dev/null +++ b/package-lock.json @@ -0,0 +1,73 @@ +{ + "name": "hackernews-react-apollo", + "version": "0.1.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@prisma/cli": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/@prisma/cli/-/cli-2.8.0.tgz", + "integrity": "sha512-Kg1C47d75jdEIMmJif8TMlv/2Ihx08E1qWp0euwoZhjd807HGnjgC9tJYjTfkdf+NMJSAUbvoPXKInEX0HoOMw==", + "dev": true + }, + "@prisma/client": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-2.8.0.tgz", + "integrity": "sha512-5+GzRTkPnmv4OEV2tB8kwQt/xLLxBR/daJBcMt6pnnonJvrREsu0tSTdz2LJNPaj3kTT0fSS/OaeGMMdfVYSpw==", + "requires": { + "pkg-up": "^3.1.0" + } + }, + "find-up": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", + "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", + "requires": { + "locate-path": "^3.0.0" + } + }, + "locate-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", + "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", + "requires": { + "p-locate": "^3.0.0", + "path-exists": "^3.0.0" + } + }, + "p-limit": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", + "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==", + "requires": { + "p-try": "^2.0.0" + } + }, + "p-locate": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", + "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", + "requires": { + "p-limit": "^2.0.0" + } + }, + "p-try": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", + "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==" + }, + "path-exists": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" + }, + "pkg-up": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/pkg-up/-/pkg-up-3.1.0.tgz", + "integrity": "sha512-nDywThFk1i4BQK4twPQ6TA4RT8bDY96yeuCVBWL3ePARCiEKDRSrNGbFIgUJpLp+XeIR65v8ra7WuJOFUBtkMA==", + "requires": { + "find-up": "^3.0.0" + } + } + } +} diff --git a/server/package.json b/server/package.json index 56a7184..f2738df 100644 --- a/server/package.json +++ b/server/package.json @@ -7,12 +7,12 @@ "prisma": "prisma" }, "dependencies": { + "@prisma/client": "^2.8.0", + "apollo-server": "^2.18.2", "bcryptjs": "^2.4.3", - "graphql-yoga": "^1.7.0", - "jsonwebtoken": "^8.2.0", - "prisma-client-lib": "^1.31.0" + "jsonwebtoken": "^8.2.0" }, "devDependencies": { - "prisma": "^1.31.0" + "@prisma/cli": "^2.8.0" } } diff --git a/server/prisma/datamodel.prisma b/server/prisma/datamodel.prisma deleted file mode 100644 index 8ae94ee..0000000 --- a/server/prisma/datamodel.prisma +++ /dev/null @@ -1,23 +0,0 @@ -type Link { - id: ID! @id - createdAt: DateTime! @createdAt - description: String! - url: String! - postedBy: User - votes: [Vote!]! -} - -type User { - id: ID! @id - name: String! - email: String! @unique - password: String! - links: [Link!]! - votes: [Vote!]! -} - -type Vote { - id: ID! @id - link: Link! - user: User! -} diff --git a/server/prisma/dev.db b/server/prisma/dev.db new file mode 100644 index 0000000..e69de29 diff --git a/server/prisma/prisma.yml b/server/prisma/prisma.yml deleted file mode 100644 index 4f93df8..0000000 --- a/server/prisma/prisma.yml +++ /dev/null @@ -1,15 +0,0 @@ -# Specifies the HTTP endpoint of your Prisma API. -endpoint: '' - -# Defines your models, each model is mapped to the database as a table. -datamodel: datamodel.prisma - -# Specifies the language and directory for the generated Prisma client. -generate: - - generator: javascript-client - output: ../src/generated/prisma-client - -# Ensures Prisma client is re-generated after a datamodel change. -hooks: - post-deploy: - - prisma generate \ No newline at end of file diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma new file mode 100644 index 0000000..314be15 --- /dev/null +++ b/server/prisma/schema.prisma @@ -0,0 +1,38 @@ +// This is your Prisma schema file, +// learn more about it in the docs: https://pris.ly/d/prisma-schema + +datasource db { + provider = "sqlite" + url = "file:./dev.db" +} + +generator client { + provider = "prisma-client-js" +} + +model Link { + id String @id @default(cuid()) + description String + url String + createdAt DateTime @default(now()) + postedBy User @relation(fields: [userId], references: [id]) + votes Vote[] + userId String +} + +model User { + id String @id @default(cuid()) + name String + email String + password String + links Link[] + votes Vote[] +} + +model Vote { + id String @id @default(cuid()) + link Link @relation(fields: [linkId], references: [id]) + user User @relation(fields: [userId], references: [id]) + linkId String + userId String +} diff --git a/server/src/index.js b/server/src/index.js index b2ec0bd..9302d13 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -1,27 +1,19 @@ -const { GraphQLServer } = require('graphql-yoga') -const { prisma } = require('./generated/prisma-client') -const Query = require('./resolvers/Query') -const Mutation = require('./resolvers/Mutation') -const Subscription = require('./resolvers/Subscription') -const User = require('./resolvers/User') -const Link = require('./resolvers/Link') -const Vote = require('./resolvers/Vote') +import { ApolloServer } from 'apollo-server'; +import { PrismaClient } from '@prisma/client'; +import * as resolvers from './resolvers'; -const resolvers = { - Query, - Mutation, - Subscription, - User, - Link, - Vote, -} +const prisma = new PrismaClient(); -const server = new GraphQLServer({ +const server = new ApolloServer({ typeDefs: './src/schema.graphql', resolvers, - context: request => ({ - ...request, - prisma, - }), -}) -server.start(() => console.log(`Server is running on http://localhost:4000`)) + context: () => ({ + prisma + }) +}); + +server.listen().then(({ port }) => { + console.log( + `Server listening on http://localhost:${port}` + ); +}); diff --git a/server/src/resolvers/index.js b/server/src/resolvers/index.js new file mode 100644 index 0000000..7557d48 --- /dev/null +++ b/server/src/resolvers/index.js @@ -0,0 +1,6 @@ +export * as Link from './Link'; +export * as Mutation from './Mutation'; +export * as Query from './Query'; +export * as Subscription from './Subscription'; +export * as User from './User'; +export * as Vote from './Vote';