PR-448199:

Moving Server to src directory.
This commit is contained in:
2022-05-25 23:48:42 +00:00
parent e9bc4f091f
commit 2b698e785a
13 changed files with 7 additions and 4 deletions

5
src/@types/custom.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
// index.d.ts
declare module "*.gql" {
const content: any;
export default content;
}

View 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;

View 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
}
`

View 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
View 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;

View 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
View 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
View 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
View 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;

View 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);
});
});