Skip to content

Commit 8681b8a

Browse files
committed
format
1 parent e812230 commit 8681b8a

34 files changed

+361
-426
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ignore artifacts:
2+
build
3+
coverage

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,30 @@ You can learn more in the [Create React App documentation](https://facebook.gith
4545

4646
To learn React, check out the [React documentation](https://reactjs.org/).
4747

48-
## Run example
48+
## Run example
49+
50+
- Clone repo with command
4951

50-
- Clone repo with command
5152
```
5253
git clone https://github.com/linhprovip2002/React-GraphQL.git
53-
```
54+
```
55+
5456
- Entered the directory server
5557

5658
```
5759
cd React-GraphQL/server
5860
```
59-
- Run server
61+
62+
- Run server
6063

6164
```
6265
yarn install
6366
yarn start
6467
```
65-
- Back the directory client
68+
69+
- Back the directory client
70+
6671
```
6772
yarn install
6873
yarn dev
69-
```
74+
```

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"scripts": {
2525
"start": "react-scripts start",
2626
"build": "react-scripts build",
27+
"format": "prettier --write \"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}\"",
2728
"test": "react-scripts test",
2829
"eject": "react-scripts eject"
2930
},
@@ -46,6 +47,7 @@
4647
]
4748
},
4849
"devDependencies": {
50+
"prettier": "3.2.4",
4951
"tailwindcss": "^3.4.1"
5052
}
5153
}

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />

server/src/index.js

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
const { ApolloServer } = require('apollo-server');
2-
const {PubSub} = require("graphql-subscriptions");
3-
const { PrismaClient } = require('@prisma/client');
4-
const Query = require('./resolvers/Query');
5-
const Mutation = require('./resolvers/Mutation');
6-
const Subscription = require('./resolvers/Subscription');
7-
const User = require('./resolvers/User');
8-
const Link = require('./resolvers/Link');
9-
const Vote = require('./resolvers/Vote');
10-
const fs = require('fs');
11-
const path = require('path');
12-
const { getUserId } = require('./utils');
1+
const { ApolloServer } = require("apollo-server");
2+
const { PubSub } = require("graphql-subscriptions");
3+
const { PrismaClient } = require("@prisma/client");
4+
const Query = require("./resolvers/Query");
5+
const Mutation = require("./resolvers/Mutation");
6+
const Subscription = require("./resolvers/Subscription");
7+
const User = require("./resolvers/User");
8+
const Link = require("./resolvers/Link");
9+
const Vote = require("./resolvers/Vote");
10+
const fs = require("fs");
11+
const path = require("path");
12+
const { getUserId } = require("./utils");
1313

1414
const pubsub = new PubSub();
1515

1616
const prisma = new PrismaClient({
17-
errorFormat: 'minimal'
17+
errorFormat: "minimal",
1818
});
1919

2020
const resolvers = {
@@ -23,47 +23,34 @@ const resolvers = {
2323
Subscription,
2424
User,
2525
Link,
26-
Vote
26+
Vote,
2727
};
2828

2929
const server = new ApolloServer({
30-
typeDefs: fs.readFileSync(
31-
path.join(__dirname, 'schema.graphql'),
32-
'utf8'
33-
),
30+
typeDefs: fs.readFileSync(path.join(__dirname, "schema.graphql"), "utf8"),
3431
resolvers,
3532
context: ({ req }) => {
3633
return {
3734
...req,
3835
prisma,
3936
pubsub,
40-
userId:
41-
req && req.headers.authorization
42-
? getUserId(req)
43-
: null
37+
userId: req && req.headers.authorization ? getUserId(req) : null,
4438
};
4539
},
4640
subscriptions: {
4741
onConnect: (connectionParams) => {
4842
if (connectionParams.authToken) {
4943
return {
5044
prisma,
51-
userId: getUserId(
52-
null,
53-
connectionParams.authToken
54-
)
45+
userId: getUserId(null, connectionParams.authToken),
5546
};
5647
} else {
5748
return {
58-
prisma
49+
prisma,
5950
};
6051
}
61-
}
62-
}
52+
},
53+
},
6354
});
6455

65-
server
66-
.listen()
67-
.then(({ url }) =>
68-
console.log(`Server is running on ${url}`)
69-
);
56+
server.listen().then(({ url }) => console.log(`Server is running on ${url}`));

server/src/resolvers/Link.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ function postedBy(parent, args, context) {
55
}
66

77
function votes(parent, args, context) {
8-
return context.prisma.link
9-
.findUnique({ where: { id: parent.id } })
10-
.votes();
8+
return context.prisma.link.findUnique({ where: { id: parent.id } }).votes();
119
}
1210

1311
module.exports = {
1412
postedBy,
15-
votes
13+
votes,
1614
};

server/src/resolvers/Mutation.js

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
const bcrypt = require('bcryptjs');
2-
const jwt = require('jsonwebtoken');
3-
const { APP_SECRET } = require('../utils');
1+
const bcrypt = require("bcryptjs");
2+
const jwt = require("jsonwebtoken");
3+
const { APP_SECRET } = require("../utils");
44

55
async function post(parent, args, context, info) {
66
const { userId } = context;
77

8-
let postedBy = undefined
8+
let postedBy = undefined;
99
if (userId) {
10-
postedBy = { connect: { id: userId } }
10+
postedBy = { connect: { id: userId } };
1111
}
1212

1313
const newLink = await context.prisma.link.create({
1414
data: {
1515
url: args.url,
1616
description: args.description,
17-
postedBy
18-
}
17+
postedBy,
18+
},
1919
});
2020

21-
context.pubsub.publish('NEW_LINK', newLink);
21+
context.pubsub.publish("NEW_LINK", newLink);
2222

2323
return newLink;
2424
}
@@ -28,20 +28,20 @@ async function signup(parent, args, context, info) {
2828
const password = await bcrypt.hash(args.password, 10);
2929
// check user with email already exists
3030
const userExists = await context.prisma.user.findUnique({
31-
where: { email: args.email }
31+
where: { email: args.email },
3232
});
3333
if (userExists) {
34-
throw new Error('User already exists');
34+
throw new Error("User already exists");
3535
}
3636
const user = await context.prisma.user.create({
37-
data: { ...args, password }
37+
data: { ...args, password },
3838
});
3939

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

4242
return {
4343
token,
44-
user
44+
user,
4545
};
4646
} catch (error) {
4747
throw new Error(`Signup failed: ${error.message}`);
@@ -50,28 +50,27 @@ async function signup(parent, args, context, info) {
5050

5151
async function login(parent, args, context, info) {
5252
const user = await context.prisma.user.findUnique({
53-
where: { email: args.email }
53+
where: { email: args.email },
5454
});
5555

5656
if (!user) {
57-
throw new Error('No such user found');
57+
throw new Error("No such user found");
5858
}
5959

6060
const valid = await bcrypt.compare(args.password, user.password);
6161

6262
if (!valid) {
63-
throw new Error('Invalid password');
63+
throw new Error("Invalid password");
6464
}
6565

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

6868
return {
6969
token,
70-
user
70+
user,
7171
};
7272
}
7373

74-
7574
async function vote(parent, args, context, info) {
7675
try {
7776
const { userId } = context;
@@ -80,39 +79,39 @@ async function vote(parent, args, context, info) {
8079
where: {
8180
linkId_userId: {
8281
linkId: args.linkId,
83-
userId: userId
84-
}
85-
}
82+
userId: userId,
83+
},
84+
},
8685
});
87-
86+
8887
if (vote) {
8988
// User has already voted, remove the vote (unvote)
9089
await context.prisma.vote.delete({
9190
where: {
92-
id: vote.id
93-
}
91+
id: vote.id,
92+
},
9493
});
95-
94+
9695
// Inform the client that the vote has been removed
97-
context.pubsub.publish('VOTE_REMOVED', { voteRemoved: vote });
98-
96+
context.pubsub.publish("VOTE_REMOVED", { voteRemoved: vote });
97+
9998
// You can return some information or null indicating success
10099
return { message: `Vote removed for link: ${args.linkId}` };
101100
}
102-
101+
103102
const newVote = await context.prisma.vote.create({
104103
data: {
105104
user: { connect: { id: userId } },
106-
link: { connect: { id: args.linkId } }
107-
}
105+
link: { connect: { id: args.linkId } },
106+
},
108107
});
109-
108+
110109
// Publish the new vote with the correct payload
111-
context.pubsub.publish('NEW_VOTE', { newVote });
112-
113-
return {message: `Vote added for link: ${args.linkId}`};
110+
context.pubsub.publish("NEW_VOTE", { newVote });
111+
112+
return { message: `Vote added for link: ${args.linkId}` };
114113
} catch (error) {
115-
return { message : "Error voting"}
114+
return { message: "Error voting" };
116115
}
117116
}
118117

@@ -123,19 +122,17 @@ async function deleteLink(parent, args, context, info) {
123122
where: { id },
124123
});
125124
console.log("Link deleted successfully");
126-
return {message: "Link deleted successfully"}
125+
return { message: "Link deleted successfully" };
127126
} catch (error) {
128127
console.log(error);
129-
return {error:"Error deleting link"}
128+
return { error: "Error deleting link" };
130129
}
131130
}
132131

133-
134132
module.exports = {
135133
post,
136134
signup,
137135
login,
138136
vote,
139-
deleteLink
137+
deleteLink,
140138
};
141-

server/src/resolvers/Query.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ async function feed(parent, args, context, info) {
33
? {
44
OR: [
55
{ description: { contains: args.filter } },
6-
{ url: { contains: args.filter } }
7-
]
6+
{ url: { contains: args.filter } },
7+
],
88
}
99
: {};
1010

1111
const links = await context.prisma.link.findMany({
1212
where,
1313
skip: args.skip,
1414
take: args.take,
15-
orderBy: args.orderBy
15+
orderBy: args.orderBy,
1616
});
1717

1818
const count = await context.prisma.link.count({ where });
1919

2020
return {
21-
id: 'main-feed',
21+
id: "main-feed",
2222
links,
23-
count
23+
count,
2424
};
2525
}
2626

2727
module.exports = {
28-
feed
28+
feed,
2929
};

0 commit comments

Comments
 (0)