@lenne.tech/nest-server 10.8.9 → 10.8.11

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 (212) hide show
  1. package/dist/core/common/args/filter.args.js +2 -2
  2. package/dist/core/common/args/filter.args.js.map +1 -1
  3. package/dist/core/common/args/pagination.args.js +5 -5
  4. package/dist/core/common/args/pagination.args.js.map +1 -1
  5. package/dist/core/common/helpers/config.helper.d.ts +1 -1
  6. package/dist/core/common/helpers/config.helper.js +8 -8
  7. package/dist/core/common/helpers/config.helper.js.map +1 -1
  8. package/dist/core/common/helpers/db.helper.d.ts +11 -11
  9. package/dist/core/common/helpers/db.helper.js +100 -100
  10. package/dist/core/common/helpers/db.helper.js.map +1 -1
  11. package/dist/core/common/helpers/file.helper.d.ts +1 -1
  12. package/dist/core/common/helpers/file.helper.js +10 -10
  13. package/dist/core/common/helpers/file.helper.js.map +1 -1
  14. package/dist/core/common/helpers/filter.helper.d.ts +12 -12
  15. package/dist/core/common/helpers/filter.helper.js +19 -19
  16. package/dist/core/common/helpers/filter.helper.js.map +1 -1
  17. package/dist/core/common/helpers/graphql.helper.d.ts +8 -8
  18. package/dist/core/common/helpers/graphql.helper.js +75 -75
  19. package/dist/core/common/helpers/graphql.helper.js.map +1 -1
  20. package/dist/core/common/helpers/input.helper.d.ts +7 -7
  21. package/dist/core/common/helpers/input.helper.js +60 -60
  22. package/dist/core/common/helpers/input.helper.js.map +1 -1
  23. package/dist/core/common/helpers/model.helper.d.ts +8 -8
  24. package/dist/core/common/helpers/model.helper.js +38 -38
  25. package/dist/core/common/helpers/model.helper.js.map +1 -1
  26. package/dist/core/common/helpers/service.helper.d.ts +2 -2
  27. package/dist/core/common/helpers/table.helper.d.ts +1 -1
  28. package/dist/core/common/inputs/combined-filter.input.js +6 -6
  29. package/dist/core/common/inputs/combined-filter.input.js.map +1 -1
  30. package/dist/core/common/inputs/filter.input.js +6 -6
  31. package/dist/core/common/inputs/filter.input.js.map +1 -1
  32. package/dist/core/common/inputs/single-filter.input.js +10 -10
  33. package/dist/core/common/inputs/single-filter.input.js.map +1 -1
  34. package/dist/core/common/inputs/sort.input.js +4 -4
  35. package/dist/core/common/inputs/sort.input.js.map +1 -1
  36. package/dist/core/common/interfaces/server-options.interface.d.ts +8 -8
  37. package/dist/core/common/models/core-persistence.model.js +8 -8
  38. package/dist/core/common/models/core-persistence.model.js.map +1 -1
  39. package/dist/core/common/plugins/mongoose-id.plugin.d.ts +1 -1
  40. package/dist/core/common/plugins/mongoose-id.plugin.js +2 -2
  41. package/dist/core/common/plugins/mongoose-id.plugin.js.map +1 -1
  42. package/dist/core/common/scalars/any.scalar.js +8 -8
  43. package/dist/core/common/scalars/any.scalar.js.map +1 -1
  44. package/dist/core/common/scalars/date-timestamp.scalar.js +1 -1
  45. package/dist/core/common/scalars/date-timestamp.scalar.js.map +1 -1
  46. package/dist/core/common/scalars/date.scalar.js +1 -1
  47. package/dist/core/common/scalars/date.scalar.js.map +1 -1
  48. package/dist/core/common/scalars/json.scalar.js +8 -8
  49. package/dist/core/common/scalars/json.scalar.js.map +1 -1
  50. package/dist/core/common/services/config.service.d.ts +26 -26
  51. package/dist/core/common/services/crud.service.d.ts +32 -32
  52. package/dist/core/common/services/mailjet.service.js +1 -1
  53. package/dist/core/common/services/mailjet.service.js.map +1 -1
  54. package/dist/core/common/services/module.service.d.ts +1 -1
  55. package/dist/core/common/services/module.service.js.map +1 -1
  56. package/dist/core/common/types/falsy.type.d.ts +1 -1
  57. package/dist/core/common/types/id.type.d.ts +4 -4
  58. package/dist/core/common/types/require-only-one.type.d.ts +2 -2
  59. package/dist/core/common/types/required-at-least-one.type.d.ts +2 -2
  60. package/dist/core/common/types/string-or-object-id.type.d.ts +1 -1
  61. package/dist/core/modules/auth/core-auth.controller.js +6 -6
  62. package/dist/core/modules/auth/core-auth.controller.js.map +1 -1
  63. package/dist/core/modules/auth/core-auth.model.js +2 -2
  64. package/dist/core/modules/auth/core-auth.model.js.map +1 -1
  65. package/dist/core/modules/auth/core-auth.module.d.ts +4 -4
  66. package/dist/core/modules/auth/core-auth.module.js +2 -2
  67. package/dist/core/modules/auth/core-auth.module.js.map +1 -1
  68. package/dist/core/modules/auth/core-auth.resolver.js +6 -6
  69. package/dist/core/modules/auth/core-auth.resolver.js.map +1 -1
  70. package/dist/core/modules/auth/guards/auth.guard.d.ts +2 -2
  71. package/dist/core/modules/auth/inputs/core-auth-sign-in.input.js +2 -2
  72. package/dist/core/modules/auth/inputs/core-auth-sign-in.input.js.map +1 -1
  73. package/dist/core/modules/auth/inputs/core-auth-sign-up.input.js +2 -2
  74. package/dist/core/modules/auth/inputs/core-auth-sign-up.input.js.map +1 -1
  75. package/dist/core/modules/auth/services/core-auth.service.d.ts +2 -2
  76. package/dist/core/modules/file/core-file-info.model.js +8 -8
  77. package/dist/core/modules/file/core-file-info.model.js.map +1 -1
  78. package/dist/core/modules/file/core-file.controller.js +2 -2
  79. package/dist/core/modules/file/core-file.controller.js.map +1 -1
  80. package/dist/core/modules/file/core-file.resolver.js +6 -6
  81. package/dist/core/modules/file/core-file.resolver.js.map +1 -1
  82. package/dist/core/modules/file/core-file.service.d.ts +6 -6
  83. package/dist/core/modules/health-check/core-health-check-result.model.js +9 -9
  84. package/dist/core/modules/health-check/core-health-check-result.model.js.map +1 -1
  85. package/dist/core/modules/health-check/core-health-check.controller.js +2 -2
  86. package/dist/core/modules/health-check/core-health-check.controller.js.map +1 -1
  87. package/dist/core/modules/health-check/core-health-check.resolver.js +2 -2
  88. package/dist/core/modules/health-check/core-health-check.resolver.js.map +1 -1
  89. package/dist/core/modules/user/core-user.model.js +11 -11
  90. package/dist/core/modules/user/core-user.model.js.map +1 -1
  91. package/dist/core/modules/user/core-user.service.d.ts +2 -2
  92. package/dist/core/modules/user/inputs/core-user-create.input.js +3 -3
  93. package/dist/core/modules/user/inputs/core-user-create.input.js.map +1 -1
  94. package/dist/core/modules/user/inputs/core-user.input.js +10 -10
  95. package/dist/core/modules/user/inputs/core-user.input.js.map +1 -1
  96. package/dist/index.d.ts +8 -8
  97. package/dist/index.js +8 -8
  98. package/dist/index.js.map +1 -1
  99. package/dist/server/common/models/persistence.model.d.ts +2 -2
  100. package/dist/server/common/models/persistence.model.js +6 -6
  101. package/dist/server/common/models/persistence.model.js.map +1 -1
  102. package/dist/server/modules/auth/auth.controller.js +1 -1
  103. package/dist/server/modules/auth/auth.controller.js.map +1 -1
  104. package/dist/server/modules/auth/auth.model.js +3 -3
  105. package/dist/server/modules/auth/auth.model.js.map +1 -1
  106. package/dist/server/modules/auth/auth.resolver.js +3 -3
  107. package/dist/server/modules/auth/auth.resolver.js.map +1 -1
  108. package/dist/server/modules/auth/inputs/auth-sign-in.input.js +2 -2
  109. package/dist/server/modules/auth/inputs/auth-sign-in.input.js.map +1 -1
  110. package/dist/server/modules/auth/inputs/auth-sign-up.input.js +4 -4
  111. package/dist/server/modules/auth/inputs/auth-sign-up.input.js.map +1 -1
  112. package/dist/server/modules/file/file-info.model.js +2 -2
  113. package/dist/server/modules/file/file-info.model.js.map +1 -1
  114. package/dist/server/modules/file/file.controller.js +5 -5
  115. package/dist/server/modules/file/file.controller.js.map +1 -1
  116. package/dist/server/modules/file/file.resolver.js +5 -5
  117. package/dist/server/modules/file/file.resolver.js.map +1 -1
  118. package/dist/server/modules/user/avatar.controller.js +2 -2
  119. package/dist/server/modules/user/avatar.controller.js.map +1 -1
  120. package/dist/server/modules/user/inputs/user-create.input.js +2 -2
  121. package/dist/server/modules/user/inputs/user-create.input.js.map +1 -1
  122. package/dist/server/modules/user/inputs/user.input.js +2 -2
  123. package/dist/server/modules/user/inputs/user.input.js.map +1 -1
  124. package/dist/server/modules/user/outputs/find-and-count-users-result.output.js +4 -4
  125. package/dist/server/modules/user/outputs/find-and-count-users-result.output.js.map +1 -1
  126. package/dist/server/modules/user/user.model.js +8 -8
  127. package/dist/server/modules/user/user.model.js.map +1 -1
  128. package/dist/server/modules/user/user.resolver.d.ts +1 -1
  129. package/dist/server/modules/user/user.resolver.js +11 -11
  130. package/dist/server/modules/user/user.resolver.js.map +1 -1
  131. package/dist/server/modules/user/user.service.d.ts +1 -1
  132. package/dist/server/server.controller.js +2 -2
  133. package/dist/server/server.controller.js.map +1 -1
  134. package/dist/test/test.helper.d.ts +14 -14
  135. package/dist/test/test.helper.js +13 -3
  136. package/dist/test/test.helper.js.map +1 -1
  137. package/dist/tsconfig.build.tsbuildinfo +1 -1
  138. package/package.json +22 -19
  139. package/src/core/common/args/filter.args.ts +2 -2
  140. package/src/core/common/args/pagination.args.ts +5 -5
  141. package/src/core/common/decorators/graphql-service-options.decorator.ts +1 -1
  142. package/src/core/common/decorators/graphql-user.decorator.ts +1 -1
  143. package/src/core/common/decorators/rest-user.decorator.ts +1 -1
  144. package/src/core/common/helpers/config.helper.ts +22 -21
  145. package/src/core/common/helpers/db.helper.ts +164 -164
  146. package/src/core/common/helpers/file.helper.ts +18 -18
  147. package/src/core/common/helpers/filter.helper.ts +35 -35
  148. package/src/core/common/helpers/graphql.helper.ts +100 -100
  149. package/src/core/common/helpers/input.helper.ts +89 -89
  150. package/src/core/common/helpers/model.helper.ts +70 -70
  151. package/src/core/common/helpers/service.helper.ts +3 -3
  152. package/src/core/common/helpers/table.helper.ts +1 -1
  153. package/src/core/common/inputs/combined-filter.input.ts +5 -5
  154. package/src/core/common/inputs/filter.input.ts +5 -5
  155. package/src/core/common/inputs/single-filter.input.ts +9 -9
  156. package/src/core/common/inputs/sort.input.ts +3 -3
  157. package/src/core/common/interfaces/server-options.interface.ts +9 -9
  158. package/src/core/common/models/core-persistence.model.ts +8 -8
  159. package/src/core/common/plugins/mongoose-id.plugin.js +2 -2
  160. package/src/core/common/scalars/any.scalar.ts +8 -8
  161. package/src/core/common/scalars/date-timestamp.scalar.ts +1 -1
  162. package/src/core/common/scalars/date.scalar.ts +1 -1
  163. package/src/core/common/scalars/json.scalar.ts +8 -8
  164. package/src/core/common/services/config.service.ts +7 -7
  165. package/src/core/common/services/crud.service.ts +19 -19
  166. package/src/core/common/services/mailjet.service.ts +1 -1
  167. package/src/core/common/services/module.service.ts +4 -4
  168. package/src/core/common/types/falsy.type.ts +1 -1
  169. package/src/core/common/types/id.type.ts +1 -1
  170. package/src/core/common/types/require-only-one.type.ts +3 -3
  171. package/src/core/common/types/required-at-least-one.type.ts +3 -3
  172. package/src/core/common/types/string-or-object-id.type.ts +1 -1
  173. package/src/core/modules/auth/core-auth.controller.ts +6 -6
  174. package/src/core/modules/auth/core-auth.model.ts +1 -1
  175. package/src/core/modules/auth/core-auth.module.ts +4 -4
  176. package/src/core/modules/auth/core-auth.resolver.ts +6 -6
  177. package/src/core/modules/auth/guards/auth.guard.ts +3 -3
  178. package/src/core/modules/auth/inputs/core-auth-sign-in.input.ts +1 -1
  179. package/src/core/modules/auth/inputs/core-auth-sign-up.input.ts +1 -1
  180. package/src/core/modules/auth/services/core-auth.service.ts +1 -1
  181. package/src/core/modules/auth/tokens.decorator.ts +2 -2
  182. package/src/core/modules/file/core-file-info.model.ts +7 -7
  183. package/src/core/modules/file/core-file.controller.ts +2 -2
  184. package/src/core/modules/file/core-file.resolver.ts +6 -6
  185. package/src/core/modules/file/core-file.service.ts +6 -6
  186. package/src/core/modules/health-check/core-health-check-result.model.ts +8 -8
  187. package/src/core/modules/health-check/core-health-check.controller.ts +2 -2
  188. package/src/core/modules/health-check/core-health-check.resolver.ts +2 -2
  189. package/src/core/modules/user/core-user.model.ts +11 -11
  190. package/src/core/modules/user/core-user.service.ts +2 -2
  191. package/src/core/modules/user/inputs/core-user-create.input.ts +2 -2
  192. package/src/core/modules/user/inputs/core-user.input.ts +9 -9
  193. package/src/index.ts +9 -9
  194. package/src/server/common/models/persistence.model.ts +8 -8
  195. package/src/server/modules/auth/auth.controller.ts +1 -1
  196. package/src/server/modules/auth/auth.model.ts +2 -2
  197. package/src/server/modules/auth/auth.resolver.ts +3 -3
  198. package/src/server/modules/auth/inputs/auth-sign-in.input.ts +1 -1
  199. package/src/server/modules/auth/inputs/auth-sign-up.input.ts +3 -3
  200. package/src/server/modules/file/file-info.model.ts +2 -2
  201. package/src/server/modules/file/file.controller.ts +5 -5
  202. package/src/server/modules/file/file.module.ts +1 -1
  203. package/src/server/modules/file/file.resolver.ts +5 -5
  204. package/src/server/modules/user/avatar.controller.ts +2 -2
  205. package/src/server/modules/user/inputs/user-create.input.ts +1 -1
  206. package/src/server/modules/user/inputs/user.input.ts +1 -1
  207. package/src/server/modules/user/outputs/find-and-count-users-result.output.ts +3 -3
  208. package/src/server/modules/user/user.model.ts +8 -8
  209. package/src/server/modules/user/user.resolver.ts +11 -11
  210. package/src/server/modules/user/user.service.ts +1 -1
  211. package/src/server/server.controller.ts +2 -2
  212. package/src/test/test.helper.ts +31 -17
@@ -26,7 +26,7 @@ export class Filter {
26
26
  /**
27
27
  * Generate filter query
28
28
  */
29
- public static generateFilterQuery<T = any>(filter?: Partial<FilterInput>): FilterQuery<T> | any {
29
+ public static generateFilterQuery<T = any>(filter?: Partial<FilterInput>): any | FilterQuery<T> {
30
30
  return generateFilterQuery(filter);
31
31
  }
32
32
 
@@ -38,6 +38,39 @@ export class Filter {
38
38
  }
39
39
  }
40
40
 
41
+ /**
42
+ * Convert filter arguments to a query array
43
+ */
44
+ export function convertFilterArgsToQuery<T = any>(filterArgs: Partial<FilterArgs>): [FilterQuery<T>, QueryOptions] {
45
+ return [generateFilterQuery(filterArgs?.filter), generateFindOptions(filterArgs)];
46
+ }
47
+
48
+ /**
49
+ * Merge FilterArgs and FilterQueries
50
+ */
51
+ export function filterMerge<T = unknown>(
52
+ filterArgs: Partial<FilterArgs>,
53
+ filterQuery: Partial<FilterQuery<T>>,
54
+ options?: {
55
+ operator?: '$and' | '$nor' | '$or';
56
+ queryOptions?: Partial<QueryOptions>;
57
+ samples?: number;
58
+ },
59
+ ): { filterQuery: FilterQuery<T>; queryOptions?: QueryOptions; samples?: number } {
60
+ const config = {
61
+ operator: '$and',
62
+ ...options,
63
+ };
64
+ const converted = convertFilterArgsToQuery(filterArgs);
65
+ return {
66
+ filterQuery: (converted[0]
67
+ ? { [config.operator]: [converted[0], filterQuery || {}] }
68
+ : filterQuery) as FilterQuery<T>,
69
+ queryOptions: converted[1] || config.queryOptions ? { ...converted[1], ...config.queryOptions } : undefined,
70
+ samples: config.samples,
71
+ };
72
+ }
73
+
41
74
  /**
42
75
  * Helper function to create $and, $or or $nor filter
43
76
  */
@@ -104,20 +137,13 @@ export function findFilter(options?: {
104
137
  return filterOptions;
105
138
  }
106
139
 
107
- /**
108
- * Convert filter arguments to a query array
109
- */
110
- export function convertFilterArgsToQuery<T = any>(filterArgs: Partial<FilterArgs>): [FilterQuery<T>, QueryOptions] {
111
- return [generateFilterQuery(filterArgs?.filter), generateFindOptions(filterArgs)];
112
- }
113
-
114
140
  /**
115
141
  * Generate filter query
116
142
  */
117
143
  export function generateFilterQuery<T = any>(
118
144
  filter?: Partial<FilterInput>,
119
145
  options?: { automaticObjectIdFiltering?: boolean },
120
- ): FilterQuery<T> | any {
146
+ ): any | FilterQuery<T> {
121
147
  // Check filter
122
148
  if (!filter) {
123
149
  return undefined;
@@ -263,29 +289,3 @@ export function generateFindOptions(filterArgs: Partial<FilterArgs>, options?: {
263
289
 
264
290
  return queryOptions;
265
291
  }
266
-
267
- /**
268
- * Merge FilterArgs and FilterQueries
269
- */
270
- export function filterMerge<T = unknown>(
271
- filterArgs: Partial<FilterArgs>,
272
- filterQuery: Partial<FilterQuery<T>>,
273
- options?: {
274
- operator?: '$and' | '$nor' | '$or';
275
- queryOptions?: Partial<QueryOptions>;
276
- samples?: number;
277
- },
278
- ): { filterQuery: FilterQuery<T>; queryOptions?: QueryOptions; samples?: number } {
279
- const config = {
280
- operator: '$and',
281
- ...options,
282
- };
283
- const converted = convertFilterArgsToQuery(filterArgs);
284
- return {
285
- filterQuery: (converted[0]
286
- ? { [config.operator]: [converted[0], filterQuery || {}] }
287
- : filterQuery) as FilterQuery<T>,
288
- queryOptions: converted[1] || config.queryOptions ? { ...converted[1], ...config.queryOptions } : undefined,
289
- samples: config.samples,
290
- };
291
- }
@@ -139,48 +139,59 @@ export default class GraphQLHelper {
139
139
  }
140
140
 
141
141
  /**
142
- * Check if AST is a Field
143
- * @param ast
142
+ * Create flatten AST from FieldNode for GraphQLResolveInfo
144
143
  */
145
- export function isFieldNode(ast: SelectionNode): ast is FieldNode {
146
- return ast.kind === 'Field';
147
- }
144
+ export function flattenAST(
145
+ ast: FieldNode,
146
+ info: GraphQLResolveInfo,
147
+ obj: any = {},
148
+ config: Partial<GraphQLFieldsConfig> = {},
149
+ ) {
150
+ // Process configuration
151
+ config = Object.assign(
152
+ {
153
+ excludedFields: [],
154
+ processArguments: false,
155
+ },
156
+ config,
157
+ );
148
158
 
149
- /**
150
- * Check if ValueNode has a value field
151
- */
152
- export function isValueNodeWithValueField(value: ValueNode): value is ValueNodeWithValueField {
153
- return value.kind !== 'NullValue' && value.kind !== 'ListValue' && value.kind !== 'ObjectValue';
154
- }
159
+ return getASTSelections(ast).reduce((flattened, a) => {
160
+ if (a.directives && a.directives.length) {
161
+ const { shouldInclude, shouldSkip } = getDirectiveResults(a, info);
162
+ // Field/fragment is not included if either the @skip condition is true or the @include condition is false
163
+ // https://facebook.github.io/graphql/draft/#sec--include
164
+ if (shouldSkip || !shouldInclude) {
165
+ return flattened;
166
+ }
167
+ }
155
168
 
156
- /**
157
- * Check ValueNode is a ListValue
158
- * @param value
159
- */
160
- export function isListValueNode(value: ValueNode): value is ListValueNode {
161
- return value.kind === 'ListValue';
162
- }
169
+ if (isFieldNode(a)) {
170
+ const name = a.name.value;
171
+ if (config.excludedFields.indexOf(name) !== -1) {
172
+ return flattened;
173
+ }
163
174
 
164
- /**
165
- * Get selections of AST
166
- */
167
- export function getASTSelections(ast: FieldNode) {
168
- if (ast && ast.selectionSet && ast.selectionSet.selections && ast.selectionSet.selections.length) {
169
- return ast.selectionSet.selections;
170
- }
175
+ if (flattened[name] && flattened[name] !== '__arguments') {
176
+ Object.assign(flattened[name], flattenAST(a, info, flattened[name]));
177
+ } else {
178
+ flattened[name] = flattenAST(a, info);
179
+ }
171
180
 
172
- return [];
173
- }
181
+ if (config.processArguments) {
182
+ // check if the current field has arguments
183
+ if (a.arguments && a.arguments.length) {
184
+ Object.assign(flattened[name], {
185
+ __arguments: getArguments(a),
186
+ });
187
+ }
188
+ }
189
+ } else {
190
+ flattened = flattenAST(getAST(a, info), info, flattened);
191
+ }
174
192
 
175
- /**
176
- * Get AST
177
- */
178
- export function getAST(ast, info) {
179
- if (ast.kind === 'FragmentSpread') {
180
- const fragmentName = ast.name.value;
181
- return info.fragments[fragmentName];
182
- }
183
- return ast;
193
+ return flattened;
194
+ }, obj);
184
195
  }
185
196
 
186
197
  /**
@@ -203,18 +214,25 @@ export function getArguments(ast: FieldNode) {
203
214
  }
204
215
 
205
216
  /**
206
- * Get directive value from DirectiveNode for GraphQLResolveInfo
217
+ * Get AST
207
218
  */
208
- export function getDirectiveValue(directive: DirectiveNode, info: GraphQLResolveInfo) {
209
- const arg = directive.arguments?.[0]; // only arg on an include or skip directive is "if"
210
- if (!arg) {
211
- return;
219
+ export function getAST(ast, info) {
220
+ if (ast.kind === 'FragmentSpread') {
221
+ const fragmentName = ast.name.value;
222
+ return info.fragments[fragmentName];
212
223
  }
213
- if (arg.value.kind !== 'Variable') {
214
- const valueNode = arg.value;
215
- return isValueNodeWithValueField(valueNode) ? !!valueNode.value : false;
224
+ return ast;
225
+ }
226
+
227
+ /**
228
+ * Get selections of AST
229
+ */
230
+ export function getASTSelections(ast: FieldNode) {
231
+ if (ast && ast.selectionSet && ast.selectionSet.selections && ast.selectionSet.selections.length) {
232
+ return ast.selectionSet.selections;
216
233
  }
217
- return info.variableValues[arg.value.name.value];
234
+
235
+ return [];
218
236
  }
219
237
 
220
238
  /**
@@ -246,59 +264,18 @@ export function getDirectiveResults(ast: SelectionNode, info: GraphQLResolveInfo
246
264
  }
247
265
 
248
266
  /**
249
- * Create flatten AST from FieldNode for GraphQLResolveInfo
267
+ * Get directive value from DirectiveNode for GraphQLResolveInfo
250
268
  */
251
- export function flattenAST(
252
- ast: FieldNode,
253
- info: GraphQLResolveInfo,
254
- obj: any = {},
255
- config: Partial<GraphQLFieldsConfig> = {},
256
- ) {
257
- // Process configuration
258
- config = Object.assign(
259
- {
260
- excludedFields: [],
261
- processArguments: false,
262
- },
263
- config,
264
- );
265
-
266
- return getASTSelections(ast).reduce((flattened, a) => {
267
- if (a.directives && a.directives.length) {
268
- const { shouldInclude, shouldSkip } = getDirectiveResults(a, info);
269
- // Field/fragment is not included if either the @skip condition is true or the @include condition is false
270
- // https://facebook.github.io/graphql/draft/#sec--include
271
- if (shouldSkip || !shouldInclude) {
272
- return flattened;
273
- }
274
- }
275
-
276
- if (isFieldNode(a)) {
277
- const name = a.name.value;
278
- if (config.excludedFields.indexOf(name) !== -1) {
279
- return flattened;
280
- }
281
-
282
- if (flattened[name] && flattened[name] !== '__arguments') {
283
- Object.assign(flattened[name], flattenAST(a, info, flattened[name]));
284
- } else {
285
- flattened[name] = flattenAST(a, info);
286
- }
287
-
288
- if (config.processArguments) {
289
- // check if the current field has arguments
290
- if (a.arguments && a.arguments.length) {
291
- Object.assign(flattened[name], {
292
- __arguments: getArguments(a),
293
- });
294
- }
295
- }
296
- } else {
297
- flattened = flattenAST(getAST(a, info), info, flattened);
298
- }
299
-
300
- return flattened;
301
- }, obj);
269
+ export function getDirectiveValue(directive: DirectiveNode, info: GraphQLResolveInfo) {
270
+ const arg = directive.arguments?.[0]; // only arg on an include or skip directive is "if"
271
+ if (!arg) {
272
+ return;
273
+ }
274
+ if (arg.value.kind !== 'Variable') {
275
+ const valueNode = arg.value;
276
+ return isValueNodeWithValueField(valueNode) ? !!valueNode.value : false;
277
+ }
278
+ return info.variableValues[arg.value.name.value];
302
279
  }
303
280
 
304
281
  /**
@@ -326,10 +303,11 @@ export function getFields(
326
303
  }
327
304
 
328
305
  /**
329
- * Check if field is in GraphQLResolveInfo
306
+ * Check if AST is a Field
307
+ * @param ast
330
308
  */
331
- export function isInGraphQLResolveInfo(path: any, info: GraphQLResolveInfo, config: Partial<GraphQLFieldsConfig> = {}) {
332
- return _.has(getFields(info, config), path);
309
+ export function isFieldNode(ast: SelectionNode): ast is FieldNode {
310
+ return ast.kind === 'Field';
333
311
  }
334
312
 
335
313
  /**
@@ -338,3 +316,25 @@ export function isInGraphQLResolveInfo(path: any, info: GraphQLResolveInfo, conf
338
316
  export function isInFields(path: any, fields: any) {
339
317
  return _.has(fields, path);
340
318
  }
319
+
320
+ /**
321
+ * Check if field is in GraphQLResolveInfo
322
+ */
323
+ export function isInGraphQLResolveInfo(path: any, info: GraphQLResolveInfo, config: Partial<GraphQLFieldsConfig> = {}) {
324
+ return _.has(getFields(info, config), path);
325
+ }
326
+
327
+ /**
328
+ * Check ValueNode is a ListValue
329
+ * @param value
330
+ */
331
+ export function isListValueNode(value: ValueNode): value is ListValueNode {
332
+ return value.kind === 'ListValue';
333
+ }
334
+
335
+ /**
336
+ * Check if ValueNode has a value field
337
+ */
338
+ export function isValueNodeWithValueField(value: ValueNode): value is ValueNodeWithValueField {
339
+ return value.kind !== 'NullValue' && value.kind !== 'ListValue' && value.kind !== 'ObjectValue';
340
+ }
@@ -466,6 +466,29 @@ export function getPlain(object: any) {
466
466
  return JSON.parse(JSON.stringify(object));
467
467
  }
468
468
 
469
+ /**
470
+ * Get instance of array items (via first item)
471
+ */
472
+ export function instanceofArray(arr: any[], strict = false): string {
473
+ let constructor: string = undefined;
474
+ if (!arr?.length) {
475
+ return constructor;
476
+ }
477
+ try {
478
+ constructor = arr[0].constructor;
479
+ if (strict) {
480
+ for (const item of arr) {
481
+ if (item.constructor !== constructor) {
482
+ return undefined;
483
+ }
484
+ }
485
+ }
486
+ } catch (e) {
487
+ return undefined;
488
+ }
489
+ return constructor;
490
+ }
491
+
469
492
  /**
470
493
  * Check if parameter is an array
471
494
  */
@@ -605,13 +628,6 @@ export function isObject(parameter: any, falseFunction: (...params) => any = err
605
628
  : falseFunction(isObject);
606
629
  }
607
630
 
608
- /**
609
- * Check whether the parameter can be converted to true
610
- */
611
- export function isTrue(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
612
- return !!parameter ? true : falseFunction(isTrue);
613
- }
614
-
615
631
  /**
616
632
  * Check if parameter is a string
617
633
  */
@@ -620,41 +636,10 @@ export function isString(parameter: string, falseFunction: (...params) => any =
620
636
  }
621
637
 
622
638
  /**
623
- * Merge plain objects deep into target object and ignores undefined
624
- */
625
- export function mergePlain(target: Record<any, any>, ...args: Record<any, any>[]): any {
626
- return merge(
627
- target,
628
- ...args.map(
629
- // Prepare records
630
- item =>
631
- // Return item if not an object or cloned record with undefined properties removed
632
- !item ? item : filterProperties(clone(item, { circles: false }), prop => prop !== undefined),
633
- ),
634
- );
635
- }
636
-
637
- /**
638
- * Alternative for errorFunction
639
- */
640
- export function returnFalse(): boolean {
641
- return false;
642
- }
643
-
644
- /**
645
- * Match function to use instead of switch case
646
- * Inspired by https://yusfuu.medium.com/dont-use-switch-or-if-else-in-javascript-instead-try-this-82f32616c269
647
- *
648
- * Example:
649
- * const matched = match(expr, {
650
- * Oranges: 'Oranges are $0.59 a pound.',
651
- * Mangoes: 'Mangoes and papayas are $2.79 a pound.',
652
- * Papayas: 'Mangoes and papayas are $2.79 a pound.',
653
- * default: `Sorry, we are out of ${expr}.`,
654
- * });
639
+ * Check whether the parameter can be converted to true
655
640
  */
656
- export function match(expression: any, cases: Record<any, any>): any {
657
- return cases[expression] || cases?.default;
641
+ export function isTrue(parameter: any, falseFunction: (...params) => any = errorFunction): boolean {
642
+ return !!parameter ? true : falseFunction(isTrue);
658
643
  }
659
644
 
660
645
  /**
@@ -684,45 +669,50 @@ export function mapClass<T>(
684
669
  }
685
670
 
686
671
  /**
687
- * Get type of array (via first item)
672
+ * Match function to use instead of switch case
673
+ * Inspired by https://yusfuu.medium.com/dont-use-switch-or-if-else-in-javascript-instead-try-this-82f32616c269
674
+ *
675
+ * Example:
676
+ * const matched = match(expr, {
677
+ * Oranges: 'Oranges are $0.59 a pound.',
678
+ * Mangoes: 'Mangoes and papayas are $2.79 a pound.',
679
+ * Papayas: 'Mangoes and papayas are $2.79 a pound.',
680
+ * default: `Sorry, we are out of ${expr}.`,
681
+ * });
688
682
  */
689
- export function typeofArray(arr: any[], strict = false): string {
690
- let type: string = undefined;
691
- if (!arr?.length) {
692
- return type;
693
- }
694
- type = typeof arr[0];
695
- if (strict) {
696
- for (const item of arr) {
697
- if (typeof item !== type) {
698
- return undefined;
699
- }
700
- }
701
- }
702
- return type;
683
+ export function match(expression: any, cases: Record<any, any>): any {
684
+ return cases[expression] || cases?.default;
703
685
  }
704
686
 
705
687
  /**
706
- * Get instance of array items (via first item)
688
+ * Merge plain objects deep into target object and ignores undefined
707
689
  */
708
- export function instanceofArray(arr: any[], strict = false): string {
709
- let constructor: string = undefined;
710
- if (!arr?.length) {
711
- return constructor;
690
+ export function mergePlain(target: Record<any, any>, ...args: Record<any, any>[]): any {
691
+ return merge(
692
+ target,
693
+ ...args.map(
694
+ // Prepare records
695
+ item =>
696
+ // Return item if not an object or cloned record with undefined properties removed
697
+ !item ? item : filterProperties(clone(item, { circles: false }), prop => prop !== undefined),
698
+ ),
699
+ );
700
+ }
701
+
702
+ /**
703
+ * Helper to avoid very slow merge of serviceOptions
704
+ */
705
+ export function prepareServiceOptionsForCreate(serviceOptions: any) {
706
+ if (!serviceOptions) {
707
+ serviceOptions = {};
712
708
  }
713
- try {
714
- constructor = arr[0].constructor;
715
- if (strict) {
716
- for (const item of arr) {
717
- if (item.constructor !== constructor) {
718
- return undefined;
719
- }
720
- }
721
- }
722
- } catch (e) {
723
- return undefined;
709
+ if (!serviceOptions.prepareInput) {
710
+ serviceOptions.prepareInput = {};
724
711
  }
725
- return constructor;
712
+ if (serviceOptions.prepareInput.create === undefined) {
713
+ serviceOptions.prepareInput.create = true;
714
+ }
715
+ return serviceOptions;
726
716
  }
727
717
 
728
718
  /**
@@ -794,22 +784,6 @@ export function processDeep(
794
784
  return func(data);
795
785
  }
796
786
 
797
- /**
798
- * Helper to avoid very slow merge of serviceOptions
799
- */
800
- export function prepareServiceOptionsForCreate(serviceOptions: any) {
801
- if (!serviceOptions) {
802
- serviceOptions = {};
803
- }
804
- if (!serviceOptions.prepareInput) {
805
- serviceOptions.prepareInput = {};
806
- }
807
- if (serviceOptions.prepareInput.create === undefined) {
808
- serviceOptions.prepareInput.create = true;
809
- }
810
- return serviceOptions;
811
- }
812
-
813
787
  /**
814
788
  * Remove properties deep
815
789
  */
@@ -857,3 +831,29 @@ export function removePropertiesDeep(
857
831
  // Process others
858
832
  return data;
859
833
  }
834
+
835
+ /**
836
+ * Alternative for errorFunction
837
+ */
838
+ export function returnFalse(): boolean {
839
+ return false;
840
+ }
841
+
842
+ /**
843
+ * Get type of array (via first item)
844
+ */
845
+ export function typeofArray(arr: any[], strict = false): string {
846
+ let type: string = undefined;
847
+ if (!arr?.length) {
848
+ return type;
849
+ }
850
+ type = typeof arr[0];
851
+ if (strict) {
852
+ for (const item of arr) {
853
+ if (typeof item !== type) {
854
+ return undefined;
855
+ }
856
+ }
857
+ }
858
+ return type;
859
+ }