@things-factory/shell 4.0.0-y.0 → 4.0.5

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.
package/server/server.ts CHANGED
@@ -1,25 +1,24 @@
1
1
  process.env.NODE_ENV = 'production'
2
2
  process.setMaxListeners(0)
3
3
 
4
- import Koa from 'koa'
5
- import ip from 'koa-ip'
6
- import cors from '@koa/cors'
7
- import koaStatic from 'koa-static'
8
- import koaBodyParser from 'koa-bodyparser'
9
- import { historyApiFallback } from 'koa2-connect-history-api-fallback'
4
+ import { ConnectionContext, SubscriptionServer } from 'subscriptions-transport-ws'
5
+ import { config, loader, logger, orderedModuleNames } from '@things-factory/env'
6
+ import { domainPrivateRouter, domainPublicRouter, globalPrivateRouter, globalPublicRouter } from './routers'
7
+ import { execute, subscribe } from 'graphql'
10
8
 
11
9
  import { ApolloServer } from 'apollo-server-koa'
12
- import { graphqlUploadKoa } from 'graphql-upload'
13
-
10
+ import { GraphqlLocalClient } from './graphql-local-client'
11
+ import Koa from 'koa'
14
12
  import co from 'co'
15
13
  import compose from 'koa-compose'
16
-
17
- import { config, logger, orderedModuleNames, loader } from '@things-factory/env'
18
-
14
+ import cors from '@koa/cors'
15
+ import { createServer } from 'http'
19
16
  import { databaseInitializer } from './initializers/database'
20
- import { globalPublicRouter, globalPrivateRouter, domainPublicRouter, domainPrivateRouter } from './routers'
21
- import { GraphqlLocalClient } from './graphql-local-client'
22
-
17
+ import { graphqlUploadKoa } from 'graphql-upload'
18
+ import { historyApiFallback } from 'koa2-connect-history-api-fallback'
19
+ import ip from 'koa-ip'
20
+ import koaBodyParser from 'koa-bodyparser'
21
+ import koaStatic from 'koa-static'
23
22
  import { schema } from './schema'
24
23
 
25
24
  const args = require('args')
@@ -77,12 +76,15 @@ const bootstrap = async () => {
77
76
 
78
77
  const builtSchema = await schema()
79
78
 
80
- const server = new ApolloServer({
81
- schema: builtSchema,
82
- uploads: false,
83
- subscriptions: {
84
- path: '/subscriptions',
85
- onConnect: async (connectionParams, webSocket, context) => {
79
+ const httpServer = createServer(app.callback())
80
+
81
+ const subscriptionServer = SubscriptionServer.create(
82
+ {
83
+ schema: builtSchema,
84
+ // These are imported from `graphql`.
85
+ execute,
86
+ subscribe,
87
+ async onConnect(connectionParams: Object, webSocket: WebSocket, context: ConnectionContext) {
86
88
  var { request } = context
87
89
 
88
90
  var url = new URL((connectionParams['headers'] || connectionParams).referer)
@@ -106,6 +108,14 @@ const bootstrap = async () => {
106
108
  return koacontext
107
109
  }
108
110
  },
111
+ {
112
+ server: httpServer,
113
+ path: '/subscriptions'
114
+ }
115
+ )
116
+
117
+ const server = new ApolloServer({
118
+ schema: builtSchema,
109
119
  formatError: error => {
110
120
  logger.error(error)
111
121
  return error
@@ -115,20 +125,28 @@ const bootstrap = async () => {
115
125
  return response
116
126
  },
117
127
  introspection: true,
118
- playground: {
119
- settings: {
120
- 'request.credentials': 'include'
121
- }
122
- },
123
128
  context: async ({ connection, ctx }) => {
124
129
  if (connection) {
125
130
  return connection.context
126
131
  } else {
127
132
  return ctx
128
133
  }
129
- }
134
+ },
135
+ plugins: [
136
+ {
137
+ async serverWillStart() {
138
+ return {
139
+ async drainServer() {
140
+ subscriptionServer.close()
141
+ }
142
+ }
143
+ }
144
+ }
145
+ ]
130
146
  })
131
147
 
148
+ await server.start()
149
+
132
150
  GraphqlLocalClient.init(builtSchema, app)
133
151
 
134
152
  orderedModuleNames.forEach(name => {
@@ -136,7 +154,7 @@ const bootstrap = async () => {
136
154
  initMiddlewares && initMiddlewares(app)
137
155
  })
138
156
 
139
- const render = require('koa-ejs-remote')
157
+ const render = require('@things-factory/ejs-remote')
140
158
  render(app, {
141
159
  root: '/views',
142
160
  host: `http://127.0.0.1:${PORT}`,
@@ -180,14 +198,12 @@ const bootstrap = async () => {
180
198
  })
181
199
  )
182
200
 
183
- const httpServer = app.listen({ port: PORT }, () => {
201
+ httpServer.listen({ port: PORT }, () => {
184
202
  logger.info(`🚀 Server ready at http://0.0.0.0:${PORT}${server.graphqlPath}`)
185
- logger.info(`🚀 Subscriptions ready at ws://0.0.0.0:${PORT}${server.subscriptionsPath}`)
203
+ logger.info(`🚀 Subscriptions ready at ws://0.0.0.0:${PORT}${'/subscriptions'}`)
186
204
 
187
205
  process.emit('bootstrap-module-start' as any, { app, config, builtSchema, httpServer } as any)
188
206
  })
189
-
190
- server.installSubscriptionHandlers(httpServer)
191
207
  }
192
208
 
193
209
  bootstrap()
@@ -1,19 +1,21 @@
1
- import { Resolver, Query, Mutation, Args, Arg, Ctx, Directive } from 'type-graphql'
1
+ import { Arg, Args, Ctx, Directive, Mutation, Query, Resolver } from 'type-graphql'
2
2
  import { getRepository, In, Repository } from 'typeorm'
3
- import { ListParam } from '../common-types/list-param'
3
+
4
4
  import { slugger } from '@things-factory/utils'
5
- import { Domain } from './domain'
6
- import { DomainPatch, DomainList } from './domain-types'
5
+
7
6
  import { buildQuery } from '../../utils/list-query-builder'
7
+ import { ListParam } from '../common-types/list-param'
8
+ import { Domain } from './domain'
9
+ import { DomainList, DomainPatch } from './domain-types'
8
10
 
9
11
  @Resolver(Domain)
10
12
  export class DomainResolver {
11
13
  @Directive('@privilege(category: "system", privilege: "mutation", domainOwnerGranted: true, superUserGranted: true)')
12
14
  @Query(returns => Domain, { description: 'To fetch domain' })
13
- async domain(@Arg('name') name: string): Promise<Domain> {
15
+ async domain(@Arg('id') id: string): Promise<Domain> {
14
16
  const repository = getRepository(Domain)
15
17
 
16
- return await repository.findOne({ name })
18
+ return await repository.findOne({ id })
17
19
  }
18
20
 
19
21
  @Directive('@privilege(category: "system", privilege: "query", superUserGranted: true)')
@@ -1,11 +1,18 @@
1
+ import { transactionDirectiveResolver, transactionDirectiveTypeDefs } from './directive-transaction'
2
+ import {
3
+ entities as DataEntities,
4
+ entities as DomainEntities,
5
+ resolvers as DataResolvers,
6
+ resolvers as DomainResolvers
7
+ } from './domain'
8
+ import { resolvers as SubscriptionDataResolvers } from './subscription-data'
9
+
1
10
  export * from './common-types'
2
11
  export * from './domain/domain'
3
- export * from './domain/domain-types'
4
12
 
5
- import { entities as DataEntities, resolvers as DataResolvers } from './domain'
6
- import { entities as DomainEntities, resolvers as DomainResolvers } from './domain'
7
- import { resolvers as SubscriptionDataResolvers } from './subscription-data'
8
- import { transactionDirectiveTypeDefs, transactionDirectiveResolver } from './directive-transaction'
13
+ /* EXPORT TYPES */
14
+ export * from './domain/domain-types'
15
+ export * from './subscription-data/data-types'
9
16
 
10
17
  export const entities = [
11
18
  /* Entities */
@@ -15,68 +15,66 @@ export const buildCondition = function (
15
15
  switch (operator) {
16
16
  case 'eq':
17
17
  return {
18
- clause: `"${alias}"."${fieldName}" = :args${seq}`,
18
+ clause: `${alias}.${fieldName} = :args${seq}`,
19
19
  parameters: { [`args${seq}`]: value }
20
20
  }
21
21
 
22
22
  case 'like':
23
23
  return {
24
- clause: `"${alias}"."${fieldName}" LIKE :args${seq}`,
24
+ clause: `${alias}.${fieldName} LIKE :args${seq}`,
25
25
  parameters: { [`args${seq}`]: `${value}` }
26
26
  }
27
27
 
28
28
  case 'i_like':
29
29
  return {
30
- clause: `LOWER("${alias}"."${fieldName}") LIKE :args${seq}`,
30
+ clause: `LOWER(${alias}.${fieldName}) LIKE :args${seq}`,
31
31
  parameters: { [`args${seq}`]: `${String(value).toLowerCase()}` }
32
32
  }
33
33
 
34
34
  case 'nlike':
35
35
  return {
36
- clause: `"${alias}"."${fieldName}" NOT LIKE :args${seq}`,
36
+ clause: `${alias}.${fieldName} NOT LIKE :args${seq}`,
37
37
  value: { [`args${seq}`]: `${value}` }
38
38
  }
39
39
 
40
40
  case 'i_nlike':
41
41
  return {
42
- clause: `LOWER("${alias}"."${fieldName}") NOT LIKE :args${seq}`,
42
+ clause: `LOWER(${alias}.${fieldName}) NOT LIKE :args${seq}`,
43
43
  value: { [`args${seq}`]: `${String(value).toLowerCase()}` }
44
44
  }
45
45
 
46
46
  case 'lt':
47
47
  return {
48
- clause: `"${alias}"."${fieldName}" < :args${seq}`,
48
+ clause: `${alias}.${fieldName} < :args${seq}`,
49
49
  parameters: { [`args${seq}`]: value }
50
50
  }
51
51
 
52
52
  case 'gt':
53
53
  return {
54
- clause: `"${alias}"."${fieldName}" > :args${seq}`,
54
+ clause: `${alias}.${fieldName} > :args${seq}`,
55
55
  parameters: { [`args${seq}`]: value }
56
56
  }
57
57
 
58
58
  case 'lte':
59
59
  return {
60
- clause: `"${alias}"."${fieldName}" <= :args${seq}`,
60
+ clause: `${alias}.${fieldName} <= :args${seq}`,
61
61
  parameters: { [`args${seq}`]: value }
62
62
  }
63
63
 
64
64
  case 'gte':
65
65
  return {
66
- clause: `"${alias}"."${fieldName}" >= :args${seq}`,
66
+ clause: `${alias}.${fieldName} >= :args${seq}`,
67
67
  parameters: { [`args${seq}`]: value }
68
68
  }
69
69
 
70
70
  case 'noteq':
71
71
  return {
72
- clause: `"${alias}"."${fieldName}" != :args${seq}`,
72
+ clause: `${alias}.${fieldName} != :args${seq}`,
73
73
  parameters: { [`args${seq}`]: value }
74
74
  }
75
75
 
76
76
  case 'in':
77
- const clause = relation
78
- ? `"${fieldName}"."id" IN (:...args${seq})`
79
- : `"${alias}"."${fieldName}" IN (:...args${seq})`
77
+ const clause = relation ? `${fieldName}.id IN (:...args${seq})` : `${alias}.${fieldName} IN (:...args${seq})`
80
78
  value = value?.length ? value : [value]
81
79
  return {
82
80
  clause,
@@ -85,57 +83,57 @@ export const buildCondition = function (
85
83
  case 'notin':
86
84
  value = value?.length ? value : [value]
87
85
  return {
88
- clause: `"${alias}"."${fieldName}" NOT IN (:...args${seq})`,
86
+ clause: `${alias}.${fieldName} NOT IN (:...args${seq})`,
89
87
  parameters: { [`args${seq}`]: value }
90
88
  }
91
89
 
92
90
  case 'notin_with_null':
93
91
  value = value?.length ? value : [value]
94
92
  return {
95
- clause: `("${alias}"."${fieldName}" IS NULL OR "${alias}"."${fieldName}" NOT IN (:...args${seq}))`,
93
+ clause: `(${alias}.${fieldName} IS NULL OR ${alias}.${fieldName} NOT IN (:...args${seq}))`,
96
94
  parameters: { [`args${seq}`]: value }
97
95
  }
98
96
 
99
97
  case 'is_null':
100
98
  return {
101
- clause: `"${alias}"."${fieldName}" IS NULL`
99
+ clause: `${alias}.${fieldName} IS NULL`
102
100
  }
103
101
  case 'is_not_null':
104
102
  return {
105
- clause: `"${alias}"."${fieldName}" IS NOT NULL`
103
+ clause: `${alias}.${fieldName} IS NOT NULL`
106
104
  }
107
105
  case 'is_false':
108
106
  return {
109
- clause: `"${alias}"."${fieldName}" IS FALSE`
107
+ clause: `${alias}.${fieldName} IS FALSE`
110
108
  }
111
109
  case 'is_true':
112
110
  return {
113
- clause: `"${alias}"."${fieldName}" IS TRUE`
111
+ clause: `${alias}.${fieldName} IS TRUE`
114
112
  }
115
113
  case 'is_not_false':
116
114
  return {
117
- clause: `"${alias}"."${fieldName}" IS NOT FALSE`
115
+ clause: `${alias}.${fieldName} IS NOT FALSE`
118
116
  }
119
117
  case 'is_not_true':
120
118
  return {
121
- clause: `"${alias}"."${fieldName}" IS NOT TRUE`
119
+ clause: `${alias}.${fieldName} IS NOT TRUE`
122
120
  }
123
121
  case 'is_present':
124
122
  return {
125
- clause: `"${alias}"."${fieldName}" IS PRESENT`
123
+ clause: `${alias}.${fieldName} IS PRESENT`
126
124
  }
127
125
  case 'is_blank':
128
126
  return {
129
- clause: `"${alias}"."${fieldName}" IS BLANK`
127
+ clause: `${alias}.${fieldName} IS BLANK`
130
128
  }
131
129
  case 'is_empty_num_id':
132
130
  return {
133
- clause: `"${alias}"."${fieldName}" IS EMPTY NUMERIC ID`
131
+ clause: `${alias}.${fieldName} IS EMPTY NUMERIC ID`
134
132
  }
135
133
 
136
134
  case 'between':
137
135
  return {
138
- clause: `"${alias}"."${fieldName}" BETWEEN :args${seq}_1 AND :args${seq}_2`,
136
+ clause: `${alias}.${fieldName} BETWEEN :args${seq}_1 AND :args${seq}_2`,
139
137
  parameters: { [`args${seq}_1`]: value[0], [`args${seq}_2`]: value[1] }
140
138
  }
141
139
  }
@@ -23,7 +23,7 @@ export const buildQuery = function (queryBuilder: any, params: any, context: any
23
23
  }
24
24
 
25
25
  if (domainRef) {
26
- queryBuilder.andWhere(`"${queryBuilder.alias}"."domain_id" = :domainId`, { domainId })
26
+ queryBuilder.andWhere(`${queryBuilder.alias}.domain = :domainId`, { domainId })
27
27
  }
28
28
 
29
29
  if (pagination && pagination.page > 0 && pagination.limit > 0) {