Skip to content

Commit 6d92dcb

Browse files
committed
Minor refactor
1 parent 0869d53 commit 6d92dcb

File tree

8 files changed

+28
-41
lines changed

8 files changed

+28
-41
lines changed

src/context.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { PrismaClient } from "@prisma/client";
2-
import { decodeAuthHeader, AuthTokenPayload } from "./utils/auth";
3-
import { Request } from "express";
2+
import { decodeAuthHeader, AuthTokenPayload } from "./utils/auth";
3+
import { Request } from "express";
44

55
export const prisma = new PrismaClient();
66

77
export interface Context {
88
prisma: PrismaClient;
9-
userId?: number;
9+
userId?: number;
1010
}
1111

12-
export const context = ({ req }: { req: Request }): Context => {
12+
export const context = ({ req }: { req: Request }): Context => {
1313
const token =
1414
req && req.headers.authorization
1515
? decodeAuthHeader(req.headers.authorization)
1616
: null;
1717

18-
return {
18+
return {
1919
prisma,
20-
userId: token?.userId,
20+
userId: token?.userId,
2121
};
22-
};
22+
};

src/graphql/Auth.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export const AuthPayload = objectType({
1717
export const AuthMutation = extendType({
1818
type: "Mutation",
1919
definition(t) {
20-
2120
t.nonNull.field("signup", {
2221
type: "AuthPayload",
2322
args: {
@@ -27,18 +26,15 @@ export const AuthMutation = extendType({
2726
},
2827
async resolve(parent, args, context) {
2928
const { email, name } = args;
30-
29+
3130
const password = await bcrypt.hash(args.password, 10);
3231

33-
3432
const user = await context.prisma.user.create({
3533
data: { email, name, password },
3634
});
3735

38-
3936
const token = jwt.sign({ userId: user.id }, APP_SECRET);
4037

41-
4238
return {
4339
token,
4440
user,
@@ -53,15 +49,13 @@ export const AuthMutation = extendType({
5349
password: nonNull(stringArg()),
5450
},
5551
async resolve(parent, args, context) {
56-
5752
const user = await context.prisma.user.findUnique({
5853
where: { email: args.email },
5954
});
6055
if (!user) {
6156
throw new Error("No such user found");
6257
}
6358

64-
6559
const valid = await bcrypt.compare(
6660
args.password,
6761
user.password,
@@ -72,12 +66,11 @@ export const AuthMutation = extendType({
7266

7367
const token = jwt.sign({ userId: user.id }, APP_SECRET);
7468

75-
7669
return {
7770
token,
7871
user,
7972
};
8073
},
8174
});
8275
},
83-
});
76+
});

src/graphql/Link.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { extendType, nonNull, objectType, stringArg } from "nexus";
2-
import { NexusGenObjects } from "../../nexus-typegen";
2+
import { NexusGenObjects } from "../../nexus-typegen";
33

44
export const Link = objectType({
55
name: "Link",
@@ -8,10 +8,8 @@ export const Link = objectType({
88
t.nonNull.string("description");
99
t.nonNull.string("url");
1010
t.field("postedBy", {
11-
1211
type: "User",
1312
resolve(parent, args, context) {
14-
1513
return context.prisma.link
1614
.findUnique({ where: { id: parent.id } })
1715
.postedBy();
@@ -26,7 +24,6 @@ export const LinkQuery = extendType({
2624
t.nonNull.list.nonNull.field("feed", {
2725
type: "Link",
2826
resolve(parent, args, context) {
29-
3027
return context.prisma.link.findMany();
3128
},
3229
});
@@ -42,24 +39,24 @@ export const LinkMutation = extendType({
4239
description: nonNull(stringArg()),
4340
url: nonNull(stringArg()),
4441
},
45-
resolve(parent, args, context) {
42+
resolve(parent, args, context) {
4643
const { description, url } = args;
4744
const { userId } = context;
4845

49-
if (!userId) {
46+
if (!userId) {
5047
throw new Error("Cannot post without logging in.");
5148
}
5249

5350
const newLink = context.prisma.link.create({
5451
data: {
5552
description,
5653
url,
57-
postedBy: { connect: { id: userId } },
54+
postedBy: { connect: { id: userId } },
5855
},
5956
});
6057

6158
return newLink;
6259
},
6360
});
6461
},
65-
});
62+
});

src/graphql/User.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ export const User = objectType({
77
t.nonNull.string("name");
88
t.nonNull.string("email");
99
t.nonNull.list.nonNull.field("links", {
10-
1110
type: "Link",
1211
resolve(parent, args, context) {
13-
14-
return context.prisma.user
12+
return context.prisma.user
1513
.findUnique({ where: { id: parent.id } })
1614
.links();
1715
},

src/graphql/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from "./Link";
1+
export * from "./Link";
22
export * from "./User";
3-
export * from "./Auth"
3+
export * from "./Auth";

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import { ApolloServer } from "apollo-server";
22

33
import { schema } from "./schema";
44

5-
import { context } from "./context";
5+
import { context } from "./context";
66

77
export const server = new ApolloServer({
88
schema,
9-
context,
10-
9+
context,
1110
});
1211

1312
const port = 3000;

src/schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { makeSchema } from "nexus";
22
import { join } from "path";
3-
import * as types from "./graphql";
3+
import * as types from "./graphql";
44

55
export const schema = makeSchema({
6-
types,
6+
types,
77
outputs: {
8-
typegen: join(__dirname, "..", "nexus-typegen.ts"),
9-
schema: join(__dirname, "..", "schema.graphql"),
8+
typegen: join(__dirname, "..", "nexus-typegen.ts"),
9+
schema: join(__dirname, "..", "schema.graphql"),
1010
},
1111
contextType: {
1212
module: join(__dirname, "./context.ts"),

src/utils/auth.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import * as jwt from "jsonwebtoken";
22

33
export const APP_SECRET = "GraphQL-is-aw3some";
44

5-
export interface AuthTokenPayload {
5+
export interface AuthTokenPayload {
66
userId: number;
77
}
88

9-
export function decodeAuthHeader(authHeader: String): AuthTokenPayload {
10-
const token = authHeader.replace("Bearer ", "");
9+
export function decodeAuthHeader(authHeader: String): AuthTokenPayload {
10+
const token = authHeader.replace("Bearer ", "");
1111

1212
if (!token) {
1313
throw new Error("No token found");
1414
}
15-
return jwt.verify(token, APP_SECRET) as AuthTokenPayload;
16-
}
15+
return jwt.verify(token, APP_SECRET) as AuthTokenPayload;
16+
}

0 commit comments

Comments
 (0)