From ebb84f1f01396cf75e7fb70bb0e2987bbc0b14b2 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Fri, 10 Aug 2018 13:14:27 -0400 Subject: 30 --- stepped-solutions/27/backend/src/index.js | 33 ++++++++++++++++++++++ .../27/backend/src/resolvers/Query.js | 21 ++++++++++++++ stepped-solutions/27/backend/src/schema.graphql | 15 ++++++++++ 3 files changed, 69 insertions(+) create mode 100755 stepped-solutions/27/backend/src/index.js create mode 100755 stepped-solutions/27/backend/src/resolvers/Query.js create mode 100755 stepped-solutions/27/backend/src/schema.graphql (limited to 'stepped-solutions/27/backend/src') diff --git a/stepped-solutions/27/backend/src/index.js b/stepped-solutions/27/backend/src/index.js new file mode 100755 index 0000000..d95ec58 --- /dev/null +++ b/stepped-solutions/27/backend/src/index.js @@ -0,0 +1,33 @@ +const cookieParser = require('cookie-parser'); +const jwt = require('jsonwebtoken'); + +require('dotenv').config({ path: 'variables.env' }); +const createServer = require('./createServer'); +const db = require('./db'); + +const server = createServer(); + +server.express.use(cookieParser()); + +// decode the JWT so we can get the user Id on each request +server.express.use((req, res, next) => { + const { token } = req.cookies; + if (token) { + const { userId } = jwt.verify(token, process.env.APP_SECRET); + // put the userId onto the req for future requests to access + req.userId = userId; + } + next(); +}); + +server.start( + { + cors: { + credentials: true, + origin: process.env.FRONTEND_URL, + }, + }, + deets => { + console.log(`Server is now running on port http://localhost:${deets.port}`); + } +); diff --git a/stepped-solutions/27/backend/src/resolvers/Query.js b/stepped-solutions/27/backend/src/resolvers/Query.js new file mode 100755 index 0000000..8055122 --- /dev/null +++ b/stepped-solutions/27/backend/src/resolvers/Query.js @@ -0,0 +1,21 @@ +const { forwardTo } = require('prisma-binding'); + +const Query = { + items: forwardTo('db'), + item: forwardTo('db'), + itemsConnection: forwardTo('db'), + me(parent, args, ctx, info) { + // check if there is a current user ID + if (!ctx.request.userId) { + return null; + } + return ctx.db.query.user( + { + where: { id: ctx.request.userId }, + }, + info + ); + }, +}; + +module.exports = Query; diff --git a/stepped-solutions/27/backend/src/schema.graphql b/stepped-solutions/27/backend/src/schema.graphql new file mode 100755 index 0000000..f2cc283 --- /dev/null +++ b/stepped-solutions/27/backend/src/schema.graphql @@ -0,0 +1,15 @@ +# import * from './generated/prisma.graphql' + +type Mutation { + createItem(title: String, description: String, price: Int, image: String, largeImage: String): Item! + updateItem(id: ID!, title: String, description: String, price: Int): Item! + deleteItem(id: ID!): Item + signup(email: String!, password: String!, name: String!): User! +} + +type Query { + items(where: ItemWhereInput, orderBy: ItemOrderByInput, skip: Int, first: Int): [Item]! + item(where: ItemWhereUniqueInput!): Item + itemsConnection(where: ItemWhereInput): ItemConnection! + me: User +} -- cgit v1.3