@smartive/graphql-magic 15.4.0 → 16.0.0

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 (63) hide show
  1. package/.gqmrc.json +4 -2
  2. package/CHANGELOG.md +1 -6
  3. package/dist/bin/gqm.cjs +115 -42
  4. package/dist/cjs/index.cjs +111 -30
  5. package/dist/esm/api/execute.d.ts +1 -1
  6. package/dist/esm/context.d.ts +5 -4
  7. package/dist/esm/db/generate.d.ts +2 -1
  8. package/dist/esm/db/generate.js +13 -8
  9. package/dist/esm/db/generate.js.map +1 -1
  10. package/dist/esm/index.d.ts +1 -0
  11. package/dist/esm/index.js +1 -0
  12. package/dist/esm/index.js.map +1 -1
  13. package/dist/esm/migrations/generate.d.ts +1 -0
  14. package/dist/esm/migrations/generate.js +28 -6
  15. package/dist/esm/migrations/generate.js.map +1 -1
  16. package/dist/esm/models/mutation-hook.d.ts +5 -12
  17. package/dist/esm/models/utils.d.ts +20 -7
  18. package/dist/esm/models/utils.js +8 -0
  19. package/dist/esm/models/utils.js.map +1 -1
  20. package/dist/esm/permissions/check.d.ts +2 -3
  21. package/dist/esm/permissions/check.js.map +1 -1
  22. package/dist/esm/resolvers/arguments.d.ts +1 -1
  23. package/dist/esm/resolvers/mutations.js +5 -2
  24. package/dist/esm/resolvers/mutations.js.map +1 -1
  25. package/dist/esm/schema/utils.js +19 -8
  26. package/dist/esm/schema/utils.js.map +1 -1
  27. package/dist/esm/utils/dates.d.ts +12 -0
  28. package/dist/esm/utils/dates.js +37 -0
  29. package/dist/esm/utils/dates.js.map +1 -0
  30. package/dist/esm/utils/index.d.ts +1 -0
  31. package/dist/esm/utils/index.js +3 -0
  32. package/dist/esm/utils/index.js.map +1 -0
  33. package/dist/esm/values.d.ts +1 -3
  34. package/docker-compose.yml +0 -1
  35. package/docs/docs/1-tutorial.md +6 -6
  36. package/docs/docs/6-graphql-server.md +1 -3
  37. package/docs/docs/7-graphql-client.md +1 -1
  38. package/docs/docs/8-permissions.md +145 -0
  39. package/docs/package-lock.json +177 -177
  40. package/docs/package.json +6 -6
  41. package/knexfile.ts +2 -2
  42. package/migrations/20230912185644_setup.ts +37 -8
  43. package/package.json +5 -4
  44. package/src/bin/gqm/codegen.ts +4 -3
  45. package/src/bin/gqm/gqm.ts +4 -2
  46. package/src/bin/gqm/settings.ts +37 -2
  47. package/src/bin/gqm/templates.ts +19 -8
  48. package/src/context.ts +9 -5
  49. package/src/db/generate.ts +15 -8
  50. package/src/index.ts +1 -0
  51. package/src/migrations/generate.ts +34 -16
  52. package/src/models/mutation-hook.ts +5 -8
  53. package/src/models/utils.ts +24 -0
  54. package/src/permissions/check.ts +2 -3
  55. package/src/resolvers/mutations.ts +10 -6
  56. package/src/schema/utils.ts +14 -2
  57. package/src/utils/dates.ts +48 -0
  58. package/src/utils/index.ts +3 -0
  59. package/src/values.ts +1 -5
  60. package/tests/generated/client/index.ts +3 -1
  61. package/tests/generated/db/index.ts +43 -43
  62. package/tests/utils/database/seed.ts +9 -5
  63. package/tests/utils/server.ts +3 -3
@@ -1,3 +1,4 @@
1
+ import { Dayjs } from 'dayjs';
1
2
  import {
2
3
  ArgumentNode,
3
4
  DefinitionNode,
@@ -222,7 +223,13 @@ export const value = (val: Value = null): ValueNode =>
222
223
  kind: 'StringValue',
223
224
  value: val.toString(),
224
225
  }
225
- : {
226
+ : val instanceof Dayjs
227
+ ? {
228
+ kind: 'StringValue',
229
+ value: val.toISOString(),
230
+ }
231
+ : typeof val === 'object'
232
+ ? {
226
233
  kind: 'ObjectValue',
227
234
  fields: Object.keys(val).map(
228
235
  (nme): ObjectFieldNode => ({
@@ -231,4 +238,9 @@ export const value = (val: Value = null): ValueNode =>
231
238
  value: value(val[nme]),
232
239
  })
233
240
  ),
234
- };
241
+ }
242
+ : doThrow(`Unsupported value ${val}`);
243
+
244
+ const doThrow = (message: string) => {
245
+ throw new Error(message);
246
+ };
@@ -0,0 +1,48 @@
1
+ import { Dayjs, isDayjs } from 'dayjs';
2
+ import { DateTime } from 'luxon';
3
+
4
+ export type DateLibrary = 'luxon' | 'dayjs';
5
+
6
+ export const DATE_CLASS: { [key in DateLibrary]: string } = {
7
+ luxon: 'DateTime',
8
+ dayjs: 'Dayjs',
9
+ };
10
+
11
+ export const DATE_CLASS_IMPORT = {
12
+ luxon: `import { DateTime } from 'luxon';`,
13
+ dayjs: `import { Dayjs } from 'dayjs';`,
14
+ };
15
+
16
+ export type AnyDateType = DateTime | Dayjs | Date | string;
17
+
18
+ export const anyDateToLuxon = (date: unknown, zone: string | undefined, fallbackToNow = false) => {
19
+ if (!date) {
20
+ if (fallbackToNow) {
21
+ return DateTime.local({ zone });
22
+ } else {
23
+ return undefined;
24
+ }
25
+ }
26
+
27
+ if (DateTime.isDateTime(date)) {
28
+ return date.setZone(zone);
29
+ }
30
+
31
+ if (isDayjs(date)) {
32
+ return DateTime.fromISO(date.toISOString(), { zone });
33
+ }
34
+
35
+ if (date instanceof Date) {
36
+ return DateTime.fromJSDate(date, { zone });
37
+ }
38
+
39
+ if (typeof date === 'string' && date) {
40
+ return DateTime.fromISO(date, { zone });
41
+ }
42
+
43
+ if (typeof date === 'number') {
44
+ return DateTime.fromMillis(date, { zone });
45
+ }
46
+
47
+ throw new Error(`Unsupported date format: ${date} (${date.constructor.name})`);
48
+ };
@@ -0,0 +1,3 @@
1
+ // created from 'create-ts-index'
2
+
3
+ export * from './dates';
package/src/values.ts CHANGED
@@ -1,8 +1,4 @@
1
- import { DateTime } from 'luxon';
2
-
3
- export type BasicValue = undefined | null | boolean | string | number | DateTime;
4
-
5
- export type Value = any; // BasicValue | Symbol | Symbol[] | Record<string, Value> | Value[];
1
+ export type Value = unknown;
6
2
 
7
3
  export type Values = {
8
4
  name: string;
@@ -857,7 +857,7 @@ export type SomeQueryQuery = { manyObjects: Array<{ __typename: 'SomeObject', id
857
857
  export type ReverseFiltersQueryQueryVariables = Exact<{ [key: string]: never; }>;
858
858
 
859
859
 
860
- export type ReverseFiltersQueryQuery = { all: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, withFloat0: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, withFloat0_5: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, noneFloat0: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, noneFloat0_5: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }> };
860
+ export type ReverseFiltersQueryQuery = { all: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, withFloat0: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, withFloat0_5: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, noneFloat0: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, noneFloat0_5: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }>, noneFloat2: Array<{ __typename: 'AnotherObject', id: string, manyObjects: Array<{ __typename: 'SomeObject', float: number }> }> };
861
861
 
862
862
  export type DeleteAnotherObjectMutationMutationVariables = Exact<{
863
863
  id: Scalars['ID']['input'];
@@ -1061,6 +1061,8 @@ export namespace ReverseFiltersQuery {
1061
1061
  export type ___manyObjects = NonNullable<(NonNullable<NonNullable<(NonNullable<ReverseFiltersQueryQuery['noneFloat0']>)[number]>['manyObjects']>)[number]>;
1062
1062
  export type noneFloat0_5 = NonNullable<(NonNullable<ReverseFiltersQueryQuery['noneFloat0_5']>)[number]>;
1063
1063
  export type ____manyObjects = NonNullable<(NonNullable<NonNullable<(NonNullable<ReverseFiltersQueryQuery['noneFloat0_5']>)[number]>['manyObjects']>)[number]>;
1064
+ export type noneFloat2 = NonNullable<(NonNullable<ReverseFiltersQueryQuery['noneFloat2']>)[number]>;
1065
+ export type _____manyObjects = NonNullable<(NonNullable<NonNullable<(NonNullable<ReverseFiltersQueryQuery['noneFloat2']>)[number]>['manyObjects']>)[number]>;
1064
1066
  }
1065
1067
 
1066
1068
  export namespace DeleteAnotherObjectMutation {
@@ -35,7 +35,7 @@ export type AnotherObject = {
35
35
  'name': string | null;
36
36
  'myselfId': string | null;
37
37
  'deleted': boolean;
38
- 'deletedAt': DateTime | string | null;
38
+ 'deletedAt': DateTime | null;
39
39
  'deletedById': string | null;
40
40
  }
41
41
 
@@ -44,7 +44,7 @@ export type AnotherObjectInitializer = {
44
44
  'name'?: string | null;
45
45
  'myselfId'?: string | null;
46
46
  'deleted'?: boolean;
47
- 'deletedAt'?: DateTime | string | null;
47
+ 'deletedAt'?: DateTime | null;
48
48
  'deletedById'?: string | null;
49
49
  }
50
50
 
@@ -53,7 +53,7 @@ export type AnotherObjectMutator = {
53
53
  'name'?: string | null;
54
54
  'myselfId'?: string | null;
55
55
  'deleted'?: boolean;
56
- 'deletedAt'?: DateTime | string | null;
56
+ 'deletedAt'?: DateTime | null;
57
57
  'deletedById'?: string | null;
58
58
  }
59
59
 
@@ -62,7 +62,7 @@ export type AnotherObjectSeed = {
62
62
  'name'?: string | null;
63
63
  'myselfId'?: string | null;
64
64
  'deleted'?: boolean;
65
- 'deletedAt'?: DateTime | string | null;
65
+ 'deletedAt'?: DateTime | null;
66
66
  'deletedById'?: string | null;
67
67
  }
68
68
 
@@ -73,12 +73,12 @@ export type SomeObject = {
73
73
  'float': number;
74
74
  'list': SomeEnum[];
75
75
  'xyz': number;
76
- 'createdAt': DateTime | string;
76
+ 'createdAt': DateTime;
77
77
  'createdById': string;
78
- 'updatedAt': DateTime | string;
78
+ 'updatedAt': DateTime;
79
79
  'updatedById': string;
80
80
  'deleted': boolean;
81
- 'deletedAt': DateTime | string | null;
81
+ 'deletedAt': DateTime | null;
82
82
  'deletedById': string | null;
83
83
  }
84
84
 
@@ -89,12 +89,12 @@ export type SomeObjectInitializer = {
89
89
  'float': number;
90
90
  'list': SomeEnum[] | string;
91
91
  'xyz': number;
92
- 'createdAt': DateTime | string;
92
+ 'createdAt': DateTime;
93
93
  'createdById': string;
94
- 'updatedAt': DateTime | string;
94
+ 'updatedAt': DateTime;
95
95
  'updatedById': string;
96
96
  'deleted'?: boolean;
97
- 'deletedAt'?: DateTime | string | null;
97
+ 'deletedAt'?: DateTime | null;
98
98
  'deletedById'?: string | null;
99
99
  }
100
100
 
@@ -105,12 +105,12 @@ export type SomeObjectMutator = {
105
105
  'float'?: number;
106
106
  'list'?: SomeEnum[] | string;
107
107
  'xyz'?: number;
108
- 'createdAt'?: DateTime | string;
108
+ 'createdAt'?: DateTime;
109
109
  'createdById'?: string;
110
- 'updatedAt'?: DateTime | string;
110
+ 'updatedAt'?: DateTime;
111
111
  'updatedById'?: string;
112
112
  'deleted'?: boolean;
113
- 'deletedAt'?: DateTime | string | null;
113
+ 'deletedAt'?: DateTime | null;
114
114
  'deletedById'?: string | null;
115
115
  }
116
116
 
@@ -121,12 +121,12 @@ export type SomeObjectSeed = {
121
121
  'float': number;
122
122
  'list': string[] | string;
123
123
  'xyz': number;
124
- 'createdAt'?: DateTime | string;
124
+ 'createdAt'?: DateTime;
125
125
  'createdById'?: string;
126
- 'updatedAt'?: DateTime | string;
126
+ 'updatedAt'?: DateTime;
127
127
  'updatedById'?: string;
128
128
  'deleted'?: boolean;
129
- 'deletedAt'?: DateTime | string | null;
129
+ 'deletedAt'?: DateTime | null;
130
130
  'deletedById'?: string | null;
131
131
  }
132
132
 
@@ -135,12 +135,12 @@ export type Reaction = {
135
135
  'type': ReactionType;
136
136
  'parentId': string | null;
137
137
  'content': string | null;
138
- 'createdAt': DateTime | string;
138
+ 'createdAt': DateTime;
139
139
  'createdById': string;
140
- 'updatedAt': DateTime | string;
140
+ 'updatedAt': DateTime;
141
141
  'updatedById': string;
142
142
  'deleted': boolean;
143
- 'deletedAt': DateTime | string | null;
143
+ 'deletedAt': DateTime | null;
144
144
  'deletedById': string | null;
145
145
  }
146
146
 
@@ -149,12 +149,12 @@ export type ReactionInitializer = {
149
149
  'type': ReactionType;
150
150
  'parentId'?: string | null;
151
151
  'content'?: string | null;
152
- 'createdAt': DateTime | string;
152
+ 'createdAt': DateTime;
153
153
  'createdById': string;
154
- 'updatedAt': DateTime | string;
154
+ 'updatedAt': DateTime;
155
155
  'updatedById': string;
156
156
  'deleted'?: boolean;
157
- 'deletedAt'?: DateTime | string | null;
157
+ 'deletedAt'?: DateTime | null;
158
158
  'deletedById'?: string | null;
159
159
  }
160
160
 
@@ -163,12 +163,12 @@ export type ReactionMutator = {
163
163
  'type'?: ReactionType;
164
164
  'parentId'?: string | null;
165
165
  'content'?: string | null;
166
- 'createdAt'?: DateTime | string;
166
+ 'createdAt'?: DateTime;
167
167
  'createdById'?: string;
168
- 'updatedAt'?: DateTime | string;
168
+ 'updatedAt'?: DateTime;
169
169
  'updatedById'?: string;
170
170
  'deleted'?: boolean;
171
- 'deletedAt'?: DateTime | string | null;
171
+ 'deletedAt'?: DateTime | null;
172
172
  'deletedById'?: string | null;
173
173
  }
174
174
 
@@ -177,12 +177,12 @@ export type Review = {
177
177
  'type': ReactionType;
178
178
  'parentId': string | null;
179
179
  'content': string | null;
180
- 'createdAt': DateTime | string;
180
+ 'createdAt': DateTime;
181
181
  'createdById': string;
182
- 'updatedAt': DateTime | string;
182
+ 'updatedAt': DateTime;
183
183
  'updatedById': string;
184
184
  'deleted': boolean;
185
- 'deletedAt': DateTime | string | null;
185
+ 'deletedAt': DateTime | null;
186
186
  'deletedById': string | null;
187
187
  'rating': number | null;
188
188
  }
@@ -201,12 +201,12 @@ export type ReviewSeed = {
201
201
  'id': string;
202
202
  'parentId'?: string | null;
203
203
  'content'?: string | null;
204
- 'createdAt'?: DateTime | string;
204
+ 'createdAt'?: DateTime;
205
205
  'createdById'?: string;
206
- 'updatedAt'?: DateTime | string;
206
+ 'updatedAt'?: DateTime;
207
207
  'updatedById'?: string;
208
208
  'deleted'?: boolean;
209
- 'deletedAt'?: DateTime | string | null;
209
+ 'deletedAt'?: DateTime | null;
210
210
  'deletedById'?: string | null;
211
211
  'rating'?: number | null;
212
212
  }
@@ -216,12 +216,12 @@ export type Question = {
216
216
  'type': ReactionType;
217
217
  'parentId': string | null;
218
218
  'content': string | null;
219
- 'createdAt': DateTime | string;
219
+ 'createdAt': DateTime;
220
220
  'createdById': string;
221
- 'updatedAt': DateTime | string;
221
+ 'updatedAt': DateTime;
222
222
  'updatedById': string;
223
223
  'deleted': boolean;
224
- 'deletedAt': DateTime | string | null;
224
+ 'deletedAt': DateTime | null;
225
225
  'deletedById': string | null;
226
226
  }
227
227
 
@@ -237,12 +237,12 @@ export type QuestionSeed = {
237
237
  'id': string;
238
238
  'parentId'?: string | null;
239
239
  'content'?: string | null;
240
- 'createdAt'?: DateTime | string;
240
+ 'createdAt'?: DateTime;
241
241
  'createdById'?: string;
242
- 'updatedAt'?: DateTime | string;
242
+ 'updatedAt'?: DateTime;
243
243
  'updatedById'?: string;
244
244
  'deleted'?: boolean;
245
- 'deletedAt'?: DateTime | string | null;
245
+ 'deletedAt'?: DateTime | null;
246
246
  'deletedById'?: string | null;
247
247
  }
248
248
 
@@ -251,12 +251,12 @@ export type Answer = {
251
251
  'type': ReactionType;
252
252
  'parentId': string | null;
253
253
  'content': string | null;
254
- 'createdAt': DateTime | string;
254
+ 'createdAt': DateTime;
255
255
  'createdById': string;
256
- 'updatedAt': DateTime | string;
256
+ 'updatedAt': DateTime;
257
257
  'updatedById': string;
258
258
  'deleted': boolean;
259
- 'deletedAt': DateTime | string | null;
259
+ 'deletedAt': DateTime | null;
260
260
  'deletedById': string | null;
261
261
  }
262
262
 
@@ -272,12 +272,12 @@ export type AnswerSeed = {
272
272
  'id': string;
273
273
  'parentId'?: string | null;
274
274
  'content'?: string | null;
275
- 'createdAt'?: DateTime | string;
275
+ 'createdAt'?: DateTime;
276
276
  'createdById'?: string;
277
- 'updatedAt'?: DateTime | string;
277
+ 'updatedAt'?: DateTime;
278
278
  'updatedById'?: string;
279
279
  'deleted'?: boolean;
280
- 'deletedAt'?: DateTime | string | null;
280
+ 'deletedAt'?: DateTime | null;
281
281
  'deletedById'?: string | null;
282
282
  }
283
283
 
@@ -1,6 +1,5 @@
1
1
  import { Knex } from 'knex';
2
2
  import { pick } from 'lodash';
3
- import { DateTime } from 'luxon';
4
3
  import { getColumnName, isInTable, modelNeedsTable } from '../../../src';
5
4
  import { SeedData } from '../../generated/db';
6
5
  import { models } from '../models';
@@ -78,19 +77,18 @@ export const seed: SeedData = {
78
77
  ],
79
78
  };
80
79
 
81
- export const setupSeed = async (knex: Knex) => {
82
- const now = DateTime.now();
80
+ export const setupSeed = async (knex: Knex, now: string) => {
83
81
  for (const [table, entities] of Object.entries(seed)) {
84
82
  const model = models.getModel(table, 'entity');
85
83
  const mappedEntities = entities.map((entity, i) => ({
86
84
  ...entity,
87
85
  ...(model.parent && { type: model.name }),
88
86
  ...(model.creatable && {
89
- createdAt: now.plus({ second: i }),
87
+ createdAt: addSeconds(now, i),
90
88
  createdById: ADMIN_ID,
91
89
  }),
92
90
  ...(model.updatable && {
93
- updatedAt: now.plus({ second: i }),
91
+ updatedAt: addSeconds(now, i),
94
92
  updatedById: ADMIN_ID,
95
93
  }),
96
94
  }));
@@ -112,3 +110,9 @@ export const setupSeed = async (knex: Knex) => {
112
110
  }
113
111
  }
114
112
  };
113
+
114
+ const addSeconds = (dateString: string, seconds: number) => {
115
+ const date = new Date(dateString);
116
+ date.setSeconds(date.getSeconds() + seconds);
117
+ return date.toISOString();
118
+ };
@@ -2,7 +2,6 @@ import { TypedQueryDocumentNode } from 'graphql';
2
2
  import graphqlRequest, { RequestDocument, Variables } from 'graphql-request';
3
3
  import { RequestListener, createServer } from 'http';
4
4
  import { Knex } from 'knex';
5
- import { DateTime } from 'luxon';
6
5
  import { up } from '../../migrations/20230912185644_setup';
7
6
  import { execute } from '../../src';
8
7
  import { getKnex } from './database/knex';
@@ -44,7 +43,8 @@ export const withServer = async (
44
43
 
45
44
  try {
46
45
  await up(knex);
47
- await setupSeed(knex);
46
+ const now = '2020-01-01T00:00:00.000Z';
47
+ await setupSeed(knex, now);
48
48
 
49
49
  handler = async (req, res) => {
50
50
  const user = await knex('User').where({ id: ADMIN_ID }).first();
@@ -66,7 +66,7 @@ export const withServer = async (
66
66
  user,
67
67
  models,
68
68
  permissions,
69
- now: DateTime.fromISO('2020-01-01T00:00:00.000Z'),
69
+ now,
70
70
  body,
71
71
  });
72
72