mirror of
https://github.com/aleleba/create-node-ts-graphql-server.git
synced 2025-01-08 04:56:46 -06:00
PR-448199:
Adding files from first version. Creating GraphQL server with their controllers and their models, squema and resolvers. Adding support to webpack and typescript.
This commit is contained in:
parent
4b9abd6aac
commit
b55b0f61a9
6
.babelrc
Normal file
6
.babelrc
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"presets": [
|
||||
"@babel/preset-env",
|
||||
"@babel/preset-typescript"
|
||||
]
|
||||
}
|
11
.env.example
Normal file
11
.env.example
Normal file
@ -0,0 +1,11 @@
|
||||
#ENVIRONMENT Defauld production
|
||||
ENVIRONMENT=
|
||||
|
||||
#WHITELIST URLS Default to http://localhost
|
||||
WHITELIST_URLS=
|
||||
|
||||
#GRAPHIQL Default to "false"
|
||||
GRAPHIQL=
|
||||
|
||||
# PORT EXPOSE APP Default to 4000
|
||||
PORT=
|
7
.eslintignore
Normal file
7
.eslintignore
Normal file
@ -0,0 +1,7 @@
|
||||
#Eslint
|
||||
.eslintrc.js
|
||||
#Build
|
||||
build
|
||||
#Webpack
|
||||
webpack.config.ts
|
||||
webpack.config.dev.ts
|
37
.eslintrc.js
Normal file
37
.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
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
# dependencies
|
||||
/node_modules
|
||||
/build
|
||||
.env
|
5
@types/custom.d.ts
vendored
Normal file
5
@types/custom.d.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
// index.d.ts
|
||||
declare module "*.gql" {
|
||||
const content: any;
|
||||
export default content;
|
||||
}
|
23
GraphQL/resolvers/index.ts
Normal file
23
GraphQL/resolvers/index.ts
Normal file
@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
import { getTest, addText } from '../../controllers/controllerGraphQL';
|
||||
|
||||
// A map of functions which return data for the schema.
|
||||
const resolvers = {
|
||||
Query: {
|
||||
// eslint-disable-next-line
|
||||
test: (rootValue, args, context) => ({}),
|
||||
},
|
||||
Mutation: {
|
||||
// eslint-disable-next-line
|
||||
testMutation: (rootValue, args, context) => ({}),
|
||||
},
|
||||
Test: {
|
||||
test: (rootValue, args, context) => getTest(rootValue, args, context)
|
||||
},
|
||||
TestMutation: {
|
||||
testMutation: (rootValue, args, context) => addText(rootValue, args, context)
|
||||
}
|
||||
};
|
||||
|
||||
export default resolvers;
|
13
GraphQL/schema/Test.gql
Normal file
13
GraphQL/schema/Test.gql
Normal file
@ -0,0 +1,13 @@
|
||||
module.exports = `
|
||||
|
||||
"""Test Query"""
|
||||
type Test {
|
||||
test: String
|
||||
}
|
||||
|
||||
"""Esta es la Data de LogIn, Si los datos no son correctos devuelve el usuario Null y la conexion en False"""
|
||||
type TestMutation {
|
||||
testMutation(text: String): String
|
||||
}
|
||||
|
||||
`
|
17
GraphQL/schema/index.ts
Normal file
17
GraphQL/schema/index.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { makeExecutableSchema } from '@graphql-tools/schema';
|
||||
import resolvers from'../resolvers';
|
||||
import Test from './Test.gql';
|
||||
|
||||
// The GraphQL schema
|
||||
const rootTypes = `
|
||||
type Query {
|
||||
test: Test
|
||||
}
|
||||
type Mutation {
|
||||
testMutation: TestMutation
|
||||
}
|
||||
`;
|
||||
|
||||
const typeDefs = [ rootTypes, Test ];
|
||||
|
||||
export default makeExecutableSchema({typeDefs, resolvers});
|
30
GraphQL/server.ts
Normal file
30
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;
|
11
PRNameGenerator.ts
Normal file
11
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());
|
12
config/index.ts
Normal file
12
config/index.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import * as dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
export const config = {
|
||||
env: process.env.ENVIRONMENT ? process.env.ENVIRONMENT : 'production',
|
||||
graphiQL: process.env.GRAPHIQL === 'true' ? true : false,
|
||||
whiteList: process.env.WHITELIST_URLS ? process.env.WHITELIST_URLS.split(',') : [
|
||||
'http://localhost'
|
||||
],
|
||||
port: process.env.PORT || 4000,
|
||||
};
|
14
controllers/controllerGraphQL/index.ts
Normal file
14
controllers/controllerGraphQL/index.ts
Normal file
@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
import { getTestModel, addTextModel } from '../../models';
|
||||
|
||||
// eslint-disable-next-line
|
||||
export const getTest = async (rootValue, args, context) => {
|
||||
return getTestModel();
|
||||
};
|
||||
|
||||
// eslint-disable-next-line
|
||||
export const addText = async (rootValue, args, context) => {
|
||||
const text = args.text;
|
||||
return addTextModel({ text });
|
||||
};
|
80
index.ts
Normal file
80
index.ts
Normal file
@ -0,0 +1,80 @@
|
||||
'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 schema from './GraphQL/schema';
|
||||
import { config } from './config';
|
||||
import apiRouter from './routes';
|
||||
|
||||
|
||||
const app = express(), //creating app
|
||||
whitelist = config.whiteList,
|
||||
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
|
||||
|
||||
// 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;
|
9
models/index.ts
Normal file
9
models/index.ts
Normal file
@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
export const getTestModel = async () => {
|
||||
return 'This is the text response for Test Query from a model';
|
||||
};
|
||||
|
||||
export const addTextModel = async ({ text }) => {
|
||||
return `Simulate to insert some text: ${text} from a model`;
|
||||
};
|
14205
package-lock.json
generated
Normal file
14205
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
72
package.json
Normal file
72
package.json
Normal file
@ -0,0 +1,72 @@
|
||||
{
|
||||
"name": "ts-graphql-server",
|
||||
"version": "0.0.1",
|
||||
"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": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"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": "^8.3.13",
|
||||
"body-parser": "^1.20.0",
|
||||
"cookie-parser": "^1.4.6",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.0.1",
|
||||
"express": "^4.18.1",
|
||||
"express-graphql": "^0.12.0",
|
||||
"graphql": "^15.8.0",
|
||||
"graphql-subscriptions": "^2.0.0",
|
||||
"graphql-tools": "^8.2.11",
|
||||
"graphql-ws": "^5.8.2",
|
||||
"web-push": "^3.5.0",
|
||||
"ws": "^8.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.18.2",
|
||||
"@babel/preset-env": "^7.18.2",
|
||||
"@babel/preset-typescript": "^7.17.12",
|
||||
"@babel/register": "^7.17.7",
|
||||
"@types/jest": "^27.5.1",
|
||||
"@types/node": "^17.0.35",
|
||||
"@types/webpack": "^5.28.0",
|
||||
"@types/webpack-node-externals": "^2.5.3",
|
||||
"@typescript-eslint/eslint-plugin": "^5.26.0",
|
||||
"@typescript-eslint/parser": "^5.26.0",
|
||||
"babel-loader": "^8.2.5",
|
||||
"clean-webpack-plugin": "^4.0.0",
|
||||
"compression-webpack-plugin": "^10.0.0",
|
||||
"eslint": "^8.16.0",
|
||||
"eslint-webpack-plugin": "^3.1.1",
|
||||
"nodemon": "^2.0.16",
|
||||
"ts-loader": "^9.3.0",
|
||||
"typescript": "^4.7.2",
|
||||
"webpack": "^5.72.1",
|
||||
"webpack-cli": "^4.9.2",
|
||||
"webpack-manifest-plugin": "^5.0.0",
|
||||
"webpack-node-externals": "^3.0.0",
|
||||
"webpack-shell-plugin-next": "^2.2.2"
|
||||
}
|
||||
}
|
13
routes/index.ts
Normal file
13
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
tsconfig.json
Normal file
11
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"]
|
||||
}
|
66
webpack.config.dev.ts
Normal file
66
webpack.config.dev.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import path from 'path';
|
||||
import * as dotenv from 'dotenv';
|
||||
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';
|
||||
|
||||
const dotEnvToParse = dotenv.config();
|
||||
|
||||
const ROOT_DIR = path.resolve(__dirname);
|
||||
const resolvePath = (...args) => path.resolve(ROOT_DIR, ...args);
|
||||
const BUILD_DIR = resolvePath('build');
|
||||
|
||||
const config = {
|
||||
entry: './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.DefinePlugin({
|
||||
'process.env': JSON.stringify(dotEnvToParse.parsed),
|
||||
}),
|
||||
new WebpackShellPluginNext({
|
||||
onBuildEnd: {
|
||||
scripts: ['npm run start:nodemon'],
|
||||
blocking: false,
|
||||
parallel: true
|
||||
}
|
||||
})
|
||||
],
|
||||
};
|
||||
|
||||
export default config;
|
64
webpack.config.ts
Normal file
64
webpack.config.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import path from 'path';
|
||||
import * as dotenv from 'dotenv';
|
||||
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';
|
||||
|
||||
const dotEnvToParse = dotenv.config();
|
||||
|
||||
const ROOT_DIR = path.resolve(__dirname);
|
||||
const resolvePath = (...args) => path.resolve(ROOT_DIR, ...args);
|
||||
const BUILD_DIR = resolvePath('build');
|
||||
|
||||
const config = {
|
||||
entry: './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.DefinePlugin({
|
||||
'process.env': JSON.stringify(dotEnvToParse.parsed),
|
||||
}),
|
||||
],
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new TerserPlugin(),
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
export default config;
|
Loading…
Reference in New Issue
Block a user