Skip to content

Commit 20f00af

Browse files
committed
add psuedo-code
1 parent 057324b commit 20f00af

File tree

7 files changed

+65
-76
lines changed

7 files changed

+65
-76
lines changed

app/src/Dashboard/FormsContainer.jsx

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,23 @@
1-
import React, { useContext, useState, useEffect } from 'react';
1+
import React from 'react';
22
import { Link } from 'react-router-dom';
33

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

7+
// Implement Apollo Client useQuery hook to retrieve data from the server through graphQL. This includes 2 steps:
8+
// 1) Impliment Apollo Provider in the top component in ./src/index.js, this allows children components access to the queried data
9+
// 2) useQuery hook will update the data stored in Apollo Client's cache and automatically trigger child components rendering
910

1011
const FormsContainer = () => {
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-
12+
// define the graphQL query string
4713
const GET_TESTS = gql`query {readAllTests { description id }}`;
48-
14+
// useQuery hook abstracts fetch request
4915
const { loading, error, data } = useQuery(GET_TESTS);
5016
if (loading) return <p>Loading...</p>;
5117
if (error) return <p>Error :{error}</p>;
52-
18+
// based on resolver(readAllTests) for this query, the data is stored in the data object with the key 'readAllTests'
5319
const myTests = data.readAllTests;
54-
console.log('myTests: ', myTests);
55-
20+
// generate an array of Form components based on data
5621
const forms = myTests.map((test, index) => <Form key={index} id={test.id} description={test.description} />);
5722

5823
return (

app/src/index.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ 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-
/*
12-
* Dashboard
13-
*/
14-
import Dashboard from './Dashboard/FormsContainer.jsx';
15-
import styles from './Dashboard/styles.css';
16-
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';
1711

1812
import {
1913
HashRouter as Router,
@@ -22,10 +16,28 @@ import {
2216
Switch,
2317
} from 'react-router-dom';
2418

19+
20+
/*
21+
* Dashboard
22+
*/
23+
import Dashboard from './Dashboard/FormsContainer.jsx';
24+
import styles from './Dashboard/styles.css';
25+
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';
26+
27+
const client = new ApolloClient({
28+
uri: 'http://localhost:5000/graphql',
29+
cache: new InMemoryCache()
30+
});
31+
32+
/*
33+
*
34+
*/
35+
36+
2537
const PrivateRoute = ({ component: Component, ...rest }) => (
2638
<Route
2739
{...rest}
28-
render={props => {
40+
render={ (props) => {
2941
return Cookies.get('ssid') || window.localStorage.getItem('ssid') ? (
3042
<Component {...props} />
3143
) : (
@@ -36,24 +48,19 @@ const PrivateRoute = ({ component: Component, ...rest }) => (
3648
);
3749

3850

39-
const client = new ApolloClient({
40-
uri: 'http://localhost:5000/graphql',
41-
cache: new InMemoryCache()
42-
});
43-
4451
ReactDOM.render(
4552
<ApolloProvider client={client}>
46-
<Router>
47-
<Switch>
48-
<Route exact path="/login" component={SignIn} />
49-
<Route exact path="/signup" component={SignUp} />
50-
<Route exact path="/password" component={FBPassWord} />
51-
<PrivateRoute exact path="/" component={App} />
52-
<Route exact path="/dashboard" component={Dashboard} />
53-
<Route exact path="/tutorial" component={Tutorial} />
54-
<Route exact path="/tutorialPage/:learn" component={TutorialPage} />
55-
</Switch>
56-
</Router>
53+
<Router>
54+
<Switch>
55+
<Route exact path="/login" component={SignIn} />
56+
<Route exact path="/signup" component={SignUp} />
57+
<Route exact path="/password" component={FBPassWord} />
58+
<PrivateRoute exact path="/" component={App} />
59+
<Route exact path="/dashboard" component={Dashboard} />
60+
<Route exact path="/tutorial" component={Tutorial} />
61+
<Route exact path="/tutorialPage/:learn" component={TutorialPage} />
62+
</Switch>
63+
</Router>
5764
</ ApolloProvider>,
5865
document.getElementById('app')
5966
);

server/graphQL/resolvers/mutation.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
const { Tests } = require('../../models/reactypeModels');
2+
/*
3+
* resolvers are functions that handles graphQL requests. This file defines the logic for graphQL mutation requests
4+
* Link to Apollo Mutations:
5+
* https://www.apollographql.com/docs/apollo-server/data/resolvers/#defining-a-resolver
6+
*/
27

38

49
module.exports = {
5-
10+
611
addTest: async (parent, args) => {
712
const resp = await Tests.create({ name: args.name });
8-
// console.log('Response >>> ', resp);
13+
console.log('Added test', resp);
914
if (resp) return { description: args.name };
1015
return { description: 'Error creating test' };
1116
},
12-
updateTest: async (parent, args, context, info) => {
1317

18+
updateTest: async (parent, args) => {
1419
const filter = { _id: args.id };
1520
const update = { name: args.name };
16-
1721
const resp = await Tests.updateOne(filter, update);
1822
console.log('Updated database with', resp);
1923
if (resp) return { description: args.name };
2024
return { description: 'Error updating' };
2125
},
2226

23-
2427
deleteTest: async (parent, args) => {
2528
const filter = { _id: args.id };
2629
const resp = await Tests.deleteOne(filter);

server/graphQL/resolvers/query.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const { Tests } = require('../../models/reactypeModels');
22

3+
// Link to Apollo Query Types:
4+
// https://www.apollographql.com/docs/apollo-server/data/resolvers/#defining-a-resolver
5+
36
module.exports = {
47
readTest: async (parent, args) => {
58
const resp = await Tests.findOne({ _id: args.id });

server/graphQL/typeDefs.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11

22
const { gql } = require('apollo-server-express');
33

4+
// Link to defining a schema in Apollo:
5+
// https://www.apollographql.com/docs/apollo-server/schema/schema/
6+
// The schema specifies which queries and mutations are available for clients
7+
// to execute against your data graph.
48

59
const typeDefs = gql`
6-
# Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
7-
10+
811
type Mutation {
912
addTest(name: String): Test
1013
updateTest(id: String, name: String): Test

server/models/reactypeModels.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,17 @@ const projectSchema = new Schema({
6666
createdAt: { type: Date, default: Date.now }
6767
});
6868

69+
// Test schema for implementing GraphQL
6970
const testSchema = new Schema({
7071
name: String,
7172
});
73+
const Tests = mongoose.model('Tests', testSchema);
74+
/* *********************************************** */
75+
7276

7377
const Users = mongoose.model('Users', userSchema);
7478
const Sessions = mongoose.model('Sessions', sessionSchema);
7579
const Projects = mongoose.model('Projects', projectSchema);
76-
const Tests = mongoose.model('Tests', testSchema);
7780

7881
module.exports = {
7982
Users, Sessions, Projects, Tests,

server/server.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,21 @@ GraphQl Router
5858
/* ******************************************************************* */
5959
const { ApolloServer } = require('apollo-server-express');
6060

61+
// Query resolvers
6162
const query = require('./graphQL/resolvers/query');
62-
63+
// Mutation resolvers
6364
const mutation = require('./graphQL/resolvers/mutation');
6465

66+
// package resolvers into one variable to pass to Apollo Server
6567
const resolvers = {
6668
Query: query,
6769
Mutation: mutation,
6870
};
6971

72+
// schemas used for graphQL
7073
const { typeDefs } = require('./graphQL/typeDefs');
74+
75+
// instantiate Apollo server and attach to Express server, mounted at 'http://localhost:PORT/graphql'
7176
const server = new ApolloServer({ typeDefs, resolvers });
7277
server.applyMiddleware({ app });
7378
/** ****************************************************************** */

0 commit comments

Comments
 (0)