Skip to content

Commit fe610f1

Browse files
committed
impliment Apollo Client
1 parent a636ae0 commit fe610f1

File tree

7 files changed

+72
-42
lines changed

7 files changed

+72
-42
lines changed

app/src/Dashboard/Form.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Form = (props) => {
66
return (
77
<div className = 'form'>
88
<h2>{ props.description }</h2>
9+
<button id={ props.id }>heart</button>
910
</div>);
1011
};
1112

app/src/Dashboard/FormsContainer.jsx

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,67 @@
11
import React, { useContext, useState, useEffect } from 'react';
22
import { Link } from 'react-router-dom';
33

4+
import {
5+
ApolloClient, InMemoryCache, gql, ApolloProvider, useQuery,
6+
} from '@apollo/client';
47
import Form from './Form.jsx';
58

69

710
const FormsContainer = () => {
8-
const [tests, updateTests] = useState([]);
9-
10-
useEffect(() => {
11-
console.log('inside useEffect');
12-
13-
fetch('http://localhost:5000/graphql', {
14-
method: 'POST',
15-
headers: {
16-
'Content-Type': 'application/json',
17-
Accept: 'application/json',
18-
},
19-
body: JSON.stringify({ query: '{readAllTests { description }}' }),
20-
})
21-
.then(res => res.json())
22-
.then((resp) => {
23-
// console.log('resp: ', resp);
24-
const myTests = resp.data.readAllTests;
25-
console.log('myTests: ', myTests);
26-
updateTests(myTests);
27-
})
28-
.catch(err => console.log('error in readAllTests', err));
29-
}, []);
30-
31-
const forms = tests.map((test, index) => <Form key={index} description={test.description} />);
11+
// const [tests, updateTests] = useState([]);
12+
/* GET using fetch */
13+
// useEffect(() => {
14+
// console.log('inside useEffect');
15+
16+
// fetch('http://localhost:5000/graphql', {
17+
// method: 'POST',
18+
// headers: {
19+
// 'Content-Type': 'application/json',
20+
// Accept: 'application/json',
21+
// },
22+
// body: JSON.stringify({ query: '{readAllTests { description }}' }),
23+
// })
24+
// .then(res => res.json())
25+
// .then((resp) => {
26+
// // console.log('resp: ', resp);
27+
// const myTests = resp.data.readAllTests;
28+
// console.log('myTests: ', myTests);
29+
// updateTests(myTests);
30+
// })
31+
// .catch(err => console.log('error in readAllTests', err));
32+
// }, []);
33+
34+
35+
/* RULES OF HOOKS: DO NOT use hooks within hooks or conditionals */
36+
// useEffect(() => {
37+
// const GET_TESTS = gql`query {readAllTests { description }}`;
38+
// const { loading, error, data } = useQuery(GET_TESTS);
39+
// if (loading) console.log('Loading...');
40+
// if (error) console.log(`Error :${error}`);
41+
// const myTests = data.readAllTests;
42+
// console.log('myTests: ', myTests);
43+
// updateTests(myTests);
44+
// }, []);
45+
46+
47+
const GET_TESTS = gql`query {readAllTests { description id }}`;
48+
49+
const { loading, error, data } = useQuery(GET_TESTS);
50+
if (loading) return <p>Loading...</p>;
51+
if (error) return <p>Error :{error}</p>;
52+
53+
const myTests = data.readAllTests;
54+
console.log('myTests: ', myTests);
55+
56+
const forms = myTests.map((test, index) => <Form key={index} id={test.id} description={test.description} />);
3257

3358
return (
34-
<div>
35-
<Link to="/">
36-
<button type="button">Go Back</button>
37-
</Link>
38-
{forms}
39-
</div>
59+
<div>
60+
<Link to="/">
61+
<button type="button">Go Back</button>
62+
</Link>
63+
{forms}
64+
</div>
4065
);
4166
};
4267

app/src/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import SignUp from './components/login/SignUp.tsx';
88
import FBPassWord from './components/login/FBPassWord.tsx';
99
import Tutorial from './tutorial/Tutorial.tsx';
1010
import TutorialPage from './tutorial/TutorialPage.tsx';
11-
1211
/*
1312
* Dashboard
1413
*/
1514
import Dashboard from './Dashboard/FormsContainer.jsx';
1615
import styles from './Dashboard/styles.css';
17-
16+
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';
1817

1918
import {
2019
HashRouter as Router,
@@ -36,7 +35,14 @@ const PrivateRoute = ({ component: Component, ...rest }) => (
3635
/>
3736
);
3837

38+
39+
const client = new ApolloClient({
40+
uri: 'http://localhost:5000/graphql',
41+
cache: new InMemoryCache()
42+
});
43+
3944
ReactDOM.render(
45+
<ApolloProvider client={client}>
4046
<Router>
4147
<Switch>
4248
<Route exact path="/login" component={SignIn} />
@@ -47,6 +53,7 @@ ReactDOM.render(
4753
<Route exact path="/tutorial" component={Tutorial} />
4854
<Route exact path="/tutorialPage/:learn" component={TutorialPage} />
4955
</Switch>
50-
</Router>,
56+
</Router>
57+
</ ApolloProvider>,
5158
document.getElementById('app')
5259
);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
},
9595
"homepage": "https://github.com/open-source-labs/ReacType#readme",
9696
"dependencies": {
97+
"@apollo/client": "^3.3.11",
9798
"@babel/cli": "^7.10.4",
9899
"@babel/register": "^7.10.4",
99100
"@material-ui/core": "^4.11.0",

server/graphQL/resolvers/query.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ const { Tests } = require('../../models/reactypeModels');
33
module.exports = {
44
readTest: async (parent, args) => {
55
const resp = await Tests.findOne({ _id: args.id });
6-
if (resp) return { description: resp.name };
7-
return { description: 'Error reading' };
6+
if (resp) return { description: resp.name, id: resp._id };
7+
return { description: 'Error reading', id: '0' };
88
},
99
readAllTests: async () => {
1010
const resp = await Tests.find({});
1111
// console.log('resp', resp);
1212
if (resp) {
13-
return resp.map(elem => ({ description: elem.name }));
13+
return resp.map(elem => ({ description: elem.name, id: elem._id }));
1414
}
1515
// TODO: Go back to this to see how to handle error later
16-
return [{ description: 'Error reading all tests' }];
16+
return [{ description: 'Error reading all tests', id: '0' }];
1717
},
1818
};

server/graphQL/typeDefs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const typeDefs = gql`
2121
}
2222
type Test {
2323
description: String
24+
id: ID
2425
}
2526
type Query {
2627
readTest(id: String): Test

server/server.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ const server = new ApolloServer({ typeDefs, resolvers });
7272
server.applyMiddleware({ app });
7373
/** ****************************************************************** */
7474

75-
// const path = require('path');
76-
// // Serve Static Assets
77-
// app.use(express.static(path.resolve(__dirname, './assets')));
78-
/* ******************************************************************* */
79-
8075

8176

8277
app.post(

0 commit comments

Comments
 (0)