Skip to content
This repository was archived by the owner on Oct 9, 2024. It is now read-only.

Commit c0b4e09

Browse files
author
Abu Sakib
authored
Add subscriptions to Instagram clone sample app (#155)
1 parent a63fe4f commit c0b4e09

File tree

5 files changed

+104
-4
lines changed

5 files changed

+104
-4
lines changed

instaclone/package-lock.json

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

instaclone/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"react-dom": "^17.0.2",
1818
"react-scripts": "4.0.3",
1919
"styled-components": "^5.3.0",
20+
"subscriptions-transport-ws": "^0.9.19",
2021
"web-vitals": "^1.1.2"
2122
},
2223
"scripts": {

instaclone/src/Components/ApolloWrapper.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import {
33
ApolloProvider,
44
InMemoryCache,
55
createHttpLink,
6+
split,
67
} from "@apollo/client";
78
import { setContext } from "@apollo/client/link/context";
89
import { useAuth0 } from "@auth0/auth0-react";
910
import { useState, useEffect } from "react";
11+
import { WebSocketLink } from '@apollo/client/link/ws';
12+
import { getMainDefinition } from "@apollo/client/utilities";
1013
import App from "../App";
1114

1215
const ApolloWrapper = () => {
@@ -37,9 +40,32 @@ const ApolloWrapper = () => {
3740
};
3841
});
3942

43+
const wsLink = new WebSocketLink({
44+
uri: process.env.REACT_APP_BACKEND_ENDPOINT.replace("https://", "wss://"),
45+
options: {
46+
reconnect: true,
47+
minTimeout: 30000,
48+
connectionParams: {
49+
"X-Auth-Token": xAuthToken.__raw,
50+
},
51+
},
52+
});
53+
54+
const splitLink = split(
55+
({ query }) => {
56+
const definition = getMainDefinition(query);
57+
return (
58+
definition.kind === "OperationDefinition" &&
59+
definition.operation === "subscription"
60+
);
61+
},
62+
wsLink,
63+
authLink.concat(httpLink)
64+
);
65+
4066
const client = new ApolloClient({
4167
cache: new InMemoryCache(),
42-
link: authLink.concat(httpLink),
68+
link: splitLink,
4369
});
4470

4571
return (

instaclone/src/Components/Feed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useQuery } from "@apollo/client";
1+
import { useSubscription } from "@apollo/client";
22
import { QUERY_LOGGED_IN_USER } from "../GraphQL/Queries";
33

44
import { useAuth0, withAuthenticationRequired } from "@auth0/auth0-react";
@@ -20,7 +20,7 @@ const Feed = () => {
2020
const { user } = useAuth0();
2121
const { email } = user;
2222

23-
const { data, loading, error } = useQuery(QUERY_LOGGED_IN_USER, {
23+
const { data, loading, error } = useSubscription(QUERY_LOGGED_IN_USER, {
2424
variables: {
2525
userFilter: {
2626
email: {

instaclone/src/GraphQL/Queries.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { gql } from "@apollo/client";
22

33
export const QUERY_LOGGED_IN_USER = gql`
4-
query getUserInfo($userFilter: UserFilter!) {
4+
subscription getUserInfo($userFilter: UserFilter!) {
55
queryUser(filter: $userFilter) {
66
email
77
following {

0 commit comments

Comments
 (0)