Clase 6
This commit is contained in:
parent
f1391bde3d
commit
074021530f
4
Clase 6/Ejercicio/00-namespace.yaml
Normal file
4
Clase 6/Ejercicio/00-namespace.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: node-app
|
13
Clase 6/Ejercicio/01-configmap.yaml
Normal file
13
Clase 6/Ejercicio/01-configmap.yaml
Normal file
@ -0,0 +1,13 @@
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: node-app-configmap
|
||||
namespace: node-app
|
||||
data:
|
||||
#WHITELIST URLS Default to http://localhost
|
||||
WHITELIST_URLS: http://localhost,http://localhost:4000
|
||||
PORT: "4000"
|
||||
HOST_MONGO: mongodb-node-app-svc
|
||||
PORT_MONGO: "27017"
|
||||
DB_MONGO: app_db
|
||||
|
9
Clase 6/Ejercicio/02-secret.yaml
Normal file
9
Clase 6/Ejercicio/02-secret.yaml
Normal file
@ -0,0 +1,9 @@
|
||||
apiVersion: v1
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: node-app-secret
|
||||
namespace: node-app
|
||||
type: Opaque
|
||||
data:
|
||||
USER_MONGO: cm9vdA==
|
||||
PASSWORD_MONGO: MTIzNA==
|
16
Clase 6/Ejercicio/03-service.yaml
Normal file
16
Clase 6/Ejercicio/03-service.yaml
Normal file
@ -0,0 +1,16 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: app-service
|
||||
namespace: node-app
|
||||
spec:
|
||||
ports:
|
||||
- name: 80-tcp
|
||||
port: 80
|
||||
protocol: TCP
|
||||
targetPort: 4000
|
||||
selector:
|
||||
com.docker.project: node-app
|
||||
type: LoadBalancer
|
||||
status:
|
||||
loadBalancer: {}
|
13
Clase 6/Ejercicio/04-db-svc.yaml
Normal file
13
Clase 6/Ejercicio/04-db-svc.yaml
Normal file
@ -0,0 +1,13 @@
|
||||
kind: Service
|
||||
apiVersion: v1
|
||||
metadata:
|
||||
name: mongodb-node-app-svc
|
||||
namespace: node-app
|
||||
spec:
|
||||
selector:
|
||||
app: mongodb-node-app
|
||||
ports:
|
||||
- protocol: TCP
|
||||
name: mongodb-node-app
|
||||
port: 27017
|
||||
targetPort: 27017
|
58
Clase 6/Ejercicio/05-deployment.yaml
Normal file
58
Clase 6/Ejercicio/05-deployment.yaml
Normal file
@ -0,0 +1,58 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: node-app
|
||||
namespace: node-app
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
com.docker.project: node-app
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
com.docker.project: node-app
|
||||
spec:
|
||||
containers:
|
||||
- name: node-app
|
||||
image: aleleba/example-app:2.0.1
|
||||
imagePullPolicy: Always
|
||||
env:
|
||||
- name: PORT
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: node-app-configmap
|
||||
key: PORT
|
||||
- name: WHITELIST_URLS
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: node-app-configmap
|
||||
key: WHITELIST_URLS
|
||||
- name: HOST_MONGO
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: node-app-configmap
|
||||
key: HOST_MONGO
|
||||
- name: PORT_MONGO
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: node-app-configmap
|
||||
key: PORT_MONGO
|
||||
- name: DB_MONGO
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: node-app-configmap
|
||||
key: DB_MONGO
|
||||
- name: USER_MONGO
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: node-app-secret
|
||||
key: USER_MONGO
|
||||
- name: PASSWORD_MONGO
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: node-app-secret
|
||||
key: PASSWORD_MONGO
|
||||
ports:
|
||||
- containerPort: 80
|
||||
protocol: TCP
|
49
Clase 6/Ejercicio/06-mongodb-stayfullset.yaml
Normal file
49
Clase 6/Ejercicio/06-mongodb-stayfullset.yaml
Normal file
@ -0,0 +1,49 @@
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
name: mongodb-node-app
|
||||
namespace: node-app
|
||||
spec:
|
||||
replicas: 1
|
||||
serviceName: mongodb-node-app-svc
|
||||
selector:
|
||||
matchLabels:
|
||||
app: mongodb-node-app
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: mongodb-node-app
|
||||
spec:
|
||||
containers:
|
||||
- name: mongodb-node-app
|
||||
image: mongo:5.0.12
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 27017
|
||||
env:
|
||||
- name: MONGO_DATA_DIR
|
||||
value: /data/db
|
||||
- name: MONGO_LOG_DIR
|
||||
value: /dev/null
|
||||
- name: MONGODB_USER
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: node-app-secret
|
||||
key: USER_MONGO
|
||||
- name: MONGO_INITDB_ROOT_USERNAME
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: node-app-secret
|
||||
key: USER_MONGO
|
||||
- name: MONGODB_PASS
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: node-app-secret
|
||||
key: PASSWORD_MONGO
|
||||
- name: MONGO_INITDB_ROOT_PASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: node-app-secret
|
||||
key: PASSWORD_MONGO
|
||||
- name: MONGO_INITDB_DATABASE
|
||||
value: admin
|
6
Clase 6/Ejercicio/server-graphql/.babelrc
Normal file
6
Clase 6/Ejercicio/server-graphql/.babelrc
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"presets": [
|
||||
"@babel/preset-env",
|
||||
"@babel/preset-typescript"
|
||||
]
|
||||
}
|
3
Clase 6/Ejercicio/server-graphql/.dockerignore
Normal file
3
Clase 6/Ejercicio/server-graphql/.dockerignore
Normal file
@ -0,0 +1,3 @@
|
||||
#Quitar Node Modules
|
||||
/node_modules
|
||||
.env
|
16
Clase 6/Ejercicio/server-graphql/.env.development
Normal file
16
Clase 6/Ejercicio/server-graphql/.env.development
Normal file
@ -0,0 +1,16 @@
|
||||
#ENVIRONMENT Defauld production
|
||||
ENV=development
|
||||
#WHITELIST URLS Default to http://localhost
|
||||
WHITELIST_URLS=http://localhost:4000
|
||||
#GRAPHIQL Default to "false"
|
||||
GRAPHIQL=false
|
||||
#PLAYGROUND GRAPHQL Default to "false"
|
||||
PLAYGROUND_GRAPHQL=true
|
||||
# PORT EXPOSE APP Default to 4000
|
||||
PORT=4000
|
||||
# MONGO CONFIGURATION
|
||||
HOST_MONGO=localhost
|
||||
PORT_MONGO=27017
|
||||
DB_MONGO=app_db
|
||||
USER_MONGO=root
|
||||
PASSWORD_MONGO=1234
|
16
Clase 6/Ejercicio/server-graphql/.env.example
Normal file
16
Clase 6/Ejercicio/server-graphql/.env.example
Normal file
@ -0,0 +1,16 @@
|
||||
#ENVIRONMENT Defauld production
|
||||
ENV=
|
||||
#WHITELIST URLS Default to http://localhost
|
||||
WHITELIST_URLS=
|
||||
#GRAPHIQL Default to "false"
|
||||
GRAPHIQL=
|
||||
#PLAYGROUND GRAPHQL Default to "false"
|
||||
PLAYGROUND_GRAPHQL=
|
||||
# PORT EXPOSE APP Default to 4000
|
||||
PORT=
|
||||
# MONGO CONFIGURATION
|
||||
HOST_MONGO=
|
||||
PORT_MONGO=
|
||||
DB_MONGO=
|
||||
USER_MONGO=
|
||||
PASSWORD_MONGO=
|
7
Clase 6/Ejercicio/server-graphql/.eslintignore
Normal file
7
Clase 6/Ejercicio/server-graphql/.eslintignore
Normal file
@ -0,0 +1,7 @@
|
||||
#Eslint
|
||||
.eslintrc.js
|
||||
#Build
|
||||
build
|
||||
#Webpack
|
||||
webpack.config.ts
|
||||
webpack.config.dev.ts
|
37
Clase 6/Ejercicio/server-graphql/.eslintrc.js
Normal file
37
Clase 6/Ejercicio/server-graphql/.eslintrc.js
Normal file
@ -0,0 +1,37 @@
|
||||
module.exports = {
|
||||
'env': {
|
||||
'browser': true,
|
||||
'es2021': true,
|
||||
'node': true,
|
||||
},
|
||||
'extends': [
|
||||
'eslint:recommended',
|
||||
'plugin:@typescript-eslint/recommended'
|
||||
],
|
||||
'parser': '@typescript-eslint/parser',
|
||||
'parserOptions': {
|
||||
'ecmaVersion': 'latest',
|
||||
'sourceType': 'module'
|
||||
},
|
||||
'plugins': [
|
||||
'@typescript-eslint'
|
||||
],
|
||||
'rules': {
|
||||
'indent': [
|
||||
'error',
|
||||
'tab'
|
||||
],
|
||||
'linebreak-style': [
|
||||
'error',
|
||||
'unix'
|
||||
],
|
||||
'quotes': [
|
||||
'error',
|
||||
'single'
|
||||
],
|
||||
'semi': [
|
||||
'error',
|
||||
'always'
|
||||
]
|
||||
}
|
||||
};
|
4
Clase 6/Ejercicio/server-graphql/.gitignore
vendored
Normal file
4
Clase 6/Ejercicio/server-graphql/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
# dependencies
|
||||
/node_modules
|
||||
/build
|
||||
.env
|
3
Clase 6/Ejercicio/server-graphql/.vscode/settings.json
vendored
Normal file
3
Clase 6/Ejercicio/server-graphql/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"typescript.tsdk": "node_modules/typescript/lib"
|
||||
}
|
54
Clase 6/Ejercicio/server-graphql/Dockerfile
Normal file
54
Clase 6/Ejercicio/server-graphql/Dockerfile
Normal file
@ -0,0 +1,54 @@
|
||||
FROM aleleba/ubuntu:4.0.0 as dev-builder
|
||||
|
||||
#docker build --target dev-builder -t aleleba/app:dev .
|
||||
#docker run --name mongodb --rm -p 27017:27017 -e MONGODB_USER=root -e MONGODB_PASS=1234 -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=1234 -d mongo:5.0.12
|
||||
|
||||
RUN sudo apt-get update
|
||||
|
||||
COPY ["./package.json", "./package-lock.json", "/app/"]
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN npm install --legacy-peer-deps
|
||||
|
||||
RUN npm audit fix --legacy-peer-deps
|
||||
|
||||
COPY [".", "/app/"]
|
||||
|
||||
EXPOSE 80
|
||||
|
||||
CMD ["/bin/bash"]
|
||||
|
||||
#CURRENT_DIR=$(pwd) && docker run -it -d --rm --name app -p 80:80 -v "$CURRENT_DIR":/app aleleba/app:dev
|
||||
|
||||
FROM aleleba/ubuntu:4.0.0 as pre-prod-builder
|
||||
|
||||
RUN sudo apt-get update
|
||||
|
||||
COPY --from=dev-builder ["/app/", "/app/"]
|
||||
|
||||
RUN sudo rm -rf /app/.env
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN npm run build
|
||||
|
||||
FROM aleleba/ubuntu:4.0.0 as prod-builder
|
||||
|
||||
#docker build -t aleleba/ro-ut:tag .
|
||||
|
||||
RUN sudo apt-get update
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=pre-prod-builder ["/app/package.json", "/app/package-lock.json", "/app/"]
|
||||
|
||||
COPY --from=pre-prod-builder ["/app/node_modules/", "/app/node_modules/"]
|
||||
|
||||
COPY --from=pre-prod-builder ["/app/build/", "/app/build/"]
|
||||
|
||||
EXPOSE 80
|
||||
|
||||
#docker run -it -d --rm -p 4000:4000 -e WHITELIST_URLS=http://localhost:4000 -e PLAYGROUND_GRAPHQL=true -e HOST_MONGO=localhost -e PORT_MONGO=27017 -e DB_MONGO=app_db -e USER_MONGO=root -e PASSWORD_MONGO=1234 --name app aleleba/example-app:2.0.1
|
||||
|
||||
CMD ["npm", "start"]
|
21
Clase 6/Ejercicio/server-graphql/LICENSE
Normal file
21
Clase 6/Ejercicio/server-graphql/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Alejandro Lembke Barrientos
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
11
Clase 6/Ejercicio/server-graphql/PRNameGenerator.ts
Normal file
11
Clase 6/Ejercicio/server-graphql/PRNameGenerator.ts
Normal file
@ -0,0 +1,11 @@
|
||||
const PRName = function () {
|
||||
let ID = '';
|
||||
// let characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
const characters = '0123456789';
|
||||
for ( let i = 0; i < 6; i++ ) {
|
||||
ID += characters.charAt(Math.floor(Math.random() * 10));
|
||||
}
|
||||
return 'PR-'+ID;
|
||||
};
|
||||
|
||||
console.log(PRName());
|
76
Clase 6/Ejercicio/server-graphql/README.md
Normal file
76
Clase 6/Ejercicio/server-graphql/README.md
Normal file
@ -0,0 +1,76 @@
|
||||
# Create Node TS GraphQL Server
|
||||
|
||||
This project aims to have a starter kit for creating a new Node with typescript, GraphQL server and tools that generally go along with it.
|
||||
|
||||
Tech(Library or Framework) | Version |
|
||||
--- | --- |
|
||||
Jest (Testing) | 29.0.3
|
||||
Typescript | 4.8.3
|
||||
GraphQL | 16.6.0
|
||||
|
||||
## Setup
|
||||
To create a new project run in the terminal:
|
||||
```
|
||||
npx @aleleba/create-node-ts-graphql-server server-app-name
|
||||
```
|
||||
Then run:
|
||||
```
|
||||
cd server-app-name
|
||||
```
|
||||
You will need to create a new .env file at the root of the project for global config.
|
||||
This is an example of config.
|
||||
```
|
||||
#ENVIRONMENT Defauld production
|
||||
ENVIRONMENT=development
|
||||
#WHITELIST URLS Default to http://localhost
|
||||
WHITELIST_URLS=https://someurl.com
|
||||
#GRAPHIQL Default to "false"
|
||||
GRAPHIQL=true
|
||||
#PLAYGROUND GRAPHQL Default to "false"
|
||||
PLAYGROUND_GRAPHQL=true
|
||||
# PORT EXPOSE APP Default to 4000
|
||||
PORT=4000
|
||||
```
|
||||
The default environment is production, the server-app port defauld is 4000, the default whitelist is http://localhost and the default graphiql is false.
|
||||
|
||||
### For Development
|
||||
In the terminal run:
|
||||
```
|
||||
npm run start:dev
|
||||
```
|
||||
The ENV enviroment variable should be "development" and choose the port of your preference with the enviroment variable PORT.
|
||||
|
||||
You will find the controllers on:
|
||||
```
|
||||
scr/controllers/
|
||||
```
|
||||
You will find the models on:
|
||||
```
|
||||
scr/models
|
||||
```
|
||||
You will find the GraphQL server, resolvers and schema definition on:
|
||||
```
|
||||
scr/GraphQL
|
||||
```
|
||||
|
||||
The manage of the routes for custom API you should find on:
|
||||
```
|
||||
scr/routes
|
||||
```
|
||||
|
||||
This will start the app in development mode, also use nodemon and webpack to real time coding!
|
||||
Enjoy coding!
|
||||
|
||||
### For Production
|
||||
In the terminal run:
|
||||
```
|
||||
npm run build
|
||||
```
|
||||
It will create a build folder and run:
|
||||
```
|
||||
npm start
|
||||
```
|
||||
This will start the app.
|
||||
|
||||
## Cheers
|
||||
Hope you enjoy this proyect! Sincerely Alejandro Lembke Barrientos.
|
26
Clase 6/Ejercicio/server-graphql/config/index.ts
Normal file
26
Clase 6/Ejercicio/server-graphql/config/index.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import * as dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
export const deFaultValues = {
|
||||
ENV: 'production',
|
||||
GRAPHIQL: 'false',
|
||||
PLAYGROUND_GRAPHQL: 'true',
|
||||
WHITELIST_URLS: 'http://localhost,http://localhost:4000',
|
||||
PORT: 4000,
|
||||
};
|
||||
|
||||
export const config = {
|
||||
ENV: process.env.ENV,
|
||||
GRAPHIQL: process.env.GRAPHIQL === 'true' ? true : false,
|
||||
PLAYGROUND_GRAPHQL: process.env.PLAYGROUND_GRAPHQL === 'true' ? true : false,
|
||||
WHITELIST_URLS: process.env.WHITELIST_URLS ? process.env.WHITELIST_URLS.split(',') : deFaultValues.WHITELIST_URLS,
|
||||
PORT: process.env.PORT,
|
||||
mongoDBConfig: {
|
||||
host : process.env.HOST_MONGO,
|
||||
port : process.env.PORT_MONGO,
|
||||
db : process.env.DB_MONGO,
|
||||
user: encodeURIComponent(process.env.USER_MONGO),
|
||||
password: encodeURIComponent(process.env.PASSWORD_MONGO)
|
||||
},
|
||||
};
|
6
Clase 6/Ejercicio/server-graphql/jest.config.js
Normal file
6
Clase 6/Ejercicio/server-graphql/jest.config.js
Normal file
@ -0,0 +1,6 @@
|
||||
module.exports = {
|
||||
testEnvironment: 'node',
|
||||
transform: {
|
||||
"^.+\\.ts$": "ts-jest"
|
||||
},
|
||||
};
|
8236
Clase 6/Ejercicio/server-graphql/package-lock.json
generated
Normal file
8236
Clase 6/Ejercicio/server-graphql/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
78
Clase 6/Ejercicio/server-graphql/package.json
Normal file
78
Clase 6/Ejercicio/server-graphql/package.json
Normal file
@ -0,0 +1,78 @@
|
||||
{
|
||||
"name": "server-graphql",
|
||||
"version": "1.0.0",
|
||||
"description": "Node with Typescript and GraphQL Server",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node build/index.js",
|
||||
"start:dev": "webpack-cli --config webpack.config.dev.ts",
|
||||
"start:nodemon": "nodemon build/index.js",
|
||||
"build": "webpack-cli --config webpack.config.ts",
|
||||
"lint": "eslint ./ --ext .js --ext .ts",
|
||||
"lint:fix": "eslint ./ --ext .js --ext .ts --fix",
|
||||
"test": "jest",
|
||||
"test:watch": "jest --watch"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/aleleba/node-ts-graphql-server.git"
|
||||
},
|
||||
"keywords": [
|
||||
"node",
|
||||
"express",
|
||||
"typescript",
|
||||
"graphql",
|
||||
"server"
|
||||
],
|
||||
"author": "Alejandro Lembke Barrientos",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/aleleba/node-ts-graphql-server/issues"
|
||||
},
|
||||
"homepage": "https://github.com/aleleba/node-ts-graphql-server#readme",
|
||||
"dependencies": {
|
||||
"@graphql-tools/schema": "^9.0.4",
|
||||
"body-parser": "^1.20.0",
|
||||
"cookie-parser": "^1.4.6",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.0.2",
|
||||
"express": "^4.18.1",
|
||||
"express-graphql": "^0.12.0",
|
||||
"graphql": "^16.6.0",
|
||||
"graphql-playground-middleware-express": "^1.7.23",
|
||||
"graphql-subscriptions": "^2.0.0",
|
||||
"graphql-tools": "^8.3.6",
|
||||
"graphql-ws": "^5.10.2",
|
||||
"mongodb": "^4.9.1",
|
||||
"web-push": "^3.5.0",
|
||||
"ws": "^8.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.19.1",
|
||||
"@babel/preset-env": "^7.19.1",
|
||||
"@babel/preset-typescript": "^7.18.6",
|
||||
"@babel/register": "^7.18.9",
|
||||
"@types/jest": "^29.0.2",
|
||||
"@types/node": "^18.7.18",
|
||||
"@types/webpack": "^5.28.0",
|
||||
"@types/webpack-node-externals": "^2.5.3",
|
||||
"@typescript-eslint/eslint-plugin": "^5.37.0",
|
||||
"@typescript-eslint/parser": "^5.37.0",
|
||||
"babel-loader": "^8.2.5",
|
||||
"clean-webpack-plugin": "^4.0.0",
|
||||
"compression-webpack-plugin": "^10.0.0",
|
||||
"eslint": "^8.23.1",
|
||||
"eslint-webpack-plugin": "^3.2.0",
|
||||
"jest": "^29.0.3",
|
||||
"nodemon": "^2.0.19",
|
||||
"supertest": "^6.2.4",
|
||||
"ts-jest": "^29.0.1",
|
||||
"ts-loader": "^9.3.1",
|
||||
"typescript": "^4.8.3",
|
||||
"webpack": "^5.74.0",
|
||||
"webpack-cli": "^4.10.0",
|
||||
"webpack-manifest-plugin": "^5.0.0",
|
||||
"webpack-node-externals": "^3.0.0",
|
||||
"webpack-shell-plugin-next": "^2.2.2"
|
||||
}
|
||||
}
|
5
Clase 6/Ejercicio/server-graphql/src/@types/custom.d.ts
vendored
Normal file
5
Clase 6/Ejercicio/server-graphql/src/@types/custom.d.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
// index.d.ts
|
||||
declare module "*.gql" {
|
||||
const content: any;
|
||||
export default content;
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
import { createProductoController, deleteProductoController, getProductosController } from '../../controllers/controllerGraphQL';
|
||||
|
||||
// A map of functions which return data for the schema.
|
||||
const resolvers = {
|
||||
Query: {
|
||||
// eslint-disable-next-line
|
||||
productos: (rootValue, args, context) => getProductosController(),
|
||||
},
|
||||
Mutation: {
|
||||
// eslint-disable-next-line
|
||||
productosMutation: (rootValue, args, context) => ({}),
|
||||
},
|
||||
ProductosMutation: {
|
||||
insertProducto: (rootValue, args) => createProductoController(rootValue, args),
|
||||
deleteProducto: (rootValue, args) => deleteProductoController(rootValue, args)
|
||||
}
|
||||
};
|
||||
|
||||
export default resolvers;
|
@ -0,0 +1,24 @@
|
||||
module.exports = `
|
||||
|
||||
"""Producto Query"""
|
||||
type Producto {
|
||||
id: ID
|
||||
nombre: String
|
||||
}
|
||||
|
||||
type InsertProductoRes {
|
||||
producto: Producto
|
||||
mensaje: String
|
||||
}
|
||||
|
||||
input ProductoInput{
|
||||
nombre: String
|
||||
}
|
||||
|
||||
"""Esta es la Data de LogIn, Si los datos no son correctos devuelve el usuario Null y la conexion en False"""
|
||||
type ProductosMutation {
|
||||
insertProducto(producto: ProductoInput): InsertProductoRes
|
||||
deleteProducto(id: ID): String
|
||||
}
|
||||
|
||||
`
|
17
Clase 6/Ejercicio/server-graphql/src/GraphQL/schema/index.ts
Normal file
17
Clase 6/Ejercicio/server-graphql/src/GraphQL/schema/index.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { makeExecutableSchema } from '@graphql-tools/schema';
|
||||
import resolvers from'../resolvers';
|
||||
import Productos from './Productos.gql';
|
||||
|
||||
// The GraphQL schema
|
||||
const rootTypes = `
|
||||
type Query {
|
||||
productos: [Producto]
|
||||
}
|
||||
type Mutation {
|
||||
productosMutation: ProductosMutation
|
||||
}
|
||||
`;
|
||||
|
||||
const typeDefs = [ rootTypes, Productos ];
|
||||
|
||||
export default makeExecutableSchema({typeDefs, resolvers});
|
30
Clase 6/Ejercicio/server-graphql/src/GraphQL/server.ts
Normal file
30
Clase 6/Ejercicio/server-graphql/src/GraphQL/server.ts
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
import express from 'express'; //express
|
||||
import { graphqlHTTP } from 'express-graphql';
|
||||
import { config } from '../../config';
|
||||
import schema from './schema';
|
||||
|
||||
const server = express.Router();//Router de Express
|
||||
|
||||
server.use(
|
||||
'/',
|
||||
graphqlHTTP( (req, res) => {
|
||||
return {
|
||||
schema,
|
||||
graphiql: config.GRAPHIQL,
|
||||
context: { req, res }
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
|
||||
// DO NOT DO app.listen() unless we're testing this directly
|
||||
if (require.main === module) {
|
||||
server.listen((process.env.PORT || 4000), () => {
|
||||
console.log(`Iniciando Express en el puerto 4000${server.graphqlPath}`); /*${app.get('port')}*/
|
||||
});
|
||||
}
|
||||
|
||||
// Instead do export the app:
|
||||
export default server;
|
@ -0,0 +1,124 @@
|
||||
'use strict';
|
||||
|
||||
import { ObjectID } from 'mongodb';
|
||||
import ProductosModel from '../../models';
|
||||
|
||||
const { insertProducto, getAllProductos, deleteProducto } = ProductosModel;
|
||||
|
||||
//Funcion para insertar un Producto en MongoDB
|
||||
export const createProductoController = async (rootValue, args) => {
|
||||
|
||||
const producto = args.producto;
|
||||
|
||||
const promise = new Promise((resolve) => {
|
||||
|
||||
const cb = (res, disconnect) => {
|
||||
//assert.equal(err, null);
|
||||
//console.log("Found the following records");
|
||||
|
||||
console.log(res.insertedId.toString());
|
||||
|
||||
resolve({
|
||||
producto: {
|
||||
id: res.insertedId.toString(),
|
||||
...producto
|
||||
},
|
||||
mensaje: 'Se inserto exitosamente el Producto'
|
||||
});
|
||||
|
||||
//console.log(res)
|
||||
|
||||
disconnect();
|
||||
|
||||
};
|
||||
|
||||
insertProducto(producto, cb);
|
||||
|
||||
});
|
||||
|
||||
const result = await promise; // wait till the promise resolves (*)
|
||||
|
||||
//console.log(result); // "done!"
|
||||
|
||||
return result;
|
||||
|
||||
};
|
||||
//Termina Funcion para insertar un Producto en MongoDB
|
||||
|
||||
//Funcion para traer todos los Productos
|
||||
export const getProductosController = async () => {
|
||||
|
||||
const promise = new Promise((resolve) => {
|
||||
|
||||
const cb = (err, res, disconnect) => {
|
||||
if(err){
|
||||
new Error('No hay registros de Productos');
|
||||
|
||||
disconnect();
|
||||
|
||||
}else{
|
||||
|
||||
const data = res.map( producto => {
|
||||
|
||||
const productoReturn = {
|
||||
...producto,
|
||||
id: producto._id,
|
||||
};
|
||||
|
||||
delete productoReturn['_id'];
|
||||
|
||||
return productoReturn;
|
||||
|
||||
});
|
||||
|
||||
resolve(data);
|
||||
|
||||
disconnect();
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
getAllProductos(cb);
|
||||
|
||||
});
|
||||
|
||||
const result = await promise; // wait till the promise resolves (*)
|
||||
|
||||
//console.log(result); // "done!"
|
||||
|
||||
return result;
|
||||
};
|
||||
//Termina Funcion para traer todos los Productos
|
||||
|
||||
//Funcion para Eliminar un Objetivo en MongoDB
|
||||
export const deleteProductoController = async (rootValue, args) => {
|
||||
|
||||
const id = args.id;
|
||||
|
||||
const promise = new Promise((resolve) => {
|
||||
|
||||
const cb = (res, disconnect) => {
|
||||
//console.log(res)
|
||||
if (res) {
|
||||
//assert.equal(1, res.deletedCount);
|
||||
resolve(`Se eliminó exitosamente el Producto con id: ${id}`);
|
||||
disconnect();
|
||||
} else {
|
||||
resolve('No se eliminó el Producto');
|
||||
disconnect();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
deleteProducto(cb, new ObjectID(id));
|
||||
|
||||
});
|
||||
|
||||
const result = await promise; // wait till the promise resolves (*)
|
||||
|
||||
//console.log(result); // "done!"
|
||||
|
||||
return result;
|
||||
|
||||
};
|
||||
//Termina Funcion para Eliminar un Objetivo en MongoDB
|
84
Clase 6/Ejercicio/server-graphql/src/index.ts
Normal file
84
Clase 6/Ejercicio/server-graphql/src/index.ts
Normal file
@ -0,0 +1,84 @@
|
||||
'use strict';
|
||||
|
||||
import ws from 'ws'; // yarn add ws
|
||||
import express from 'express'; //express
|
||||
import cors from 'cors';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import { useServer } from 'graphql-ws/lib/use/ws';
|
||||
import { execute, subscribe } from 'graphql';
|
||||
import GraphQLserver from './GraphQL/server';// Server of GraphQL,
|
||||
import expressPlayground from 'graphql-playground-middleware-express';
|
||||
import schema from './GraphQL/schema';
|
||||
import { config } from '../config';
|
||||
import apiRouter from './routes';
|
||||
|
||||
const app = express(), //creating app
|
||||
whitelist = config.WHITELIST_URLS,
|
||||
corsOptions = {
|
||||
origin: function (origin, callback) {
|
||||
if (whitelist.indexOf(origin) !== -1 || !origin) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
callback(new Error('Not allowed by CORS'));
|
||||
}
|
||||
},
|
||||
credentials: true
|
||||
};
|
||||
|
||||
//Inicialization of services of express
|
||||
app
|
||||
.use(cookieParser())
|
||||
.use(express.urlencoded({limit: '500mb', extended: true}))
|
||||
.use(express.json({limit: '500mb', extended: true}))
|
||||
.use(cors(corsOptions))
|
||||
.use(apiRouter)//Routes de App
|
||||
.use('/graphql', GraphQLserver);//Server of Graphql
|
||||
|
||||
if(config.PLAYGROUND_GRAPHQL === true){
|
||||
app.get('/playground', expressPlayground({ endpoint: '/graphql' }));
|
||||
}
|
||||
|
||||
// DO NOT DO app.listen() unless we're testing this directly
|
||||
if (require.main === module) {
|
||||
|
||||
const server = app.listen(config.PORT, () => {
|
||||
// create and use the websocket server
|
||||
const wsServer = new ws.Server({
|
||||
server,
|
||||
path: '/graphql',
|
||||
});
|
||||
|
||||
useServer({
|
||||
schema,
|
||||
execute,
|
||||
subscribe,
|
||||
// eslint-disable-next-line
|
||||
onConnect: (ctx) => {
|
||||
//console.log('Connect');
|
||||
},
|
||||
// eslint-disable-next-line
|
||||
onSubscribe: (ctx, msg) => {
|
||||
//console.log('Subscribe');
|
||||
},
|
||||
// eslint-disable-next-line
|
||||
onNext: (ctx, msg, args, result) => {
|
||||
//console.debug('Next');
|
||||
},
|
||||
// eslint-disable-next-line
|
||||
onError: (ctx, msg, errors) => {
|
||||
//console.error('Error');
|
||||
},
|
||||
// eslint-disable-next-line
|
||||
onComplete: (ctx, msg) => {
|
||||
//console.log('Complete');
|
||||
},
|
||||
}, wsServer);
|
||||
|
||||
console.log(`Starting Express on port ${config.PORT} and iniciating server of web sockets`);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// Instead do export the app:
|
||||
export default app;
|
@ -0,0 +1,38 @@
|
||||
/* eslint-disable */
|
||||
'use strict';
|
||||
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { config } from '../../../../config';
|
||||
|
||||
// Connection URL
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
const url = `mongodb:\/\/${config.mongoDBConfig.user}:${config.mongoDBConfig.password}@${config.mongoDBConfig.host}:${parseInt(config.mongoDBConfig.port)}`;
|
||||
const dbName = config.mongoDBConfig.db;
|
||||
|
||||
async function connect(cb) {
|
||||
|
||||
const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true })
|
||||
|
||||
try{
|
||||
await client.connect();
|
||||
console.log('conexion exitosa con mongoDB');
|
||||
|
||||
const db = client.db(dbName);
|
||||
|
||||
const disconnect = () => {
|
||||
// Close connection
|
||||
client.close( ()=>{
|
||||
console.log('desconexion exitosa con mongoDB');
|
||||
});
|
||||
};
|
||||
|
||||
await cb(db, disconnect);
|
||||
|
||||
}catch(err){
|
||||
console.log(err.stack);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default connect;
|
||||
/* eslint-enable */
|
@ -0,0 +1,51 @@
|
||||
'use strict';
|
||||
|
||||
import conn from './config/apiMongoDB-connection';
|
||||
|
||||
export const insertProducto = (data, cb) => {
|
||||
|
||||
async function callback (db, disconnect) {
|
||||
// Insert a single document
|
||||
await db.collection('Productos')
|
||||
.insertOne(data)
|
||||
.then( res => cb(res, disconnect));
|
||||
}
|
||||
|
||||
conn(callback);
|
||||
|
||||
};
|
||||
|
||||
export const getAllProductos = (cb) => {
|
||||
|
||||
async function callback(db, disconnect) {
|
||||
|
||||
// Insert a single document
|
||||
await db.collection('Productos')
|
||||
.find()
|
||||
.limit(0)
|
||||
.toArray( (err, res) => {
|
||||
cb(err, res, disconnect);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
conn(callback);
|
||||
|
||||
};
|
||||
|
||||
export const deleteProducto = (cb, id) => {
|
||||
|
||||
async function callback (db, disconnect) {
|
||||
|
||||
// Insert a single document
|
||||
await db.collection('Productos')
|
||||
.deleteOne({ _id: id })
|
||||
.then( res => cb(res, disconnect));
|
||||
|
||||
//.updateOne({ _id: data.id }, { $set: { estado: data.estado} })
|
||||
|
||||
}
|
||||
|
||||
conn(callback);
|
||||
|
||||
};
|
5
Clase 6/Ejercicio/server-graphql/src/models/index.ts
Normal file
5
Clase 6/Ejercicio/server-graphql/src/models/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
import { insertProducto, getAllProductos, deleteProducto } from './apiMongoDBModel';
|
||||
|
||||
export default { insertProducto, getAllProductos, deleteProducto };
|
13
Clase 6/Ejercicio/server-graphql/src/routes/index.ts
Normal file
13
Clase 6/Ejercicio/server-graphql/src/routes/index.ts
Normal file
@ -0,0 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
// use this to set API REST
|
||||
import express from 'express';
|
||||
import bodyParser from 'body-parser';//bodyParser conversionde Api REST,
|
||||
|
||||
const apiRouter = express.Router();//Router de Express
|
||||
|
||||
apiRouter
|
||||
.use(bodyParser.json())
|
||||
.use(bodyParser.urlencoded({extended: false}));
|
||||
|
||||
export default apiRouter;
|
11
Clase 6/Ejercicio/server-graphql/tsconfig.json
Normal file
11
Clase 6/Ejercicio/server-graphql/tsconfig.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true,
|
||||
"target": "es6",
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": true,
|
||||
"typeRoots" : ["./src/@types", "./node_modules/@types"],
|
||||
},
|
||||
"lib": ["es2015"]
|
||||
}
|
64
Clase 6/Ejercicio/server-graphql/webpack.config.dev.ts
Normal file
64
Clase 6/Ejercicio/server-graphql/webpack.config.dev.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import path from 'path';
|
||||
import webpack from 'webpack';
|
||||
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
|
||||
import ESLintPlugin from 'eslint-webpack-plugin';
|
||||
import nodeExternals from 'webpack-node-externals';
|
||||
import WebpackShellPluginNext from 'webpack-shell-plugin-next';
|
||||
import { deFaultValues } from './config';
|
||||
|
||||
const ROOT_DIR = path.resolve(__dirname);
|
||||
const resolvePath = (...args) => path.resolve(ROOT_DIR, ...args);
|
||||
const BUILD_DIR = resolvePath('build');
|
||||
|
||||
const config = {
|
||||
entry: './src/index.ts',
|
||||
target: 'node',
|
||||
watch: true,
|
||||
externals: [nodeExternals()],
|
||||
output: {
|
||||
path: BUILD_DIR,
|
||||
filename: 'index.js',
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.js', '.ts', '.json', '.gql'],
|
||||
alias: {
|
||||
'@controllers': path.resolve(__dirname, 'controllers/'),
|
||||
'@models': path.resolve(__dirname, 'models/'),
|
||||
'@controllerGraphQL': path.resolve(__dirname, 'controllers/controllerGraphQL/'),
|
||||
'@GraphQL': path.resolve(__dirname, 'GraphQL/'),
|
||||
'@config': path.resolve(__dirname, 'config/'),
|
||||
}
|
||||
},
|
||||
mode: 'development',
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.(js|ts|mjs|gql)$/,
|
||||
exclude: /node_modules/,
|
||||
use: {
|
||||
loader: 'babel-loader',
|
||||
},
|
||||
},
|
||||
{
|
||||
test: /\.(ts)$/, loader: 'ts-loader',
|
||||
exclude: /node_modules/
|
||||
},
|
||||
],
|
||||
},
|
||||
plugins: [
|
||||
new CleanWebpackPlugin(),
|
||||
new ESLintPlugin(),
|
||||
new webpack.EnvironmentPlugin({
|
||||
...deFaultValues
|
||||
}),
|
||||
new WebpackShellPluginNext({
|
||||
onBuildEnd: {
|
||||
scripts: ['nodemon build/index.js'],
|
||||
blocking: false,
|
||||
parallel: true
|
||||
}
|
||||
})
|
||||
],
|
||||
};
|
||||
|
||||
export default config;
|
62
Clase 6/Ejercicio/server-graphql/webpack.config.ts
Normal file
62
Clase 6/Ejercicio/server-graphql/webpack.config.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import path from 'path';
|
||||
import webpack from 'webpack';
|
||||
import TerserPlugin from 'terser-webpack-plugin';
|
||||
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
|
||||
import ESLintPlugin from 'eslint-webpack-plugin';
|
||||
import nodeExternals from 'webpack-node-externals';
|
||||
import { deFaultValues } from './config';
|
||||
|
||||
const ROOT_DIR = path.resolve(__dirname);
|
||||
const resolvePath = (...args) => path.resolve(ROOT_DIR, ...args);
|
||||
const BUILD_DIR = resolvePath('build');
|
||||
|
||||
const config = {
|
||||
entry: './src/index.ts',
|
||||
target: 'node',
|
||||
externals: [nodeExternals()],
|
||||
output: {
|
||||
path: BUILD_DIR,
|
||||
filename: 'index.js',
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.js', '.ts', '.json', '.gql'],
|
||||
alias: {
|
||||
'@controllers': path.resolve(__dirname, 'controllers/'),
|
||||
'@models': path.resolve(__dirname, 'models/'),
|
||||
'@controllerGraphQL': path.resolve(__dirname, 'controllers/controllerGraphQL/'),
|
||||
'@GraphQL': path.resolve(__dirname, 'GraphQL/'),
|
||||
'@config': path.resolve(__dirname, 'config/'),
|
||||
}
|
||||
},
|
||||
mode: 'production',
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.(js|ts|mjs|gql)$/,
|
||||
exclude: /node_modules/,
|
||||
use: {
|
||||
loader: 'babel-loader',
|
||||
},
|
||||
},
|
||||
{
|
||||
test: /\.(ts)$/, loader: "ts-loader",
|
||||
exclude: /node_modules/
|
||||
},
|
||||
],
|
||||
},
|
||||
plugins: [
|
||||
new CleanWebpackPlugin(),
|
||||
new ESLintPlugin(),
|
||||
new webpack.EnvironmentPlugin({
|
||||
...deFaultValues
|
||||
}),
|
||||
],
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new TerserPlugin(),
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
export default config;
|
4
Clase 6/README.md
Normal file
4
Clase 6/README.md
Normal file
@ -0,0 +1,4 @@
|
||||
# Clase 6
|
||||
## Repaso General con Ejercicio
|
||||
|
||||
![Diap4](../img/Agradecimiento.png)
|
Loading…
Reference in New Issue
Block a user