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

Commit a63fe4f

Browse files
author
Abu Sakib
authored
Add Apollo integration to Instagram clone sample app (#146)
1 parent bd7ef15 commit a63fe4f

File tree

8 files changed

+201
-20
lines changed

8 files changed

+201
-20
lines changed

instaclone/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
REACT_APP_BACKEND_ENDPOINT=""<<your-Dgraph-Cloud-endpoint>>""
12
REACT_APP_AUTH0_DOMAIN="<<your-domain-name>>"
23
REACT_APP_AUTH0_CLIENT_ID="<<your-auth0-application-client-id>>"

instaclone/package-lock.json

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

instaclone/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@apollo/client": "^3.4.3",
6+
"@apollo/client": "^3.4.11",
77
"@auth0/auth0-react": "^1.6.0",
88
"@testing-library/jest-dom": "^5.14.1",
99
"@testing-library/react": "^11.2.7",
1010
"@testing-library/user-event": "^12.8.3",
11-
"graphql": "^15.5.1",
11+
"graphql": "^15.5.3",
1212
"grommet": "^2.17.4",
1313
"grommet-icons": "^4.6.0",
1414
"install": "^0.13.0",

instaclone/src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import NavBar from "./Components/Nav";
2+
import Feed from "./Components/Feed";
23
import { Box, Grommet } from "grommet";
34

45
const theme = {
@@ -15,6 +16,7 @@ const App = () => (
1516
<Grommet theme={theme} full>
1617
<Box fill background="light-3">
1718
<NavBar />
19+
<Feed />
1820
</Box>
1921
</Grommet>
2022
);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {
2+
ApolloClient,
3+
ApolloProvider,
4+
InMemoryCache,
5+
createHttpLink,
6+
} from "@apollo/client";
7+
import { setContext } from "@apollo/client/link/context";
8+
import { useAuth0 } from "@auth0/auth0-react";
9+
import { useState, useEffect } from "react";
10+
import App from "../App";
11+
12+
const ApolloWrapper = () => {
13+
const { isAuthenticated, getIdTokenClaims } = useAuth0();
14+
const [xAuthToken, setXAuthToken] = useState("");
15+
16+
useEffect(() => {
17+
const getToken = async () => {
18+
const token = isAuthenticated ? await getIdTokenClaims() : "";
19+
setXAuthToken(token);
20+
};
21+
getToken();
22+
}, [getIdTokenClaims, isAuthenticated]);
23+
24+
const httpLink = createHttpLink({
25+
uri: process.env.REACT_APP_BACKEND_ENDPOINT,
26+
});
27+
28+
const authLink = setContext((_, { headers, ...rest }) => {
29+
if (!xAuthToken) return { headers, ...rest };
30+
31+
return {
32+
...rest,
33+
headers: {
34+
...headers,
35+
"X-Auth-Token": xAuthToken.__raw,
36+
},
37+
};
38+
});
39+
40+
const client = new ApolloClient({
41+
cache: new InMemoryCache(),
42+
link: authLink.concat(httpLink),
43+
});
44+
45+
return (
46+
<ApolloProvider client={client}>
47+
<App />
48+
</ApolloProvider>
49+
);
50+
};
51+
52+
export default ApolloWrapper;

instaclone/src/Components/Feed.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { useQuery } from "@apollo/client";
2+
import { QUERY_LOGGED_IN_USER } from "../GraphQL/Queries";
3+
4+
import { useAuth0, withAuthenticationRequired } from "@auth0/auth0-react";
5+
import {
6+
Spinner,
7+
Text,
8+
Card,
9+
CardBody,
10+
Heading,
11+
Avatar,
12+
CardHeader,
13+
Box,
14+
Grid,
15+
Image,
16+
CardFooter,
17+
} from "grommet";
18+
19+
const Feed = () => {
20+
const { user } = useAuth0();
21+
const { email } = user;
22+
23+
const { data, loading, error } = useQuery(QUERY_LOGGED_IN_USER, {
24+
variables: {
25+
userFilter: {
26+
email: {
27+
eq: email,
28+
},
29+
},
30+
},
31+
});
32+
33+
if (loading) {
34+
return <Spinner />;
35+
}
36+
if (error) {
37+
return (
38+
<div>
39+
<Text size="large" color="red">
40+
{error.message}
41+
</Text>
42+
</div>
43+
);
44+
}
45+
console.log(data);
46+
return (
47+
<Box pad="large" direction="row" alignSelf="center">
48+
<Grid
49+
gap="large"
50+
rows="medium"
51+
columns={{ count: "fit", size: ["small", "medium"] }}
52+
>
53+
{data.queryUser.map((userInfo) =>
54+
userInfo.following.map((followedUser) =>
55+
followedUser.posts.map((post) => (
56+
<Card width="medium" key={post.id}>
57+
<CardHeader
58+
pad={{ horizontal: "small", vertical: "small" }}
59+
background="light-1"
60+
width="medium"
61+
justify="stretch"
62+
gap="small"
63+
>
64+
<Avatar
65+
size="small"
66+
src={post.postedBy.avatarImageURL}
67+
a11yTitle="Generated avatar for the user from robohash.org"
68+
/>
69+
70+
<Heading level="4" margin="none">
71+
{post.postedBy.username}
72+
</Heading>
73+
</CardHeader>
74+
<CardBody height="medium">
75+
<Image fit="cover" src={post.imageURL} />
76+
</CardBody>
77+
<CardFooter
78+
pad={{ horizontal: "small" }}
79+
height="xxsmall"
80+
background="light-3"
81+
justify="between"
82+
gap="xxsmall"
83+
>
84+
<Text size="small">{post.description}</Text>
85+
<Heading level="5" margin="none">
86+
{post.likes === 1
87+
? `${post.likes} like`
88+
: `${post.likes} likes`}
89+
</Heading>
90+
</CardFooter>
91+
</Card>
92+
))
93+
)
94+
)}
95+
</Grid>
96+
</Box>
97+
);
98+
}
99+
100+
// Do not show this component unless the user have logged in
101+
export default withAuthenticationRequired(Feed, {
102+
onRedirecting: () => <Spinner />,
103+
});

instaclone/src/GraphQL/Queries.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { gql } from "@apollo/client";
2+
3+
export const QUERY_LOGGED_IN_USER = gql`
4+
query getUserInfo($userFilter: UserFilter!) {
5+
queryUser(filter: $userFilter) {
6+
email
7+
following {
8+
username
9+
avatarImageURL
10+
posts {
11+
id
12+
imageURL
13+
description
14+
likes
15+
postedBy {
16+
username
17+
avatarImageURL
18+
}
19+
}
20+
}
21+
}
22+
}
23+
`;

instaclone/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
3-
import App from "./App";
43
import { Auth0Provider } from "@auth0/auth0-react";
4+
import ApolloWrapper from './Components/ApolloWrapper';
55

66
const domain = process.env.REACT_APP_AUTH0_DOMAIN;
77
const clientId = process.env.REACT_APP_AUTH0_CLIENT_ID;
@@ -13,7 +13,7 @@ ReactDOM.render(
1313
redirectUri={window.location.origin}
1414
>
1515
<React.StrictMode>
16-
<App />
16+
<ApolloWrapper />
1717
</React.StrictMode>
1818
</Auth0Provider>,
1919
document.getElementById("root")

0 commit comments

Comments
 (0)