/
server.js
166 lines (123 loc) · 4.31 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import waitOn from 'wait-on';
import express from 'express';
import pkg from 'lodash';
import { ApolloServer, gql } from 'apollo-server';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { introspectSchema } from '@graphql-tools/wrap';
import { stitchSchemas } from '@graphql-tools/stitch';
import makeRemoteExecutor from './lib/make_remote_executor.js';
import makeRemoteSubscriber from './lib/make_remote_subscriber.js';
import restDatasource from './datasources/restDatasource.js';
const { RESTAPI } = restDatasource;
import { typeDef as Conf, resolvers as confResolvers } from "./types/conf.type.graphql.js";
import { typeDef as Rest, resolvers as restResolvers } from "./types/login.type.graphql.js";
const { merge } = pkg;
const fullSchema = makeExecutableSchema({
typeDefs: [ Rest, Conf],
resolvers: merge(confResolvers, restResolvers)
});
export const allRESTSchemas = { schema: fullSchema };
//build the combined schema
export const gatewayRESTSchema = stitchSchemas({
subschemas: [
allRESTSchemas
]
});
async function makeGatewaySchema() {
// Make remote executors:
// these are simple functions that query a remote GraphQL API for JSON.
const allSchemas = [];
allSchemas.push(
{ schema: gatewayRESTSchema }
);
//For local
const urls = [
{
"url": 'http://localhost:8080/graphql',
"subs": 'ws://localhost:8080/graphql'
},
{
"url": 'https://n7b67.sse.codesandbox.io/graphql',
}
]
for (const obj of urls) {
try {
const exec = makeRemoteExecutor(obj.url);
const remoteSchemas = await introspectSchema(exec);
//Si falla la conexión al protocolo http, se salta la excepción y no comprueba la subscripción
if (obj.subs) {
allSchemas.push({
schema: remoteSchemas,
executor: exec,
subscriber: makeRemoteSubscriber(obj.subs)
})
} else {
allSchemas.push({
schema: remoteSchemas,
executor: exec
})
}
} catch (err) {
console.error(` >>>> error on makeGatewaySchema: ${err} <<<<`);
}
}
//console.debug(` allSchemas ${JSON.stringify(allSchemas)}`);
return stitchSchemas({
subschemas: allSchemas,
});
}
waitOn({ resources: [''] }, async () => {
const schema = await makeGatewaySchema();
try {
const server = new ApolloServer({
schema: schema,
introspection: true,
dataSources: () => ({
authAPI: new AuthAPI()
}),
context: ({ req }) => {
if (req) { // Prevent error when using Websocket connection
//console.log(`req ${JSON.stringify(req)}`);
return {
"authorization": req.headers.authorization || '',
"accept-encoding": req.headers["accept-encoding"] || '',
"accept-language": req.headers["accept-language"] || ''
};
} else {
return {
"ws": "ApolloLink" // Only trace pourposes
}
}
},
formatError: (err) => {
console.log(`err ${err}`);
// Don't give the specific errors to the client.
if (err.extensions && err.extensions.exception) {
err.extensions.exception.stacktrace = null;
}
if (err.extensions && err.extensions.response && err.extensions.response.body && err.extensions.response.body.error_description &&
err.extensions.response.body.error_description === 'Bad credentials') {
if (err.extensions && err.extensions.code) {
err.extensions.code = 'UNAUTHENTICATED';
}
}
// Otherwise return the original error. The error can also
// be manipulated in other ways, so long as it's returned.
return err;
},
plugins: [
{
async serverWillStart() {
console.log('Server Starting!');
}
}
]
});
server.listen(4000).then(() => {
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
console.log(`🚀 Subscription endpoint ready at ws://localhost:4000${server.graphqlPath}`);
});
} catch (error) {
console.error("error schema" + error);
}
});