Skip to content

Commit dae349f

Browse files
authored
Merge pull request #7 from oslabs-beta/ian/feature/working-with-react17
Ian/feature/working with react17
2 parents a91ecb7 + b82c233 commit dae349f

File tree

6 files changed

+108
-51
lines changed

6 files changed

+108
-51
lines changed

app/electron/main.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const {
2020
const { resolve } = require('app-root-path');
2121

2222
// to install react dev tool extension
23-
const {
24-
default: installExtension,
25-
REACT_DEVELOPER_TOOLS
26-
} = require('electron-devtools-installer');
23+
// const {
24+
// default: installExtension,
25+
// REACT_DEVELOPER_TOOLS
26+
// } = require('electron-devtools-installer');
2727
const debug = require('electron-debug');
2828

2929
// import custom protocol in protocol.js

package.json

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"scripts": {
6868
"postinstall": "set ELECTRON_BUILDER_ALLOW_UNRESOLVED_DEPENDENCIES=true electron-builder install-app-deps",
6969
"dev-server": "cross-env NODE_ENV=development webpack-dev-server --config ./webpack.development.js",
70-
"dev": "concurrently -k \"cross-env NODE_ENV=development webpack-dev-server --config ./webpack.development.js\" \"cross-env NODE_ENV=development nodemon server/server.js --open\"",
70+
"dev": "concurrently -k \"cross-env NODE_ENV=development webpack-dev-server --config ./webpack.development.js\" \"cross-env NODE_ENV=development nodemon server/server.mjs --open\"",
7171
"electron-dev": "cross-env NODE_ENV=development electron .",
7272
"prod-build": "cross-env NODE_ENV=production npx webpack --mode=production --config ./webpack.production.js",
7373
"prod": "npm run prod-build && electron . --no-sandbox",
@@ -126,28 +126,27 @@
126126
},
127127
"homepage": "https://github.com/open-source-labs/ReacType#readme",
128128
"dependencies": {
129-
"@apollo/client": "^3.6.9",
130-
"@apollo/federation": "^0.28.0",
129+
"@apollo/client": "^3.7.10",
130+
"@apollo/federation": "^0.38.1",
131+
"@apollo/server": "^4.4.1",
131132
"@babel/cli": "^7.18.10",
132133
"@babel/register": "^7.18.9",
134+
"@graphql-tools/schema": "^9.0.17",
133135
"@mui/icons-material": "^5.8.4",
134136
"@mui/lab": "^5.0.0-alpha.93",
135137
"@mui/styles": "^5.11.12",
136-
"@nestjs/graphql": "^10.0.21",
137138
"@reduxjs/toolkit": "^1.9.3",
138139
"@types/js-cookie": "^3.0.2",
139140
"@types/node": "^18.6.3",
140141
"@types/prettier": "^2.6.4",
141142
"@types/react-redux": "^7.1.24",
142143
"@types/react-router-dom": "^5.3.3",
143144
"ace-builds": "^1.8.1",
144-
"apollo-server": "^2.0.0",
145-
"apollo-server-core": "^3.12.0",
146-
"apollo-server-express": "^3.12.0",
147145
"app-root-path": "^3.0.0",
148146
"autoprefixer": "^10.4.8",
149147
"babel-polyfill": "^6.26.0",
150148
"bcryptjs": "^2.4.3",
149+
"body-parser": "^1.20.2",
151150
"cookie-parser": "^1.4.6",
152151
"cors": "^2.8.5",
153152
"d3": "^7.6.1",
@@ -157,11 +156,9 @@
157156
"electron-window-manager": "^1.0.6",
158157
"esbuild-wasm": "^0.14.51",
159158
"eslint-plugin-react-hooks": "^4.6.0",
160-
"express-graphql": "^0.12.0",
161159
"fs": "^0.0.1-security",
162-
"graphql": "^15.8.0",
160+
"graphql": "^16.6.0",
163161
"identity-obj-proxy": "^3.0.0",
164-
"immutable": "^4.1.0",
165162
"js-cookie": "^3.0.1",
166163
"localforage": "^1.10.0",
167164
"lodash": "^4.17.21",
@@ -204,7 +201,7 @@
204201
"@types/jest": "^28.1.6",
205202
"@typescript-eslint/eslint-plugin": "^5.35.1",
206203
"@typescript-eslint/parser": "^5.35.1",
207-
"apollo": "^2.11.1",
204+
"apollo": "^2.34.0",
208205
"axios": "^0.27.2",
209206
"babel-eslint": "^10.1.0",
210207
"babel-jest": "^28.1.3",
@@ -254,7 +251,7 @@
254251
"webpack-merge": "^4.2.2"
255252
},
256253
"engines": {
257-
"node": "18.x",
258-
"npm": "8.19.2"
254+
"node": "19.x",
255+
"npm": "9.5.0"
259256
}
260257
}

server/graphQL/resolvers/mutation.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
const { UserInputError } = require('apollo-server-express');
1+
// const { ApolloServerErrorCode.BAD_USER_INPUT } = require('apollo-server-express');
2+
3+
// import { ApolloServerErrorCode } from '@apollo/server/errors';// v4 syntax
4+
const ApolloServerErrorCode = require('@apollo/server/errors');
5+
//now using ApolloServerErrorCode.BAD_USER_INPUT in place of ApolloServerErrorCode.BAD_USER_INPUT
6+
27
const { Projects, Users, Comments } = require('../../models/reactypeModels');
8+
39
/*
410
* resolvers are functions that handles graphQL requests. This file defines the logic for graphQL mutation requests
511
* Link to Apollo Mutations:
@@ -23,7 +29,7 @@ const Project = {
2329
});
2430
}
2531

26-
throw new UserInputError('Project is not found. Please try another project ID', {
32+
throw new ApolloServerErrorCode.BAD_USER_INPUT('Project is not found. Please try another project ID', {
2733
argumentName: 'projId',
2834
});
2935
},
@@ -33,7 +39,7 @@ const Project = {
3339
const target = await Projects.findOne(filter);
3440

3541
if (!target) {
36-
throw new UserInputError('Project is not found. Please try another project ID', {
42+
throw new ApolloServerErrorCode.BAD_USER_INPUT('Project is not found. Please try another project ID', {
3743
argumentName: 'projId',
3844
});
3945
}
@@ -63,10 +69,10 @@ const Project = {
6369
});
6470
}
6571

66-
throw new UserInputError('Internal Server Error');
72+
throw new ApolloServerErrorCode.BAD_USER_INPUT('Internal Server Error');
6773
}
6874

69-
throw new UserInputError('User is not found. Please try another user ID', {
75+
throw new ApolloServerErrorCode.BAD_USER_INPUT('User is not found. Please try another user ID', {
7076
argumentName: 'userId',
7177
});
7278
},
@@ -87,7 +93,7 @@ const Project = {
8793
});
8894
}
8995

90-
throw new UserInputError('Project is not found. Please try another project ID', {
96+
throw new ApolloServerErrorCode.BAD_USER_INPUT('Project is not found. Please try another project ID', {
9197
argumentName: 'projId',
9298
});
9399
},
@@ -109,7 +115,7 @@ const Project = {
109115
});
110116
}
111117

112-
throw new UserInputError('Project is not found. Please try another project ID', {
118+
throw new ApolloServerErrorCode.BAD_USER_INPUT('Project is not found. Please try another project ID', {
113119
argumentName: 'projId',
114120
});
115121
},
@@ -145,7 +151,7 @@ const Project = {
145151
});
146152
}
147153

148-
throw new UserInputError('Project cannot be found. Please try another project ID', {
154+
throw new ApolloServerErrorCode.BAD_USER_INPUT('Project cannot be found. Please try another project ID', {
149155
argumentName: 'projId',
150156
});
151157
},

server/graphQL/resolvers/query.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
const { UserInputError } = require('apollo-server-express');
1+
// const { UserInputError } = require('apollo-server-express');//v3 syntax
2+
3+
// import { ApolloServerErrorCode } from '@apollo/server/errors';// v4 syntax
4+
const ApolloServerErrorCode = require('@apollo/server/errors')
5+
//now using ApolloServerErrorCode.BAD_USER_INPUT in place of UserInputError
6+
27
const { Projects, Comments } = require('../../models/reactypeModels');
38
// Link to Apollo Query Types:
49
// https://www.apollographql.com/docs/apollo-server/data/resolvers/#defining-a-resolver
@@ -20,7 +25,7 @@ const Project = {
2025
}
2126

2227
// resp is null if nothing is found based on the project ID
23-
throw new UserInputError('Project is not found. Please try another project ID', {
28+
throw new ApolloServerErrorCode.BAD_USER_INPUT('Project is not found. Please try another project ID', {
2429
argumentName: 'projId',
2530
});
2631
},
@@ -32,7 +37,7 @@ const Project = {
3237
resp = resp.filter(proj => proj.userId == userId);
3338
// if resp = [] after the filtering, this means the userId doesnt exisit in the database, throw error as follow
3439
if (resp.length === 0) {
35-
throw new UserInputError(`Project for userId: "${userId}". Please try another id`, {
40+
throw new ApolloServerErrorCode.BAD_USER_INPUT(`Project for userId: "${userId}". Please try another id`, {
3641
argumentName: 'userId',
3742
});
3843
}
@@ -53,7 +58,7 @@ const Project = {
5358
}));
5459
}
5560
// resp is null, return error message
56-
throw new UserInputError('Internal Server Error');
61+
throw new ApolloServerErrorCode.BAD_USER_INPUT('Internal Server Error');
5762
},
5863
};
5964

server/graphQL/schema/typeDefs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { gql } = require('apollo-server-express');
1+
// const { gql } = require('apollo-server-express');
2+
const { gql } = require('@apollo/client');
23

34
// Link to defining a schema in Apollo:
45
// https://www.apollographql.com/docs/apollo-server/schema/schema/

server/server.js renamed to server/server.mjs

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1-
const { ApolloServer } = require('apollo-server-express');
2-
const express = require('express');
3-
const cookieParser = require('cookie-parser');
1+
// const { ApolloServer } = require('@apollo/server');//changed for v4
2+
import { ApolloServer } from '@apollo/server';
3+
4+
//v4 Apollo imports
5+
import { expressMiddleware } from '@apollo/server/express4';
6+
import cors from 'cors';
7+
import bodyParser from 'body-parser';
8+
const {json, urlencoded} = bodyParser;
9+
10+
//possibly redundant
11+
// const {makeExecutableSchema} = require('@graphql-tools/schema');
12+
import { makeExecutableSchema } from '@graphql-tools/schema';
13+
14+
// const express = require('express');
15+
// const cookieParser = require('cookie-parser');
16+
import express from 'express';
17+
import cookieParser from 'cookie-parser';
18+
419
//const passport = require('passport');
520
//const GitHubStrategy = require('passport-github2').Strategy;
6-
const { DEV_PORT } = require('../config');
721

8-
const path = require('path');
9-
const cors = require('cors');
10-
const userController = require('./controllers/userController');
11-
const cookieController = require('./controllers/cookieController');
12-
const sessionController = require('./controllers/sessionController');
13-
const projectController = require('./controllers/projectController');
22+
// const { DEV_PORT } = require('../config');
23+
import DEV_PORT from '../config.js';
24+
25+
// const path = require('path');
26+
import path from 'path';
27+
28+
// const userController = require('./controllers/userController');
29+
// const cookieController = require('./controllers/cookieController');
30+
// const sessionController = require('./controllers/sessionController');
31+
// const projectController = require('./controllers/projectController');
32+
33+
import userController from './controllers/userController.js';
34+
import cookieController from './controllers/cookieController.js';
35+
import sessionController from './controllers/sessionController.js';
36+
import projectController from './controllers/projectController.js';
1437

1538
const app = express();
1639

@@ -24,7 +47,8 @@ app.use(express.urlencoded({ limit: '100mb', extended: true }));
2447
app.use(cookieParser());
2548

2649
// Routes
27-
const stylesRouter = require('./routers/stylesRouter');
50+
// const stylesRouter = require('./routers/stylesRouter');
51+
import stylesRouter from './routers/stylesRouter.js';
2852

2953
// enable cors
3054
// options: origin: allows from localhost when in dev or the app://rse when using prod, credentials: allows credentials header from origin (needed to send cookies)
@@ -102,9 +126,14 @@ GraphQl Router
102126
/* ******************************************************************* */
103127

104128
// Query resolvers
105-
const Query = require('./graphQL/resolvers/query');
129+
// const Query = require('./graphQL/resolvers/query');
130+
import Query from './graphQL/resolvers/query.js';
131+
// const {Query} = query;
132+
106133
// Mutation resolvers
107-
const Mutation = require('./graphQL/resolvers/mutation');
134+
// const Mutation = require('./graphQL/resolvers/mutation');
135+
import Mutation from './graphQL/resolvers/mutation.js';
136+
// const {Mutation} = mutation;
108137

109138
// package resolvers into one variable to pass to Apollo Server
110139
const resolvers = {
@@ -121,16 +150,34 @@ const resolvers = {
121150
app.use('/user-styles', stylesRouter);
122151

123152
// schemas used for graphQL
124-
const typeDefs = require('./graphQL/schema/typeDefs.js');
125-
const { dirname } = require('node:path');
153+
154+
// const typeDefs = require('./graphQL/schema/typeDefs.js');
155+
import typeDefs from './graphQL/schema/typeDefs.js';
156+
157+
// const { dirname } = require('node:path');
126158

127159
// instantiate Apollo server and attach to Express server, mounted at 'http://localhost:PORT/graphql'
128160

129-
const server = new ApolloServer({ typeDefs, resolvers });
130-
(async function() {
131-
await server.start()
132-
server.applyMiddleware({ app, path: '/graphql' });
133-
}());
161+
//use make exacutable schema to allow schema to be passed to new server
162+
const schema = makeExecutableSchema({typeDefs, resolvers});
163+
164+
const server = new ApolloServer({schema});
165+
//V3 syntax
166+
// (async function() {
167+
// await server.start()
168+
// server.applyMiddleware({ app, path: '/graphql' });
169+
// }());
170+
171+
//v4 syntax
172+
await server.start()
173+
app.use(
174+
'/graphql',
175+
cors(),
176+
json(),
177+
expressMiddleware(server, {
178+
context: async ({ req }) => ({ token: req.headers.token }),
179+
}),
180+
);
134181

135182
/** ****************************************************************** */
136183

@@ -204,6 +251,7 @@ app.use((err, req, res, next) => {
204251

205252
// starts server on PORT
206253
if (isDev || isProd) {
207-
app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));
254+
app.listen(PORT.DEV_PORT, () => console.log(`Server listening on port: ${PORT.DEV_PORT}`));
208255
}
209256
if (isTest) module.exports = app;
257+
// export default app;

0 commit comments

Comments
 (0)