Skip to content

Commit 59ee3b1

Browse files
committed
refact: Move everythin to GraphQL, remove redux
1 parent b3f8dd8 commit 59ee3b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+11258
-1117
lines changed

app/API/graphql_api.js

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client'
1+
import * as AbsintheSocket from '@absinthe/socket'
2+
import {
3+
ApolloClient,
4+
ApolloLink,
5+
createHttpLink,
6+
InMemoryCache,
7+
Observable,
8+
split,
9+
} from '@apollo/client'
210
import { setContext } from '@apollo/client/link/context'
11+
import { getMainDefinition } from '@apollo/client/utilities'
12+
import { print } from 'graphql'
13+
import { Socket as PhoenixSocket } from 'phoenix'
314

415
import { GRAPHQL_API_URL } from '../config'
516
import { getFromLocalStorage, LOCAL_STORAGE_KEYS } from '../lib/local_storage'
617

18+
// Convert HTTP URL to WebSocket URL
19+
const getWebSocketUrl = (httpUrl) => {
20+
const url = new URL(httpUrl)
21+
const protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'
22+
return `${protocol}//${url.host}/socket`
23+
}
24+
725
const httpLink = createHttpLink({
826
uri: GRAPHQL_API_URL,
927
})
@@ -17,8 +35,78 @@ const authLink = setContext((_, { headers }) => {
1735
}
1836
})
1937

38+
const authedHttpLink = authLink.concat(httpLink)
39+
40+
// Create Phoenix socket connection
41+
const phoenixSocket = new PhoenixSocket(getWebSocketUrl(GRAPHQL_API_URL), {
42+
params: () => {
43+
const token = getFromLocalStorage(LOCAL_STORAGE_KEYS.TOKEN)
44+
if (token) {
45+
return { token }
46+
} else {
47+
return {}
48+
}
49+
},
50+
})
51+
52+
// Wrap Phoenix socket in AbsintheSocket
53+
const absintheSocket = AbsintheSocket.create(phoenixSocket)
54+
55+
// Create custom websocket link for Absinthe subscriptions
56+
const createAbsintheLink = () => {
57+
return new ApolloLink((operation) => {
58+
const { query, variables, operationName } = operation
59+
const queryString = print(query)
60+
61+
return new Observable((observer) => {
62+
const notifier = AbsintheSocket.send(absintheSocket, {
63+
operation: queryString,
64+
variables: variables || {},
65+
operationName: operationName,
66+
})
67+
68+
AbsintheSocket.observe(absintheSocket, notifier, {
69+
onAbort: () => {
70+
observer.error(new Error('Subscription aborted'))
71+
},
72+
onError: (error) => {
73+
observer.error(error)
74+
},
75+
onStart: () => {
76+
// Subscription started
77+
},
78+
onResult: (result) => {
79+
if (result.data) {
80+
observer.next({ data: result.data })
81+
}
82+
if (result.errors) {
83+
observer.error(result.errors)
84+
}
85+
},
86+
})
87+
88+
// Return cleanup function
89+
return () => {
90+
AbsintheSocket.cancel(absintheSocket, notifier)
91+
}
92+
})
93+
})
94+
}
95+
96+
const websocketLink = createAbsintheLink()
97+
98+
// Split link: subscriptions go through websocket, queries/mutations through HTTP
99+
const link = split(
100+
({ query }) => {
101+
const definition = getMainDefinition(query)
102+
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription'
103+
},
104+
websocketLink,
105+
authedHttpLink,
106+
)
107+
20108
const GraphQLClient = new ApolloClient({
21-
link: authLink.concat(httpLink),
109+
link,
22110
cache: new InMemoryCache(),
23111
})
24112

app/API/graphql_queries.js

Lines changed: 154 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ export const VideosQuery = gql`
77
totalPages
88
entries {
99
id
10-
hash_id: hashId
11-
youtube_id: youtubeId
10+
hashId
11+
youtubeId
1212
title
1313
insertedAt
1414
isPartner
1515
thumbnail
1616
speakers {
17-
full_name: fullName
17+
fullName
1818
id
1919
slug
2020
}
@@ -23,6 +23,21 @@ export const VideosQuery = gql`
2323
}
2424
`
2525

26+
export const UserQuery = gql`
27+
query User($username: String!) {
28+
user(username: $username) {
29+
id
30+
username
31+
name
32+
reputation
33+
registeredAt
34+
pictureUrl
35+
miniPictureUrl
36+
achievements
37+
}
38+
}
39+
`
40+
2641
export const VideosAddedByUserQuery = gql`
2742
query UserAddedVideosIndex($offset: Int! = 1, $limit: Int! = 16, $username: String!) {
2843
user(username: $username) {
@@ -32,14 +47,14 @@ export const VideosAddedByUserQuery = gql`
3247
totalPages
3348
entries {
3449
id
35-
hash_id: hashId
36-
youtube_id: youtubeId
50+
hashId
51+
youtubeId
3752
title
3853
insertedAt
3954
isPartner
4055
thumbnail
4156
speakers {
42-
full_name: fullName
57+
fullName
4358
id
4459
slug
4560
}
@@ -82,7 +97,7 @@ export const loggedInUserPendingModerationCount = gql`
8297
query LoggedInUserUnreadNotificationsCount {
8398
loggedInUser {
8499
id
85-
actions_pending_moderation
100+
actionsPendingModeration
86101
}
87102
}
88103
`
@@ -95,3 +110,135 @@ export const loggedInUserTodayReputationGain = gql`
95110
}
96111
}
97112
`
113+
114+
export const VideoDebateQuery = gql`
115+
query VideoDebate($id: ID!) {
116+
video(hashId: $id) {
117+
id
118+
hashId
119+
title
120+
url
121+
thumbnail
122+
language
123+
unlisted
124+
youtubeOffset
125+
speakers {
126+
id
127+
fullName
128+
slug
129+
picture
130+
}
131+
statements {
132+
id
133+
text
134+
time
135+
isDraft
136+
speaker {
137+
id
138+
fullName
139+
picture
140+
}
141+
comments {
142+
id
143+
text
144+
approve
145+
score
146+
insertedAt
147+
replyToId
148+
user {
149+
id
150+
username
151+
pictureUrl
152+
}
153+
source {
154+
id
155+
url
156+
}
157+
}
158+
}
159+
}
160+
}
161+
`
162+
163+
export const DELETE_STATEMENT_MUTATION = gql`
164+
mutation DeleteStatement($id: ID!) {
165+
deleteStatement(id: $id) {
166+
id
167+
}
168+
}
169+
`
170+
171+
export const UPDATE_STATEMENT_MUTATION = gql`
172+
mutation UpdateStatement($id: ID!, $text: String, $time: Int, $speakerId: ID, $isDraft: Boolean) {
173+
updateStatement(id: $id, text: $text, time: $time, speakerId: $speakerId, isDraft: $isDraft) {
174+
id
175+
time
176+
text
177+
isDraft
178+
video {
179+
id
180+
}
181+
}
182+
}
183+
`
184+
185+
export const CREATE_COMMENT_MUTATION = gql`
186+
mutation CreateComment(
187+
$statementId: ID!
188+
$text: String
189+
$source: String
190+
$replyToId: ID
191+
$approve: Boolean
192+
) {
193+
createComment(
194+
statementId: $statementId
195+
text: $text
196+
source: $source
197+
replyToId: $replyToId
198+
approve: $approve
199+
) {
200+
id
201+
text
202+
approve
203+
insertedAt
204+
replyToId
205+
statementId
206+
user {
207+
id
208+
username
209+
pictureUrl
210+
}
211+
source {
212+
id
213+
url
214+
}
215+
}
216+
}
217+
`
218+
219+
export const DELETE_COMMENT_MUTATION = gql`
220+
mutation DeleteComment($id: ID!) {
221+
deleteComment(id: $id) {
222+
id
223+
statementId
224+
replyToId
225+
}
226+
}
227+
`
228+
229+
export const VOTE_COMMENT_MUTATION = gql`
230+
mutation VoteComment($commentId: ID!, $value: Int!) {
231+
voteComment(commentId: $commentId, value: $value) {
232+
id
233+
score
234+
}
235+
}
236+
`
237+
238+
export const FLAG_COMMENT_MUTATION = gql`
239+
mutation FlagComment($commentId: ID!, $reason: Int!) {
240+
flagComment(commentId: $commentId, reason: $reason) {
241+
id
242+
}
243+
}
244+
`

0 commit comments

Comments
 (0)