Skip to content

Commit aa78517

Browse files
committed
updates
1 parent 077e4f0 commit aa78517

File tree

13 files changed

+159
-292
lines changed

13 files changed

+159
-292
lines changed

server/prisma/dev.db

0 Bytes
Binary file not shown.

server/src/index.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,35 @@ const prisma = new PrismaClient();
99
const server = new ApolloServer({
1010
typeDefs,
1111
resolvers,
12-
context: ({ req }) => ({
13-
prisma,
14-
userId: req.headers.authorization
15-
? getUserId(req)
16-
: null
17-
})
12+
context: async ({ req, connection }) => {
13+
if (connection) {
14+
return connection.context;
15+
} else {
16+
return {
17+
prisma,
18+
userId: req.headers.authorization
19+
? getUserId(req)
20+
: null
21+
};
22+
}
23+
},
24+
subscriptions: {
25+
onConnect: (connectionParams) => {
26+
if (connectionParams.authToken) {
27+
return {
28+
prisma,
29+
userId: getUserId(
30+
null,
31+
connectionParams.authToken
32+
)
33+
};
34+
} else {
35+
return {
36+
prisma
37+
};
38+
}
39+
}
40+
}
1841
});
1942

2043
server.listen(process.env.PORT || 4000).then(({ port }) => {

server/src/pubsub.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { PubSub } = require('apollo-server');
2+
3+
const pubsub = new PubSub();
4+
5+
module.exports = { pubsub };

server/src/resolvers/Link.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const postedBy = async (parent, args, context) => {
99
where: { id: link.userId }
1010
});
1111
} catch (err) {
12+
console.log('the err', err);
1213
throw new ApolloError(err);
1314
}
1415
};

server/src/resolvers/Mutation.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,38 @@ const {
55
ApolloError,
66
AuthenticationError
77
} = require('apollo-server');
8+
const { pubsub } = require('./../pubsub');
89

910
const createPost = async (
1011
parent,
1112
{ url, description },
1213
context
1314
) => {
1415
try {
16+
let data = {};
1517
if (!context.userId) {
16-
return await context.prisma.link.create({
17-
data: {
18-
url,
19-
description
20-
}
21-
});
22-
}
23-
return await context.prisma.link.create({
24-
data: {
18+
data = {
19+
url,
20+
description
21+
};
22+
} else {
23+
data = {
2524
url,
2625
description,
2726
postedBy: {
2827
connect: {
2928
id: context.userId
3029
}
3130
}
32-
}
31+
};
32+
}
33+
const newLink = await context.prisma.link.create({
34+
data
35+
});
36+
pubsub.publish('POST_CREATED', {
37+
newLink
3338
});
39+
return newLink;
3440
} catch (err) {
3541
throw new ApolloError(err);
3642
}
@@ -116,7 +122,7 @@ const vote = async (parent, args, context) => {
116122
);
117123
}
118124

119-
return await context.prisma.vote.create({
125+
const newVote = await context.prisma.vote.create({
120126
data: {
121127
link: {
122128
connect: {
@@ -130,6 +136,12 @@ const vote = async (parent, args, context) => {
130136
}
131137
}
132138
});
139+
140+
pubsub.publish('VOTE', {
141+
newVote
142+
});
143+
144+
return newVote;
133145
} catch (err) {
134146
throw new ApolloError(err);
135147
}

server/src/resolvers/Subscription.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,10 @@
1-
const { PubSub } = require('apollo-server');
2-
3-
const pubsub = new PubSub();
4-
5-
function newLinkSubscribe(parent, args, context, info) {
6-
return context.prisma.$subscribe
7-
.link({ mutation_in: ['CREATED'] })
8-
.node();
9-
}
1+
const { pubsub } = require('./../pubsub');
102

113
const newLink = {
12-
subscribe: () => pubsub.asyncIterator(['CREATED']),
13-
resolve: (payload) => {
14-
return payload;
15-
}
4+
subscribe: () => pubsub.asyncIterator(['POST_CREATED'])
165
};
17-
18-
function newVoteSubscribe(parent, args, context, info) {
19-
return context.prisma.$subscribe
20-
.vote({ mutation_in: ['CREATED'] })
21-
.node();
22-
}
23-
246
const newVote = {
25-
subscribe: newVoteSubscribe,
26-
resolve: (payload) => {
27-
return payload;
28-
}
7+
subscribe: () => pubsub.asyncIterator(['VOTE'])
298
};
309

3110
module.exports = {

server/src/resolvers/Vote.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const { ApolloError } = require('apollo-server');
22

33
const link = async (parent, args, context) => {
44
try {
5-
return context.prisma.link.findMany({
5+
return await context.prisma.link.findOne({
66
where: {
7-
userId: parent.userId
7+
id: parent.linkId
88
}
99
});
1010
} catch (err) {

server/src/utils.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
const jwt = require('jsonwebtoken');
22
const APP_SECRET = 'GraphQL-is-aw3some';
33

4-
function getUserId(req) {
4+
function getTokenPayload(token) {
5+
return jwt.verify(token, APP_SECRET);
6+
}
7+
8+
function getUserId(req, authToken) {
9+
if (authToken) {
10+
const { userId } = getTokenPayload(authToken);
11+
return userId;
12+
}
13+
514
const authHeader = req.headers.authorization;
615
if (authHeader) {
716
const token = authHeader.replace('Bearer ', '');
8-
const { userId } = jwt.verify(token, APP_SECRET);
17+
if (!token) {
18+
throw new Error('No token found');
19+
}
20+
const { userId } = getTokenPayload(token);
921
return userId;
1022
}
1123

src/components/CreateLink.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React, { Component, useState } from 'react';
2-
import { FEED_QUERY } from './LinkList';
3-
import { LINKS_PER_PAGE } from '../constants';
4-
import { useMutation, gql } from '@apollo/client';
1+
import React, { useState } from 'react';
2+
import { gql, useMutation } from '@apollo/client';
53
import { useHistory } from 'react-router';
4+
import { LINKS_PER_PAGE } from '../constants';
5+
import { FEED_QUERY } from './LinkList';
66

77
const CREATE_LINK_MUTATION = gql`
88
mutation PostMutation(

src/components/Link.js

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React from 'react';
2-
import { FEED_QUERY } from './LinkList';
3-
import { AUTH_TOKEN } from '../constants';
2+
import { gql, useMutation } from '@apollo/client';
3+
import { AUTH_TOKEN, LINKS_PER_PAGE } from '../constants';
44
import { timeDifferenceForDate } from '../utils';
5-
6-
import { useMutation, gql } from '@apollo/client';
5+
import { FEED_QUERY } from './LinkList';
76

87
const VOTE_MUTATION = gql`
98
mutation VoteMutation($linkId: ID!) {
@@ -27,38 +26,50 @@ const VOTE_MUTATION = gql`
2726
const Link = (props) => {
2827
const { link } = props;
2928
const authToken = localStorage.getItem(AUTH_TOKEN);
30-
const [vote, { loading, error, data }] = useMutation(
31-
VOTE_MUTATION,
32-
{
33-
variables: {
34-
linkId: link.id
35-
},
36-
update(cache, { data: { vote } }) {
37-
const { feed } = cache.readQuery({
38-
query: FEED_QUERY
39-
});
4029

41-
const updatedLinks = feed.links.map((feedLink) => {
42-
if (feedLink.id === link.id) {
43-
return {
44-
...feedLink,
45-
votes: [...feedLink.votes, vote]
46-
};
47-
}
48-
return feedLink;
49-
});
30+
const take = LINKS_PER_PAGE;
31+
const skip = 0;
32+
const orderBy = 'createdAt_DESC';
33+
34+
const [vote] = useMutation(VOTE_MUTATION, {
35+
variables: {
36+
linkId: link.id
37+
},
38+
update(cache, { data: { vote } }) {
39+
const { feed } = cache.readQuery({
40+
query: FEED_QUERY,
41+
variables: {
42+
take,
43+
skip,
44+
orderBy
45+
}
46+
});
5047

51-
cache.writeQuery({
52-
query: FEED_QUERY,
53-
data: {
54-
feed: {
55-
links: updatedLinks
56-
}
48+
const updatedLinks = feed.links.map((feedLink) => {
49+
if (feedLink.id === link.id) {
50+
return {
51+
...feedLink,
52+
votes: [...feedLink.votes, vote]
53+
};
54+
}
55+
return feedLink;
56+
});
57+
58+
cache.writeQuery({
59+
query: FEED_QUERY,
60+
data: {
61+
feed: {
62+
links: updatedLinks
5763
}
58-
});
59-
}
64+
},
65+
variables: {
66+
take,
67+
skip,
68+
orderBy
69+
}
70+
});
6071
}
61-
);
72+
});
6273
return (
6374
<div className="flex mt2 items-start">
6475
<div className="flex items-center">

0 commit comments

Comments
 (0)