@common-stack/generate-plugin 6.0.1-alpha.9 → 6.0.2-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/.eslintrc.json +1 -1
  2. package/CHANGELOG.md +12 -0
  3. package/README.md +280 -10
  4. package/cde-config.json +86 -0
  5. package/executors.json +14 -0
  6. package/generators.json +15 -0
  7. package/lib/constants/index.cjs +2 -1
  8. package/lib/constants/index.cjs.map +1 -1
  9. package/lib/constants/index.d.ts +1 -0
  10. package/lib/constants/index.mjs +2 -1
  11. package/lib/constants/index.mjs.map +1 -1
  12. package/lib/executors/sort-package-json/executor.cjs +46 -0
  13. package/lib/executors/sort-package-json/executor.cjs.map +1 -0
  14. package/lib/executors/sort-package-json/executor.d.ts +4 -0
  15. package/lib/executors/sort-package-json/executor.mjs +46 -0
  16. package/lib/executors/sort-package-json/executor.mjs.map +1 -0
  17. package/lib/executors/sort-package-json/executor.spec.d.ts +1 -0
  18. package/lib/executors/sort-package-json/schema.json +9 -0
  19. package/lib/executors/update-deploy-version/executor.cjs +22 -0
  20. package/lib/executors/update-deploy-version/executor.cjs.map +1 -0
  21. package/lib/executors/update-deploy-version/executor.d.ts +4 -0
  22. package/lib/executors/update-deploy-version/executor.mjs +22 -0
  23. package/lib/executors/update-deploy-version/executor.mjs.map +1 -0
  24. package/lib/executors/update-deploy-version/executor.spec.d.ts +1 -0
  25. package/lib/executors/update-deploy-version/schema.json +14 -0
  26. package/lib/generators/add-backend/files/CHANGELOG.md +0 -0
  27. package/lib/generators/add-backend/files/Dockerfile +1 -1
  28. package/lib/generators/add-backend/files/package.json +1 -18
  29. package/lib/generators/add-backend/files/tsconfig.json +1 -1
  30. package/lib/generators/add-backend/files/webpack.config.js +6 -10
  31. package/lib/generators/add-backend/generator.cjs +21 -17
  32. package/lib/generators/add-backend/generator.cjs.map +1 -1
  33. package/lib/generators/add-backend/generator.d.ts +1 -0
  34. package/lib/generators/add-backend/generator.mjs +21 -17
  35. package/lib/generators/add-backend/generator.mjs.map +1 -1
  36. package/lib/generators/add-browser-package/files/package.json +2 -2
  37. package/lib/generators/add-browser-package/generator.cjs +14 -16
  38. package/lib/generators/add-browser-package/generator.cjs.map +1 -1
  39. package/lib/generators/add-browser-package/generator.d.ts +1 -0
  40. package/lib/generators/add-browser-package/generator.mjs +14 -16
  41. package/lib/generators/add-browser-package/generator.mjs.map +1 -1
  42. package/lib/generators/add-client-package/files/CHANGELOG.md +807 -0
  43. package/lib/generators/add-client-package/files/LICENSE +674 -0
  44. package/lib/generators/add-client-package/files/jest.config.js +13 -0
  45. package/lib/generators/add-client-package/files/package.json +30 -0
  46. package/lib/generators/add-client-package/files/rollup.config.mjs +29 -0
  47. package/lib/generators/add-client-package/files/src/components/index.tsx.template +1 -0
  48. package/lib/generators/add-client-package/files/src/index.ts.template +1 -0
  49. package/lib/generators/add-client-package/files/tsconfig.json +27 -0
  50. package/lib/generators/add-client-package/files/webpack.config.js +72 -0
  51. package/lib/generators/add-client-package/generator.cjs +17 -0
  52. package/lib/generators/add-client-package/generator.cjs.map +1 -0
  53. package/lib/generators/add-client-package/generator.d.ts +5 -0
  54. package/lib/generators/add-client-package/generator.mjs +17 -0
  55. package/lib/generators/add-client-package/generator.mjs.map +1 -0
  56. package/lib/generators/add-client-package/generator.spec.d.ts +1 -0
  57. package/lib/generators/add-client-package/schema.json +19 -0
  58. package/lib/generators/add-core-package/files/CHANGELOG.md +1076 -0
  59. package/lib/generators/add-core-package/files/LICENSE +39 -0
  60. package/lib/generators/add-core-package/files/jest.config.js +3 -0
  61. package/lib/generators/add-core-package/files/package.json +30 -0
  62. package/lib/generators/add-core-package/files/rollup.config.mjs +65 -0
  63. package/lib/generators/add-core-package/files/src/constants/index.ts.template +1 -0
  64. package/lib/generators/add-core-package/files/src/constants/types.ts.template +3 -0
  65. package/lib/generators/add-core-package/files/src/index.ts.template +2 -0
  66. package/lib/generators/add-core-package/files/src/types/index.ts.template +1 -0
  67. package/lib/generators/add-core-package/files/src/types/types.ts.template +17 -0
  68. package/lib/generators/add-core-package/files/tsconfig.json +22 -0
  69. package/lib/generators/add-core-package/files/webpack.config.js +53 -0
  70. package/lib/generators/add-core-package/generator.cjs +17 -0
  71. package/lib/generators/add-core-package/generator.cjs.map +1 -0
  72. package/lib/generators/add-core-package/generator.d.ts +5 -0
  73. package/lib/generators/add-core-package/generator.mjs +17 -0
  74. package/lib/generators/add-core-package/generator.mjs.map +1 -0
  75. package/lib/generators/add-core-package/generator.spec.d.ts +1 -0
  76. package/lib/generators/add-core-package/schema.json +19 -0
  77. package/lib/generators/add-frontend/frameworks/tailwindui/entry.client.tsx.template +90 -0
  78. package/lib/generators/add-frontend/frameworks/tailwindui/entry.server.tsx.template +238 -0
  79. package/lib/generators/add-frontend/frameworks/tailwindui/root.tsx.template +117 -0
  80. package/lib/generators/add-frontend/frameworks/tailwindui/tailwind.css +3 -0
  81. package/lib/generators/add-frontend/generator.cjs +60 -42
  82. package/lib/generators/add-frontend/generator.cjs.map +1 -1
  83. package/lib/generators/add-frontend/generator.d.ts +1 -0
  84. package/lib/generators/add-frontend/generator.mjs +60 -42
  85. package/lib/generators/add-frontend/generator.mjs.map +1 -1
  86. package/lib/generators/add-frontend/schema.json +1 -1
  87. package/lib/generators/add-frontend/templates/package.json +18 -76
  88. package/lib/generators/add-frontend/templates/postcss.config.js +6 -0
  89. package/lib/generators/add-frontend/templates/tailwind.config.ts.template +12 -0
  90. package/lib/generators/add-fullstack/files/jest-mongodb-config.js +3 -2
  91. package/lib/generators/add-fullstack/files/jest.config copy.js +27 -0
  92. package/lib/generators/add-fullstack/files/jest.config.base.js +43 -4
  93. package/lib/generators/add-fullstack/files/jest.config.base.mjs +99 -0
  94. package/lib/generators/add-fullstack/files/jest.config.mongodb.mjs +3 -0
  95. package/lib/generators/add-fullstack/files/lint-staged.config.js +4 -1
  96. package/lib/generators/add-fullstack/files/nx.json +18 -13
  97. package/lib/generators/add-fullstack/files/package.json +8 -8
  98. package/lib/generators/add-fullstack/files/tools/cli/helpers/util.js +1 -1
  99. package/lib/generators/add-fullstack/files/tools/deploy-cli/updateLernaVersion.js +1 -1
  100. package/lib/generators/add-fullstack/files/tools/prettier.config.js +8 -0
  101. package/lib/generators/add-fullstack/files/tools/rollup/rollupPluginGenerateJson.mjs +48 -0
  102. package/lib/generators/add-fullstack/files/tools/rollup/rollupPluginModifyLibFiles.mjs +410 -0
  103. package/lib/generators/add-fullstack/files/tools/runLint.mjs +15 -0
  104. package/lib/generators/add-fullstack/files/tools/sortPackageJson.mjs +0 -1
  105. package/lib/generators/add-fullstack/files/tools/update-dependencies.mjs +95 -0
  106. package/lib/generators/add-fullstack/files/tools/update-dependency-link.mjs +138 -0
  107. package/lib/generators/add-fullstack/files/tools/update-dependency-version.js +104 -89
  108. package/lib/generators/add-fullstack/files/tools/updateUtils.mjs +77 -0
  109. package/lib/generators/add-fullstack/generator.cjs +63 -2
  110. package/lib/generators/add-fullstack/generator.cjs.map +1 -1
  111. package/lib/generators/add-fullstack/generator.mjs +63 -2
  112. package/lib/generators/add-fullstack/generator.mjs.map +1 -1
  113. package/lib/generators/add-fullstack/schema.json +4 -0
  114. package/lib/generators/add-moleculer/files/package.json +7 -7
  115. package/lib/generators/add-moleculer/generator.cjs +14 -16
  116. package/lib/generators/add-moleculer/generator.cjs.map +1 -1
  117. package/lib/generators/add-moleculer/generator.d.ts +1 -0
  118. package/lib/generators/add-moleculer/generator.mjs +14 -16
  119. package/lib/generators/add-moleculer/generator.mjs.map +1 -1
  120. package/lib/generators/add-server-package/files/CHANGELOG.md +2599 -0
  121. package/lib/generators/add-server-package/files/LICENSE +674 -0
  122. package/lib/generators/add-server-package/files/jest.config.js +32 -0
  123. package/lib/generators/add-server-package/files/package.json +78 -0
  124. package/lib/generators/add-server-package/files/rollup.config.mjs +30 -0
  125. package/lib/generators/add-server-package/files/src/constants/constants.ts.template +62 -0
  126. package/lib/generators/add-server-package/files/src/constants/index.ts.template +1 -0
  127. package/lib/generators/add-server-package/files/src/constants/types.ts.template +21 -0
  128. package/lib/generators/add-server-package/files/src/index.ts.template +6 -0
  129. package/lib/generators/add-server-package/files/src/module.ts.template +6 -0
  130. package/lib/generators/add-server-package/files/tsconfig.json +27 -0
  131. package/lib/generators/add-server-package/generator.cjs +17 -0
  132. package/lib/generators/add-server-package/generator.cjs.map +1 -0
  133. package/lib/generators/add-server-package/generator.d.ts +5 -0
  134. package/lib/generators/add-server-package/generator.mjs +17 -0
  135. package/lib/generators/add-server-package/generator.mjs.map +1 -0
  136. package/lib/generators/add-server-package/generator.spec.d.ts +1 -0
  137. package/lib/generators/add-server-package/schema.json +19 -0
  138. package/lib/index.cjs +1 -1
  139. package/lib/index.d.ts +5 -0
  140. package/lib/index.mjs +1 -1
  141. package/package.json +4 -2
  142. package/project.json +12 -1
  143. package/rollup.config.mjs +8 -0
  144. package/src/constants/index.ts +2 -1
  145. package/src/executors/sort-package-json/executor.spec.ts +18 -0
  146. package/src/executors/sort-package-json/executor.ts +53 -0
  147. package/src/executors/sort-package-json/schema.d.ts +1 -0
  148. package/src/executors/sort-package-json/schema.json +9 -0
  149. package/src/executors/update-deploy-version/executor.spec.ts +18 -0
  150. package/src/executors/update-deploy-version/executor.ts +36 -0
  151. package/src/executors/update-deploy-version/schema.d.ts +3 -0
  152. package/src/executors/update-deploy-version/schema.json +14 -0
  153. package/src/generators/add-backend/generator.ts +8 -4
  154. package/src/generators/add-browser-package/generator.ts +7 -6
  155. package/src/generators/add-client-package/generator.spec.ts +20 -0
  156. package/src/generators/add-client-package/generator.ts +24 -0
  157. package/src/generators/add-client-package/schema.d.ts +4 -0
  158. package/src/generators/add-client-package/schema.json +19 -0
  159. package/src/generators/add-core-package/generator.spec.ts +20 -0
  160. package/src/generators/add-core-package/generator.ts +24 -0
  161. package/src/generators/add-core-package/schema.d.ts +4 -0
  162. package/src/generators/add-core-package/schema.json +19 -0
  163. package/src/generators/add-frontend/frameworks/tailwindui/entry.client.tsx.template +90 -0
  164. package/src/generators/add-frontend/frameworks/tailwindui/entry.server.tsx.template +238 -0
  165. package/src/generators/add-frontend/frameworks/tailwindui/root.tsx.template +117 -0
  166. package/src/generators/add-frontend/frameworks/tailwindui/tailwind.css +3 -0
  167. package/src/generators/add-frontend/generator.ts +24 -2
  168. package/src/generators/add-frontend/schema.json +1 -1
  169. package/src/generators/add-fullstack/files/jest-mongodb-config.js +3 -2
  170. package/src/generators/add-fullstack/files/jest.config copy.js +27 -0
  171. package/src/generators/add-fullstack/files/jest.config.base.js +43 -4
  172. package/src/generators/add-fullstack/files/jest.config.base.mjs +99 -0
  173. package/src/generators/add-fullstack/files/jest.config.mongodb.mjs +3 -0
  174. package/src/generators/add-fullstack/files/lint-staged.config.js +4 -1
  175. package/src/generators/add-fullstack/files/nx.json +18 -13
  176. package/src/generators/add-fullstack/files/package.json +8 -8
  177. package/src/generators/add-fullstack/files/tools/cli/helpers/util.js +1 -1
  178. package/src/generators/add-fullstack/files/tools/deploy-cli/updateLernaVersion.js +1 -1
  179. package/src/generators/add-fullstack/files/tools/prettier.config.js +8 -0
  180. package/src/generators/add-fullstack/files/tools/rollup/rollupPluginGenerateJson.mjs +48 -0
  181. package/src/generators/add-fullstack/files/tools/rollup/rollupPluginModifyLibFiles.mjs +410 -0
  182. package/src/generators/add-fullstack/files/tools/runLint.mjs +15 -0
  183. package/src/generators/add-fullstack/files/tools/sortPackageJson.mjs +0 -1
  184. package/src/generators/add-fullstack/files/tools/update-dependencies.mjs +95 -0
  185. package/src/generators/add-fullstack/files/tools/update-dependency-link.mjs +138 -0
  186. package/src/generators/add-fullstack/files/tools/update-dependency-version.js +104 -89
  187. package/src/generators/add-fullstack/files/tools/updateUtils.mjs +77 -0
  188. package/src/generators/add-fullstack/generator.ts +83 -1
  189. package/src/generators/add-fullstack/schema.d.ts +1 -0
  190. package/src/generators/add-fullstack/schema.json +4 -0
  191. package/src/generators/add-moleculer/files/package.json +7 -7
  192. package/src/generators/add-moleculer/generator.ts +7 -6
  193. package/src/generators/add-server-package/generator.spec.ts +20 -0
  194. package/src/generators/add-server-package/generator.ts +24 -0
  195. package/src/generators/add-server-package/schema.d.ts +4 -0
  196. package/src/generators/add-server-package/schema.json +19 -0
  197. package/src/index.ts +5 -0
  198. package/lib/generators/add-backend/files/knexfile.js +0 -63
  199. package/lib/generators/add-backend/files/src/api/remote-config.ts.template +0 -11
  200. package/lib/generators/add-backend/files/src/api/resolver.ts.template +0 -15
  201. package/lib/generators/add-backend/files/src/api/scalar.ts.template +0 -16
  202. package/lib/generators/add-backend/files/src/api/schema-builder.ts.template +0 -189
  203. package/lib/generators/add-backend/files/src/api/utils.ts.template +0 -44
  204. package/lib/generators/add-backend/files/src/config/moleculer.config.ts.template +0 -228
  205. package/lib/generators/add-backend/files/src/connectors/connection-broker.ts.template +0 -80
  206. package/lib/generators/add-backend/files/src/connectors/graphql-pubsub-connector.ts.template +0 -43
  207. package/lib/generators/add-backend/files/src/connectors/mongo-connector.ts.template +0 -78
  208. package/lib/generators/add-backend/files/src/connectors/nats-connector.ts.template +0 -82
  209. package/lib/generators/add-backend/files/src/connectors/redis-connector.ts.template +0 -73
  210. package/lib/generators/add-backend/files/src/express-app.ts.template +0 -67
  211. package/lib/generators/add-backend/files/src/interfaces/index.ts.template +0 -1
  212. package/lib/generators/add-backend/files/src/interfaces/module-interface.ts.template +0 -16
  213. package/lib/generators/add-backend/files/src/main.spec.ts.template +0 -129
  214. package/lib/generators/add-backend/files/src/middleware/__tests__/cors.test.ts.template +0 -12
  215. package/lib/generators/add-backend/files/src/middleware/cors.ts.template +0 -31
  216. package/lib/generators/add-backend/files/src/middleware/error.ts.template +0 -63
  217. package/lib/generators/add-backend/files/src/middleware/moleculer-inter-namespace.ts.template +0 -60
  218. package/lib/generators/add-backend/files/src/middleware/persistedQuery.ts.template +0 -40
  219. package/lib/generators/add-backend/files/src/middleware/sentry.ts.template +0 -9
  220. package/lib/generators/add-backend/files/src/middleware/services.ts.template +0 -16
  221. package/lib/generators/add-backend/files/src/middleware/tracer.ts.template +0 -24
  222. package/lib/generators/add-backend/files/src/modules/auth/schema/auth-schema.graphql +0 -25
  223. package/lib/generators/add-backend/files/src/server-setup/graphql-server.ts.template +0 -185
  224. package/lib/generators/add-backend/files/src/server-setup/graphql-subscription-server.ts.template +0 -113
  225. package/lib/generators/add-backend/files/src/server-setup/graphql-ws.ts.template +0 -158
  226. package/lib/generators/add-backend/files/src/server-setup/mongodb-migration-update.ts.template +0 -47
  227. package/lib/generators/add-backend/files/src/server-setup/utils.ts.template +0 -43
  228. package/lib/generators/add-backend/files/src/server-setup/websocket-multipath-update.ts.template +0 -88
  229. package/lib/generators/add-backend/files/src/stack-server.ts.template +0 -277
  230. package/lib/generators/add-backend/files/src/utils/migrations.ts.template +0 -32
  231. package/lib/generators/add-backend/files/uploads/3986781.ppt +0 -0
  232. package/lib/generators/add-fullstack/files/tools/update-dependency-link.js +0 -107
  233. package/src/generators/add-fullstack/files/tools/update-dependency-link.js +0 -107
@@ -1,185 +0,0 @@
1
- import { ApolloServer, ApolloServerExpressConfig } from 'apollo-server-express';
2
- import {
3
- ApolloServerPluginCacheControl,
4
- ApolloServerPluginDrainHttpServer,
5
- GraphQLRequestContext
6
- } from 'apollo-server-core';
7
- import responseCachePlugin from 'apollo-server-plugin-response-cache';
8
- import 'isomorphic-fetch';
9
- import { Express } from 'express';
10
- import * as http from 'http';
11
- import Keyv from 'keyv';
12
- import KeyvRedis from '@keyv/redis';
13
- import { KeyvAdapter } from '@apollo/utils.keyvadapter';
14
- import { RedisCache, RedisClusterCache } from 'apollo-server-cache-redis';
15
- import { CdmLogger } from '@cdm-logger/core';
16
- import { WebSocketServer } from 'ws';
17
- import { IModuleService } from '../interfaces';
18
- import { GraphqlWs } from './graphql-ws';
19
- import { invalidateCachePlugin } from '@adminide-stack/platform-server';
20
- import { config } from '../config';
21
-
22
- type ILogger = CdmLogger.ILogger;
23
-
24
- let debug = false;
25
- if ((process.env.LOG_LEVEL && process.env.LOG_LEVEL === 'trace') || process.env.LOG_LEVEL === 'debug') {
26
- debug = true;
27
- }
28
-
29
- // @workaround as the `dataSources` not available in Subscription (websocket) Context.
30
- // https://github.com/apollographql/apollo-server/issues/1526 need to revisit in Apollo-Server v3.
31
- const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
32
- const intializeDataSource = (instance) => {
33
- instance.initialize({ context, cache });
34
- };
35
- // tslint:disable-next-line:forin
36
- // eslint-disable-next-line guard-for-in
37
- for (const prop in dataSources) {
38
- intializeDataSource(dataSources[prop]);
39
- }
40
- return dataSources;
41
- };
42
-
43
- let wsServerCleanup: any;
44
-
45
-
46
-
47
- export class GraphqlServer {
48
- private logger: ILogger;
49
-
50
- constructor(
51
- private app: Express,
52
- private httpServer: http.Server,
53
- private cache: RedisCache | RedisClusterCache,
54
- private moduleService: IModuleService,
55
- private enableSubscription = true,
56
- ) {
57
- this.logger = this.moduleService.logger.child({ className: 'GraphqlServer' });
58
- if (enableSubscription) {
59
- const wsServer = new WebSocketServer({
60
- server: this.httpServer,
61
- path: __GRAPHQL_ENDPOINT__,
62
- });
63
- const graphqlWs = new GraphqlWs(wsServer, this.moduleService, this.cache);
64
- wsServerCleanup = graphqlWs.create();
65
- }
66
- }
67
-
68
- public async initialize() {
69
- this.logger.info('GraphqlServer initializing...');
70
- const apolloServer = this.configureApolloServer();
71
- await apolloServer.start();
72
- const corsOptions = {
73
- origin: [config.CLIENT_URL],
74
- credentials: true,
75
- };
76
- apolloServer.applyMiddleware({
77
- app: this.app,
78
- cors: corsOptions,
79
- path: __GRAPHQL_ENDPOINT__,
80
- });
81
- this.logger.info('GraphqlServer initialized - ', config.CLIENT_URL);
82
- }
83
-
84
- getUserIpAddress(req) {
85
- let ip = (req?.headers['x-forwarded-for'] || '').split(',')[0] || req?.connection?.remoteAddress;
86
- if (ip.substr(0, 7) === '::ffff:') {
87
- ip = ip.substr(7);
88
- }
89
- if (ip === '::1') {
90
- ip = '127.0.0.1';
91
- }
92
- return ip;
93
- }
94
-
95
- private configureApolloServer(): ApolloServer {
96
- this.logger.info('-- Configuring Apollo Server --');
97
- const cache = new KeyvAdapter(new Keyv({ store: new KeyvRedis(this.cache.client) }), {
98
- disableBatchReads: true,
99
- });
100
-
101
- const serverConfig: ApolloServerExpressConfig = {
102
- debug,
103
- cache,
104
- schema: this.moduleService.schema,
105
- dataSources: () => this.moduleService.dataSource,
106
- context: async ({
107
- req,
108
- res,
109
- connection,
110
- }: {
111
- req: Express.Request;
112
- res: Express.Response;
113
- connection: any;
114
- }) => {
115
- let context;
116
- let addons = {};
117
- try {
118
- if (connection) {
119
- context = connection.context;
120
- if (!context.dataSources) {
121
- addons = {
122
- // @workaround for apollo server issue #1526
123
- dataSources: constructDataSourcesForSubscriptions(
124
- connection.context,
125
- this.cache,
126
- this.moduleService.dataSource,
127
- ),
128
- };
129
- } else {
130
- addons = {
131
- // @workaround for apollo server issue #1526
132
- dataSources: context.dataSources,
133
- };
134
- }
135
- } else {
136
- const pureContext = await this.moduleService.createContext(req, res);
137
- const contextServices = await this.moduleService.serviceContext(req, res);
138
- context = {
139
- ...pureContext,
140
- ...contextServices,
141
- preferences: this.moduleService.defaultPreferences,
142
- // update: updateContainers,
143
- };
144
- }
145
- context.userIp = this.getUserIpAddress(req);
146
- } catch (err) {
147
- this.logger.error('Adding context to GraphQL failed', { error: err });
148
- throw err;
149
- }
150
- return {
151
- req,
152
- res,
153
- ...context,
154
- ...addons,
155
- };
156
- },
157
- plugins: [
158
- ApolloServerPluginDrainHttpServer({ httpServer: this.httpServer }),
159
- ApolloServerPluginCacheControl(),
160
- responseCachePlugin({
161
- sessionId: ({ context }) => context?.userContext?.accountId ?? null,
162
- generateCacheKey({
163
- queryHash,
164
- operationName
165
- }: GraphQLRequestContext<Record<string, any>>, keyData): string {
166
- return `${operationName}:${queryHash}`;
167
- },
168
- }),
169
- invalidateCachePlugin({ cache: this.cache }),
170
- ],
171
- };
172
- if (this.enableSubscription) {
173
- serverConfig.plugins.push({
174
- async serverWillStart() {
175
- return {
176
- drainServer: async () => {
177
- await wsServerCleanup.dispose();
178
- },
179
- };
180
- },
181
- });
182
- }
183
- return new ApolloServer(serverConfig);
184
- }
185
- }
@@ -1,113 +0,0 @@
1
- // import { SubscriptionServer, ConnectionContext, ExecutionParams } from 'subscriptions-transport-ws';
2
- // import { execute, subscribe, ExecutionResult } from 'graphql';
3
- // // import { GraphQLServerOptions } from 'apollo-server-core';
4
- // import { formatApolloErrors } from 'apollo-server-errors';
5
- // import { GraphQLServerOptions } from 'apollo-server-core/dist/graphqlOptions';
6
- // import { Context } from 'apollo-server-core';
7
- // import { RedisClusterCache, RedisCache } from 'apollo-server-cache-redis';
8
- // import { CdmLogger } from '@cdm-logger/core';
9
- // import { IModuleService } from '../interfaces';
10
-
11
- // type ILogger = CdmLogger.ILogger;
12
-
13
- // // @workaround as the `dataSources` not available in Subscription (websocket) Context.
14
- // // https://github.com/apollographql/apollo-server/issues/1526 need to revisit in Apollo-Server v3.
15
- // const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
16
- // const intializeDataSource = (instance) => {
17
- // instance.initialize({ context, cache });
18
- // };
19
- // // tslint:disable-next-line:forin
20
- // for (const prop in dataSources) {
21
- // // tslint:disable-next-line:no-console
22
- // intializeDataSource(dataSources[prop]);
23
- // }
24
- // return dataSources;
25
- // };
26
-
27
- // export class GraphqlSubscriptionServer {
28
- // private subscriptionServer: SubscriptionServer;
29
-
30
- // private context: Context;
31
-
32
- // private logger: ILogger;
33
-
34
- // constructor(
35
- // private moduleService: IModuleService,
36
- // private cache: RedisCache | RedisClusterCache,
37
- // private requestOptions: Partial<GraphQLServerOptions<any>> = Object.create(null),
38
- // ) {
39
- // this.logger = this.moduleService.logger.child({ className: 'GraphqlSubscriptionServer' });
40
- // }
41
-
42
- // public create() {
43
- // this.subscriptionServer = SubscriptionServer.create(
44
- // {
45
- // schema: this.moduleService.schema as any,
46
- // execute,
47
- // subscribe,
48
- // onConnect: async (connectionParams: any, webSocket: any, ctx: ConnectionContext) => {
49
- // try {
50
- // this.logger.debug(`Subscription client connected using built-in SubscriptionServer.`);
51
- // const pureContext = await this.moduleService.createContext(connectionParams, webSocket);
52
- // const contextServices = await this.moduleService.serviceContext(connectionParams, webSocket);
53
- // const context = {
54
- // ...contextServices,
55
- // ...pureContext,
56
- // preferences: this.moduleService.defaultPreferences,
57
- // // update: updateContainers,
58
- // wsCtx: ctx,
59
- // };
60
- // const addons = {
61
- // dataSources: constructDataSourcesForSubscriptions(
62
- // context,
63
- // this.cache,
64
- // this.moduleService.dataSource,
65
- // ),
66
- // };
67
- // return {
68
- // ...context,
69
- // ...addons,
70
- // };
71
- // } catch (e) {
72
- // this.logger.error(e);
73
- // }
74
- // },
75
- // onOperation: async (message: { payload: any }, connection: ExecutionParams) => {
76
- // connection.formatResponse = (value: ExecutionResult) => ({
77
- // ...value,
78
- // errors:
79
- // value.errors &&
80
- // formatApolloErrors([...value.errors], {
81
- // formatter: this.requestOptions.formatError,
82
- // debug: this.requestOptions.debug,
83
- // }),
84
- // });
85
- // let context: Context = this.context ? this.context : { connection };
86
- // try {
87
- // context =
88
- // typeof this.context === 'function'
89
- // ? await this.context({ connection, payload: message.payload })
90
- // : context;
91
- // } catch (e) {
92
- // throw formatApolloErrors([e], {
93
- // formatter: this.requestOptions.formatError,
94
- // debug: this.requestOptions.debug,
95
- // })[0];
96
- // }
97
-
98
- // return { ...connection }; // TODO: we didn't add `context`
99
- // },
100
- // },
101
- // {
102
- // noServer: true,
103
- // },
104
- // );
105
- // return this.subscriptionServer;
106
- // }
107
-
108
- // public disconnect() {
109
- // if (this.subscriptionServer) {
110
- // this.subscriptionServer.close();
111
- // }
112
- // }
113
- // }
@@ -1,158 +0,0 @@
1
- import { useServer } from 'graphql-ws/lib/use/ws';
2
- import { GraphQLSchema, parse, getOperationAST, GraphQLError, validate } from 'graphql';
3
- import { GraphQLServerOptions } from 'apollo-server-core/dist/graphqlOptions';
4
- import { Context } from 'apollo-server-core';
5
- import { RedisClusterCache, RedisCache } from 'apollo-server-cache-redis';
6
- import { CdmLogger } from '@cdm-logger/core';
7
- import { Disposable, SubscribeMessage } from 'graphql-ws';
8
- import { WebSocketServer } from 'ws';
9
- import { IModuleService } from '../interfaces';
10
- import { createContextFromConnectionParams } from './utils';
11
-
12
- type ILogger = CdmLogger.ILogger;
13
-
14
- // @workaround as the `dataSources` not available in Subscription (websocket) Context.
15
- // https://github.com/apollographql/apollo-server/issues/1526 need to revisit in Apollo-Server v3.
16
- const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
17
- const intializeDataSource = (instance) => {
18
- instance.initialize({ context, cache });
19
- };
20
- // tslint:disable-next-line:forin
21
- for (const prop in dataSources) {
22
- // tslint:disable-next-line:no-console
23
- intializeDataSource(dataSources[prop]);
24
- }
25
- return dataSources;
26
- };
27
-
28
- /**
29
- * Accept only subscription operations
30
- * Code from graphql-ws recipes:
31
- * https://github.com/enisdenjo/graphql-ws/blob/master/README.md#recipes
32
- */
33
- function allowOnlySub(schema: GraphQLSchema, msg: SubscribeMessage) {
34
- // construct the execution arguments
35
- const args = {
36
- schema,
37
- operationName: msg.payload.operationName,
38
- document: parse(msg.payload.query),
39
- variableValues: msg.payload.variables,
40
- };
41
-
42
- const operationAST = getOperationAST(args.document, args.operationName);
43
- if (!operationAST) {
44
- // returning `GraphQLError[]` sends an `ErrorMessage` and stops the subscription
45
- return [new GraphQLError('Unable to identify operation')];
46
- }
47
-
48
- // handle mutation and query requests
49
- if (operationAST.operation !== 'subscription') {
50
- // returning `GraphQLError[]` sends an `ErrorMessage` and stops the subscription
51
- // return [new GraphQLError('Only subscription operations are supported')];
52
-
53
- // or if you want to be strict and terminate the connection on illegal operations
54
- throw new Error('Only subscription operations are supported');
55
- }
56
-
57
- // dont forget to validate
58
- const errors = validate(args.schema, args.document);
59
- if (errors.length > 0) {
60
- // returning `GraphQLError[]` sends an `ErrorMessage` and stops the subscription
61
- return errors;
62
- }
63
-
64
- // ready execution arguments
65
- return args;
66
- }
67
-
68
- export class GraphqlWs {
69
- private subscriptionServer: Disposable;
70
-
71
- private logger: ILogger;
72
-
73
- constructor(
74
- private wsServer: WebSocketServer,
75
- private moduleService: IModuleService,
76
- protected cache: RedisCache | RedisClusterCache,
77
- ) {}
78
-
79
- public create() {
80
- this.subscriptionServer = useServer(
81
- {
82
- schema: this.moduleService.schema,
83
- // Adding a context property lets you add data to your GraphQL operation context
84
- context: async (ctx) => {
85
- // Returning an object here will add that information to our
86
- // GraphQL context, which all of our resolvers have access to.
87
- // ctx is the `graphql-ws` Context where connectionParams live
88
- try {
89
- const pureContext = await this.moduleService.createContext(ctx.connectionParams, null);
90
- const contextServices = await this.moduleService.serviceContext(ctx.connectionParams, null);
91
- const context = {
92
- ...contextServices,
93
- ...pureContext,
94
- preferences: this.moduleService.defaultPreferences,
95
- // update: updateContainers,
96
- ...ctx,
97
- };
98
- const addons = {
99
- dataSources: constructDataSourcesForSubscriptions(
100
- context,
101
- this.cache,
102
- this.moduleService.dataSource,
103
- ),
104
- };
105
- return {
106
- ...context,
107
- ...addons,
108
- ...ctx,
109
- ...pureContext,
110
- preferences: this.moduleService.defaultPreferences,
111
- };
112
- } catch (err) {
113
- this.logger.error(err);
114
- }
115
- },
116
- // onConnect: async (ctx) => {
117
- // // do anything when connected
118
- // },
119
- // onOperation: (ctx, msg) => {
120
-
121
- // }
122
- // onOperation: async (message: { payload: any }, connection: ExecutionParams) => {
123
- // connection.formatResponse = (value: ExecutionResult) => ({
124
- // ...value,
125
- // errors:
126
- // value.errors &&
127
- // formatApolloErrors([...value.errors], {
128
- // formatter: this.requestOptions.formatError,
129
- // debug: this.requestOptions.debug,
130
- // }),
131
- // });
132
- // let context: Context = this.context ? this.context : { connection };
133
- // try {
134
- // context =
135
- // typeof this.context === 'function'
136
- // ? await this.context({ connection, payload: message.payload })
137
- // : context;
138
- // } catch (e) {
139
- // throw formatApolloErrors([e], {
140
- // formatter: this.requestOptions.formatError,
141
- // debug: this.requestOptions.debug,
142
- // })[0];
143
- // }
144
-
145
- // return { ...connection }; // TODO: we didn't add `context`
146
- // },
147
- },
148
- this.wsServer,
149
- );
150
- return this.subscriptionServer;
151
- }
152
-
153
- public disconnect() {
154
- if (this.subscriptionServer) {
155
- this.subscriptionServer.dispose();
156
- }
157
- }
158
- }
@@ -1,47 +0,0 @@
1
- import { IModuleService } from '../interfaces';
2
- import { Schema, Connection as DBConnection } from 'mongoose';
3
-
4
-
5
-
6
- export const MigrationSchema = new Schema({
7
- migrated_at: Date,
8
- name: { required: true, type: String },
9
- });
10
-
11
-
12
-
13
- export class MongoDBMigrationUpgrade {
14
-
15
- constructor(
16
- private moduleService: IModuleService,
17
- private mongoDB: DBConnection,
18
- ) {
19
- }
20
-
21
- public async migrate(): Promise<any> {
22
- const container = await this.moduleService.createContext({});
23
- try {
24
- const migrations: any[] = container.getAll('MongodbMigration');
25
- const model = this.mongoDB.model('Migration', MigrationSchema);
26
-
27
- return Promise.all(
28
- migrations.map(async (migration) => {
29
- const exists = await model.findOne({ name: migration.constructor.name });
30
- if (!exists) {
31
- try {
32
- await migration.up();
33
- await model.create({ name: migration.constructor.name, migrated_at: new Date() });
34
- } catch (e) {
35
- console.log(`Can not process migration ${migration.constructor.name}: `, e);
36
- }
37
- }
38
-
39
- return migration.constructor.name;
40
- }),
41
- );
42
- } catch (err) {
43
- console.warn('ignoring database migratoin');
44
- }
45
- }
46
- }
47
-
@@ -1,43 +0,0 @@
1
- import { RedisCache } from 'apollo-server-cache-redis';
2
- import { CdmLogger } from '@cdm-logger/core';
3
-
4
- type ILogger = CdmLogger.ILogger;
5
-
6
- // @workaround as the `dataSources` not available in Subscription (websocket) Context.
7
- // https://github.com/apollographql/apollo-server/issues/1526 need to revisit in Apollo-Server v3.
8
- const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
9
- const intializeDataSource = (instance) => {
10
- instance.initialize({ context, cache });
11
- };
12
- // tslint:disable-next-line:forin
13
- for (const prop in dataSources) {
14
- // tslint:disable-next-line:no-console
15
- intializeDataSource(dataSources[prop]);
16
- }
17
- return dataSources;
18
- };
19
-
20
- /**
21
- * It'll get messy below but there's an issue currently with the state of the protocols that can be used (subscriptions-transport-ws vs graphql-ws).
22
- * Read more from: https://www.apollographql.com/docs/apollo-server/data/subscriptions/#the-graphql-ws-transport-library
23
- *
24
- * Bottomline is, if we use the newer and actively maintained `graphql-ws` lib, then the GraphQL Playground will not work because it uses the old protocol.
25
- *
26
- * The approach below tries to support both based on the template provided here but adjusted for our setup here.
27
- * https://github.com/enisdenjo/graphql-ws#ws-backwards-compat
28
- */
29
- export async function createContextFromConnectionParams(
30
- context: any,
31
- datasources: any,
32
- cache: RedisCache,
33
- logger: ILogger,
34
- ): Promise<any> {
35
- // logger.debug(`Subscription client connected using built-in SubscriptionServer.`);
36
- const addons = {
37
- dataSources: constructDataSourcesForSubscriptions(context, cache, datasources),
38
- };
39
- return {
40
- ...context,
41
- ...addons,
42
- };
43
- }
@@ -1,88 +0,0 @@
1
- import * as url from 'url';
2
- // import { GraphqlSubscriptionServer } from './graphql-subscription-server';
3
- import { GraphqlWs } from './graphql-ws';
4
- import { WebSocketServer } from 'ws';
5
- import { Server } from 'http';
6
- import { RedisClusterCache, RedisCache } from 'apollo-server-cache-redis';
7
- import { IModuleService } from '../interfaces';
8
- import { useServer } from 'graphql-ws/lib/use/ws';
9
-
10
- interface WebSocketsCache {
11
- [key: string]: WebSocketServer;
12
- }
13
-
14
- interface MultiWebsocketConfig {
15
- [key: string]: any;
16
- }
17
- export class WebsocketMultiPathServer {
18
- private webSockets: WebSocketsCache = {};
19
- // private graphqlSubscriptionServer: GraphqlSubscriptionServer;
20
- private _graphqlWs: WebSocketServer;
21
- constructor(
22
- public moduleService: IModuleService,
23
- public cache: RedisCache | RedisClusterCache,
24
- multiplePathConfig?: MultiWebsocketConfig,
25
- ) {
26
- this._graphqlWs = new WebSocketServer({ noServer: true, path: __GRAPHQL_ENDPOINT__ });
27
- this.webSockets[__GRAPHQL_ENDPOINT__] = this._graphqlWs;
28
- this.webSockets[__GRAPHQL_ENDPOINT__].on('connection', (ws, request) => {
29
- console.log('--REQUESTED CONNECTION--', this._graphqlWs)
30
- useServer({
31
- schema: moduleService.schema,
32
- }, this._graphqlWs);
33
- });
34
-
35
- for (let key in multiplePathConfig) {
36
- if (!multiplePathConfig.hasOwnProperty(key)) {
37
- continue;
38
- }
39
-
40
- if (!this.webSockets[key]) {
41
- this.webSockets[key] = new WebSocketServer({ noServer: true });
42
- this.webSockets[key].on('connection', (ws, request) => {
43
- Promise.all([
44
- moduleService.createContext(request, null),
45
- moduleService.serviceContext(request, null),
46
-
47
- ]).then(multiplePathConfig[key](ws));
48
- });
49
- }
50
- }
51
- }
52
-
53
- public httpServerUpgrade(httpServer: Server) {
54
- httpServer.on('upgrade', (request, socket, head) => {
55
- const pathname = url.parse(request.url).pathname;
56
- console.log('--UPGRADE', request.url)
57
-
58
- if (!this.webSockets[pathname]) {
59
- // in development
60
- if (pathname !== '/sockjs-node') {
61
- // need to destroy
62
- socket.destroy();
63
- }
64
- }
65
-
66
- // code to run when a new connection is made
67
- this.webSockets[pathname].handleUpgrade(request, socket, head, (ws) => {
68
- this.webSockets[pathname].emit('connection', ws, request);
69
- });
70
- });
71
-
72
- // (new GraphqlWs(this.graphqlWs, this.moduleService, this.cache)).create();
73
- // useServer({
74
- // schema: this.moduleService.schema,
75
- // }, this.graphqlWs);
76
-
77
- return httpServer;
78
- }
79
-
80
- public get graphqlWs() {
81
- return this._graphqlWs;
82
- }
83
- public close() {
84
- for (const key in this.webSockets) {
85
- this.webSockets[key].close();
86
- }
87
- }
88
- }