-
Notifications
You must be signed in to change notification settings - Fork 10
/
Router.ts
156 lines (119 loc) · 3.72 KB
/
Router.ts
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
import {
IGlobalConfiguration,
IMountableItem,
ICacheEngine,
IConstructorRouteOptions,
} from '.';
import Route from './Route';
import express from 'express';
import bodyParser from 'body-parser';
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import { parse, DocumentNode, getOperationAST } from 'graphql';
import { version } from './package.json';
const DEFAULT_CONFIGURATION: IGlobalConfiguration = {
cacheEngine: undefined,
auth: undefined,
proxy: undefined,
defaultTimeoutInMs: 10000,
defaultCacheTimeInMs: 0,
autoDiscoverEndpoints: false,
optimizeQueryRequest: false,
headers: { 'x-graphql-rest-router-version': version },
};
export default class Router {
private schema: DocumentNode;
private options: IGlobalConfiguration;
public routes: Route[] = [];
public modules: IMountableItem[] = [];
public axios: AxiosInstance;
private passThroughHeaders: string[] = [];
constructor(public endpoint: string, schema: string, assignedConfiguration?: IGlobalConfiguration) {
const {
auth,
proxy,
defaultTimeoutInMs: timeout,
passThroughHeaders,
...options
} = {
...DEFAULT_CONFIGURATION,
...assignedConfiguration,
// Default headers should always override
headers: {
...(assignedConfiguration || {}).headers,
...DEFAULT_CONFIGURATION.headers,
},
};
const axiosConfig: AxiosRequestConfig = {
baseURL: endpoint,
method: 'post',
headers: options.headers,
timeout,
auth,
proxy,
responseType: 'json',
};
this.schema = parse(schema);
this.axios = axios.create(axiosConfig);
if (passThroughHeaders) {
this.passThroughHeaders = passThroughHeaders;
}
this.options = options;
}
private queryForOperation(operationName: string) {
const { schema, options } = this;
const { optimizeQueryRequest } = options;
if (optimizeQueryRequest) {
console.warn(
'optimizeQueryRequest is a beta feature. It does not work with fragments'
);
return getOperationAST(schema, operationName);
}
return schema;
}
mount(operationName: string, options?: any): Route;
mount(mountableItem: IMountableItem, options?: any): IMountableItem;
mount(operationNameOrMountableItem: string | IMountableItem, options?: any): IMountableItem {
if (typeof operationNameOrMountableItem === 'string') {
const { schema, axios } = this;
const operationName = operationNameOrMountableItem;
const passThroughHeaders = Boolean(options)
? [...this.passThroughHeaders, ...options.passThroughHeaders]
: [...this.passThroughHeaders];
const routeOptions: IConstructorRouteOptions = {
...options,
operationName,
axios,
schema,
passThroughHeaders,
};
const graphQLRoute = new Route(routeOptions);
this.routes.push(graphQLRoute);
return graphQLRoute;
}
const mountedItem = operationNameOrMountableItem.withOptions(options);
if (mountedItem.onMount) {
mountedItem.onMount(this);
}
this.modules.push(mountedItem);
return mountedItem;
}
// TODO: Temporarily using express as metal
listen(port: number, callback?: () => void) {
const router = express();
router.use(this.asExpressRouter());
router.listen(port, callback);
}
asExpressRouter() {
const router: any = express.Router();
router.use(bodyParser.json());
[...this.modules, ...this.routes].forEach((route) => {
const { path, httpMethod } = route;
const routeFn = route.asExpressRoute();
router[httpMethod](path, routeFn);
});
return router;
}
asKoaRouter() {
throw new Error('Not Implemented');
}
}