mirror of
https://github.com/aleleba/create-node-ts-graphql-server.git
synced 2025-01-09 21:46:48 -06:00
Merge pull request #1 from aleleba/PR-448199
PR-448199: Adding files from first version.
This commit is contained in:
commit
317f53efa2
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
|
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"typescript.tsdk": "node_modules/typescript/lib"
|
||||||
|
}
|
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());
|
73
README.md
Normal file
73
README.md
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
# 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) | 28.1.0
|
||||||
|
Typescript | 4.7.2
|
||||||
|
|
||||||
|
## 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://dev-back-end.p-lao.com
|
||||||
|
#GRAPHIQL Default to "false"
|
||||||
|
GRAPHIQL=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.
|
29
bin/cli.js
Normal file
29
bin/cli.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
const { execSync } = require('child_process');
|
||||||
|
|
||||||
|
const runCommand = command => {
|
||||||
|
try{
|
||||||
|
execSync(`${command}`, {stdio: 'inherit'});
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`Failed to execute ${command}`, e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const repoName = process.argv[2];
|
||||||
|
const gitCheckoutCommand = `git clone --depth 1 https://github.com/aleleba/create-node-ts-graphql-server ${repoName}`;
|
||||||
|
const installDepsCommand = `cd ${repoName} && npm install`;
|
||||||
|
|
||||||
|
console.log(`Cloning the repository with name ${repoName}`);
|
||||||
|
const checkedOut = runCommand(gitCheckoutCommand);
|
||||||
|
if(!checkedOut) process.exit(-1);
|
||||||
|
|
||||||
|
console.log(`Installing dependencies for ${repoName}`);
|
||||||
|
const installedDeps = runCommand(installDepsCommand);
|
||||||
|
if(!installedDeps) process.exit(-1);
|
||||||
|
|
||||||
|
console.log("Congratulations! You are ready. Follow the following commands to start");
|
||||||
|
console.log(`cd ${repoName}`);
|
||||||
|
console.log('Create a .env file with ENV=development(defauld: production), PORT=4000 (default: 4000), WHITELIST_URLS=your_url(default: http://localhost), GRAPHIQL=true(default: false)');
|
||||||
|
console.log(`Then you can run: npm start:dev`);
|
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,
|
||||||
|
};
|
6
jest.config.js
Normal file
6
jest.config.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
module.exports = {
|
||||||
|
testEnvironment: 'node',
|
||||||
|
transform: {
|
||||||
|
"^.+\\.ts$": "ts-jest"
|
||||||
|
},
|
||||||
|
};
|
21470
package-lock.json
generated
Normal file
21470
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
77
package.json
Normal file
77
package.json
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
{
|
||||||
|
"name": "ts-graphql-server",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Node with Typescript and GraphQL Server",
|
||||||
|
"bin": "./bin/cli.js",
|
||||||
|
"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": "^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",
|
||||||
|
"jest": "^28.1.0",
|
||||||
|
"nodemon": "^2.0.16",
|
||||||
|
"supertest": "^6.2.3",
|
||||||
|
"ts-jest": "^28.0.3",
|
||||||
|
"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"
|
||||||
|
}
|
||||||
|
}
|
5
src/@types/custom.d.ts
vendored
Normal file
5
src/@types/custom.d.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
// index.d.ts
|
||||||
|
declare module "*.gql" {
|
||||||
|
const content: any;
|
||||||
|
export default content;
|
||||||
|
}
|
23
src/GraphQL/resolvers/index.ts
Normal file
23
src/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
src/GraphQL/schema/Test.gql
Normal file
13
src/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
src/GraphQL/schema/index.ts
Normal file
17
src/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
src/GraphQL/server.ts
Normal file
30
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;
|
14
src/controllers/controllerGraphQL/index.ts
Normal file
14
src/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
src/index.ts
Normal file
80
src/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
src/models/index.ts
Normal file
9
src/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`;
|
||||||
|
};
|
13
src/routes/index.ts
Normal file
13
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;
|
36
src/tests/server/index.test.ts
Normal file
36
src/tests/server/index.test.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import server from '../../index';
|
||||||
|
import supertest from 'supertest';
|
||||||
|
describe('global server tests', () => {
|
||||||
|
let request;
|
||||||
|
beforeEach(() => {
|
||||||
|
request = supertest(server);
|
||||||
|
});
|
||||||
|
test('should return Test data from test Query', async () => {
|
||||||
|
const bodyResponse = {
|
||||||
|
data: {
|
||||||
|
test: { test: 'This is the text response for Test Query from a model' }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await request.get('/graphql?query=%7B%0A%20%20test%7B%0A%20%20%20%20test%0A%20%20%7D%0A%7D%0A')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
expect(response.status).toEqual(200);
|
||||||
|
expect(response.body).toEqual(bodyResponse);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should return Test data from test Mutation', async () => {
|
||||||
|
const bodyResponse = {
|
||||||
|
data: {
|
||||||
|
testMutation: {
|
||||||
|
testMutation: 'Simulate to insert some text: test text from a model'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await request.post('/graphql')
|
||||||
|
.send({"query":"mutation{\n testMutation{\n testMutation(text: \"test text\")\n }\n}\n","variables":null})
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
expect(response.status).toEqual(200);
|
||||||
|
expect(response.body).toEqual(bodyResponse);
|
||||||
|
});
|
||||||
|
});
|
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: './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.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: './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.DefinePlugin({
|
||||||
|
'process.env': JSON.stringify(dotEnvToParse.parsed),
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
optimization: {
|
||||||
|
minimize: true,
|
||||||
|
minimizer: [
|
||||||
|
new TerserPlugin(),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default config;
|
Loading…
Reference in New Issue
Block a user