@snowtop/ent 0.1.0-alpha127 → 0.1.0-alpha129

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.
@@ -37,6 +37,12 @@ export interface gqlObjectOptions {
37
37
  name?: string;
38
38
  description?: string;
39
39
  }
40
+ export interface gqlObjectWithInterfaceOptions extends gqlObjectOptions {
41
+ interfaces?: string[];
42
+ }
43
+ export interface gqlObjectWithUnionOptions extends gqlObjectOptions {
44
+ unionTypes: string[];
45
+ }
40
46
  type gqlMutationOptions = Omit<gqlFieldOptions, "nullable" | "type"> & {
41
47
  type?: gqlFieldOptionsBase["type"];
42
48
  };
@@ -77,6 +83,8 @@ export interface CustomObject {
77
83
  nodeName: string;
78
84
  className: string;
79
85
  description?: string;
86
+ interfaces?: string[];
87
+ unionTypes?: string[];
80
88
  }
81
89
  type NullableListOptions = "contents" | "contentsAndList";
82
90
  interface FieldImpl {
@@ -103,6 +111,7 @@ declare enum NullableResult {
103
111
  }
104
112
  export declare const knownAllowedNames: Map<string, string>;
105
113
  export declare const knownDisAllowedNames: Map<string, boolean>;
114
+ export declare const knownInterfaces: Map<string, boolean>;
106
115
  export declare const isCustomType: (type: Type) => type is CustomType;
107
116
  export declare const addCustomType: (type: CustomType, gqlCapture: typeof GQLCapture) => void;
108
117
  export declare class GQLCapture {
@@ -115,6 +124,8 @@ export declare class GQLCapture {
115
124
  private static customArgs;
116
125
  private static customInputObjects;
117
126
  private static customObjects;
127
+ private static customInterfaces;
128
+ private static customUnions;
118
129
  private static customTypes;
119
130
  static clear(): void;
120
131
  static getCustomFields(): Map<string, CustomField[]>;
@@ -123,6 +134,8 @@ export declare class GQLCapture {
123
134
  static getCustomArgs(): Map<string, CustomObject>;
124
135
  static getCustomInputObjects(): Map<string, CustomObject>;
125
136
  static getCustomObjects(): Map<string, CustomObject>;
137
+ static getCustomInterfaces(): Map<string, CustomObject>;
138
+ static getCustomUnions(): Map<string, CustomObject>;
126
139
  static getCustomTypes(): Map<string, CustomType>;
127
140
  private static getNullableArg;
128
141
  static getProcessedCustomFields(): ProcessCustomFieldMap;
@@ -135,7 +148,9 @@ export declare class GQLCapture {
135
148
  static gqlContextType(): gqlFieldArg;
136
149
  static gqlArgType(options?: gqlObjectOptions): any;
137
150
  static gqlInputObjectType(options?: gqlObjectOptions): any;
138
- static gqlObjectType(options?: gqlObjectOptions): any;
151
+ static gqlObjectType(options?: gqlObjectWithInterfaceOptions): any;
152
+ static gqlUnionType(options: gqlObjectWithUnionOptions): any;
153
+ static gqlInterfaceType(options?: gqlObjectOptions): any;
139
154
  private static customGQLObject;
140
155
  static gqlQuery(options: gqlQueryOptions): any;
141
156
  static gqlMutation(options: gqlMutationOptions): any;
@@ -146,6 +161,8 @@ export declare const gqlField: typeof GQLCapture.gqlField;
146
161
  export declare const gqlArgType: typeof GQLCapture.gqlArgType;
147
162
  export declare const gqlInputObjectType: typeof GQLCapture.gqlInputObjectType;
148
163
  export declare const gqlObjectType: typeof GQLCapture.gqlObjectType;
164
+ export declare const gqlInterfaceType: typeof GQLCapture.gqlInterfaceType;
165
+ export declare const gqlUnionType: typeof GQLCapture.gqlUnionType;
149
166
  export declare const gqlQuery: typeof GQLCapture.gqlQuery;
150
167
  export declare const gqlMutation: typeof GQLCapture.gqlMutation;
151
168
  export declare const gqlContextType: typeof GQLCapture.gqlContextType;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.gqlFileUpload = exports.gqlConnection = exports.gqlContextType = exports.gqlMutation = exports.gqlQuery = exports.gqlObjectType = exports.gqlInputObjectType = exports.gqlArgType = exports.gqlField = exports.GQLCapture = exports.addCustomType = exports.isCustomType = exports.knownDisAllowedNames = exports.knownAllowedNames = exports.CustomFieldType = void 0;
3
+ exports.gqlFileUpload = exports.gqlConnection = exports.gqlContextType = exports.gqlMutation = exports.gqlQuery = exports.gqlUnionType = exports.gqlInterfaceType = exports.gqlObjectType = exports.gqlInputObjectType = exports.gqlArgType = exports.gqlField = exports.GQLCapture = exports.addCustomType = exports.isCustomType = exports.knownInterfaces = exports.knownDisAllowedNames = exports.knownAllowedNames = exports.CustomFieldType = void 0;
4
4
  var CustomFieldType;
5
5
  (function (CustomFieldType) {
6
6
  CustomFieldType["Accessor"] = "ACCESSOR";
@@ -24,6 +24,7 @@ exports.knownAllowedNames = new Map([
24
24
  ["Float", "number"],
25
25
  ["ID", "ID"],
26
26
  ["JSON", "any"],
27
+ ["Node", "Ent"],
27
28
  ]);
28
29
  exports.knownDisAllowedNames = new Map([
29
30
  ["Function", true],
@@ -31,6 +32,11 @@ exports.knownDisAllowedNames = new Map([
31
32
  ["Array", true],
32
33
  ["Promise", true],
33
34
  ]);
35
+ exports.knownInterfaces = new Map([
36
+ ["Node", true],
37
+ ["Edge", true],
38
+ ["Connection", true],
39
+ ]);
34
40
  const isArray = (type) => {
35
41
  if (typeof type === "function") {
36
42
  return false;
@@ -143,6 +149,8 @@ class GQLCapture {
143
149
  this.customArgs.clear();
144
150
  this.customInputObjects.clear();
145
151
  this.customObjects.clear();
152
+ this.customInterfaces.clear();
153
+ this.customUnions.clear();
146
154
  this.customTypes.clear();
147
155
  }
148
156
  static getCustomFields() {
@@ -163,6 +171,12 @@ class GQLCapture {
163
171
  static getCustomObjects() {
164
172
  return this.customObjects;
165
173
  }
174
+ static getCustomInterfaces() {
175
+ return this.customInterfaces;
176
+ }
177
+ static getCustomUnions() {
178
+ return this.customUnions;
179
+ }
166
180
  static getCustomTypes() {
167
181
  return this.customTypes;
168
182
  }
@@ -348,6 +362,16 @@ class GQLCapture {
348
362
  return GQLCapture.customGQLObject(ctx, GQLCapture.customObjects, options);
349
363
  };
350
364
  }
365
+ static gqlUnionType(options) {
366
+ return function (target, ctx) {
367
+ return GQLCapture.customGQLObject(ctx, GQLCapture.customUnions, options);
368
+ };
369
+ }
370
+ static gqlInterfaceType(options) {
371
+ return function (target, ctx) {
372
+ return GQLCapture.customGQLObject(ctx, GQLCapture.customInterfaces, options);
373
+ };
374
+ }
351
375
  static customGQLObject(ctx, map, options) {
352
376
  if (!GQLCapture.isEnabled() || ctx.kind !== "class" || !ctx.name) {
353
377
  return;
@@ -358,6 +382,10 @@ class GQLCapture {
358
382
  className,
359
383
  nodeName,
360
384
  description: options?.description,
385
+ // @ts-ignore
386
+ interfaces: options?.interfaces,
387
+ // @ts-ignore
388
+ unionTypes: options?.unionTypes,
361
389
  });
362
390
  }
363
391
  // we want to specify args if any, name, response if any
@@ -384,13 +412,47 @@ class GQLCapture {
384
412
  }
385
413
  static resolve(objects) {
386
414
  let baseObjects = new Map();
387
- objects.map((object) => baseObjects.set(object, true));
388
- this.customObjects.forEach((_val, key) => baseObjects.set(key, true));
415
+ objects.forEach((object) => baseObjects.set(object, true));
416
+ this.customObjects.forEach((obj, key) => {
417
+ baseObjects.set(key, true);
418
+ obj.interfaces?.forEach((interfaceName) => {
419
+ const inter = this.customInterfaces.get(interfaceName);
420
+ if (inter) {
421
+ const fields = this.customFields.get(inter.nodeName);
422
+ if (fields) {
423
+ let objFields = this.customFields.get(obj.nodeName);
424
+ if (!objFields) {
425
+ objFields = [];
426
+ }
427
+ for (const field of fields) {
428
+ objFields.push({
429
+ ...field,
430
+ nodeName: obj.nodeName,
431
+ });
432
+ }
433
+ this.customFields.set(obj.nodeName, objFields);
434
+ }
435
+ }
436
+ else if (!exports.knownInterfaces.has(interfaceName)) {
437
+ throw new Error(`object ${key} references unknown interface ${interfaceName}`);
438
+ }
439
+ });
440
+ });
389
441
  let baseArgs = new Map();
390
442
  this.customArgs.forEach((_val, key) => baseArgs.set(key, true));
391
443
  this.customInputObjects.forEach((_val, key) => baseArgs.set(key, true));
392
444
  baseArgs.set("Context", true);
393
445
  this.customTypes.forEach((_val, key) => baseArgs.set(key, true));
446
+ this.customUnions.forEach((val, key) => {
447
+ if (this.customFields.has(key)) {
448
+ throw new Error(`union ${key} has custom fields which is not allowed`);
449
+ }
450
+ val.unionTypes?.forEach((typ) => {
451
+ if (!baseObjects.has(typ)) {
452
+ throw new Error(`union ${key} references ${typ} which isn't a graphql object`);
453
+ }
454
+ });
455
+ });
394
456
  // TODO this should be aware of knownCustomTypes
395
457
  const resolveFields = (fields) => {
396
458
  fields.forEach((field) => {
@@ -412,7 +474,9 @@ class GQLCapture {
412
474
  // but i don't think it applies
413
475
  field.results.forEach((result) => {
414
476
  if (result.needsResolving) {
415
- if (baseObjects.has(result.type)) {
477
+ if (baseObjects.has(result.type) ||
478
+ this.customUnions.has(result.type) ||
479
+ this.customInterfaces.has(result.type)) {
416
480
  result.needsResolving = false;
417
481
  }
418
482
  else {
@@ -435,6 +499,8 @@ GQLCapture.customMutations = [];
435
499
  GQLCapture.customArgs = new Map();
436
500
  GQLCapture.customInputObjects = new Map();
437
501
  GQLCapture.customObjects = new Map();
502
+ GQLCapture.customInterfaces = new Map();
503
+ GQLCapture.customUnions = new Map();
438
504
  GQLCapture.customTypes = new Map();
439
505
  exports.GQLCapture = GQLCapture;
440
506
  // why is this a static class lol?
@@ -443,6 +509,8 @@ exports.gqlField = GQLCapture.gqlField;
443
509
  exports.gqlArgType = GQLCapture.gqlArgType;
444
510
  exports.gqlInputObjectType = GQLCapture.gqlInputObjectType;
445
511
  exports.gqlObjectType = GQLCapture.gqlObjectType;
512
+ exports.gqlInterfaceType = GQLCapture.gqlInterfaceType;
513
+ exports.gqlUnionType = GQLCapture.gqlUnionType;
446
514
  exports.gqlQuery = GQLCapture.gqlQuery;
447
515
  exports.gqlMutation = GQLCapture.gqlMutation;
448
516
  exports.gqlContextType = GQLCapture.gqlContextType;
@@ -8,12 +8,16 @@ export declare function validateNoCustomFields(): void;
8
8
  export declare function validateCustomArgs(expected: CustomObject[]): void;
9
9
  export declare function validateCustomInputObjects(expected: CustomObject[]): void;
10
10
  export declare function validateCustomObjects(expected: CustomObject[]): void;
11
+ export declare function validateCustomInterfaces(expected: CustomObject[]): void;
12
+ export declare function validateCustomUnions(expected: CustomObject[]): void;
11
13
  export declare function validateNoCustomArgs(): void;
12
14
  export declare function validateNoCustomQueries(): void;
13
15
  export declare function validateNoCustomMutations(): void;
14
16
  export declare function validateNoCustomInputObjects(): void;
15
17
  export declare function validateNoCustomObjects(): void;
16
18
  export declare function validateNoCustomTypes(): void;
19
+ export declare function validateNoCustomInterfaces(): void;
20
+ export declare function validateNoCustomUnions(): void;
17
21
  export declare enum CustomObjectTypes {
18
22
  Field = 1,
19
23
  Arg = 2,
@@ -21,7 +25,9 @@ export declare enum CustomObjectTypes {
21
25
  InputObject = 8,
22
26
  Query = 16,
23
27
  Mutation = 32,
24
- CustomTypes = 64
28
+ CustomTypes = 64,
29
+ Interface = 128,
30
+ Union = 256
25
31
  }
26
32
  export declare function validateNoCustom(...exceptions: number[]): void;
27
33
  export declare function validateCustomTypes(expected: CustomType[]): void;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateCustomTypes = exports.validateNoCustom = exports.CustomObjectTypes = exports.validateNoCustomTypes = exports.validateNoCustomObjects = exports.validateNoCustomInputObjects = exports.validateNoCustomMutations = exports.validateNoCustomQueries = exports.validateNoCustomArgs = exports.validateCustomObjects = exports.validateCustomInputObjects = exports.validateCustomArgs = exports.validateNoCustomFields = exports.validateFields = exports.validateCustomQueries = exports.validateCustomMutations = exports.validateCustomFields = exports.validateOneCustomField = void 0;
3
+ exports.validateCustomTypes = exports.validateNoCustom = exports.CustomObjectTypes = exports.validateNoCustomUnions = exports.validateNoCustomInterfaces = exports.validateNoCustomTypes = exports.validateNoCustomObjects = exports.validateNoCustomInputObjects = exports.validateNoCustomMutations = exports.validateNoCustomQueries = exports.validateNoCustomArgs = exports.validateCustomUnions = exports.validateCustomInterfaces = exports.validateCustomObjects = exports.validateCustomInputObjects = exports.validateCustomArgs = exports.validateNoCustomFields = exports.validateFields = exports.validateCustomQueries = exports.validateCustomMutations = exports.validateCustomFields = exports.validateOneCustomField = void 0;
4
4
  const graphql_1 = require("./graphql");
5
5
  function validateOneCustomField(expected) {
6
6
  let customFields = graphql_1.GQLCapture.getCustomFields();
@@ -101,6 +101,14 @@ function validateCustomObjects(expected) {
101
101
  validateCustom(expected, graphql_1.GQLCapture.getCustomObjects());
102
102
  }
103
103
  exports.validateCustomObjects = validateCustomObjects;
104
+ function validateCustomInterfaces(expected) {
105
+ validateCustom(expected, graphql_1.GQLCapture.getCustomInterfaces());
106
+ }
107
+ exports.validateCustomInterfaces = validateCustomInterfaces;
108
+ function validateCustomUnions(expected) {
109
+ validateCustom(expected, graphql_1.GQLCapture.getCustomUnions());
110
+ }
111
+ exports.validateCustomUnions = validateCustomUnions;
104
112
  function validateNoCustomArgs() {
105
113
  expect(graphql_1.GQLCapture.getCustomArgs().size).toBe(0);
106
114
  }
@@ -125,6 +133,14 @@ function validateNoCustomTypes() {
125
133
  expect(graphql_1.GQLCapture.getCustomTypes().size).toBe(0);
126
134
  }
127
135
  exports.validateNoCustomTypes = validateNoCustomTypes;
136
+ function validateNoCustomInterfaces() {
137
+ expect(graphql_1.GQLCapture.getCustomInterfaces().size).toBe(0);
138
+ }
139
+ exports.validateNoCustomInterfaces = validateNoCustomInterfaces;
140
+ function validateNoCustomUnions() {
141
+ expect(graphql_1.GQLCapture.getCustomUnions().size).toBe(0);
142
+ }
143
+ exports.validateNoCustomUnions = validateNoCustomUnions;
128
144
  var CustomObjectTypes;
129
145
  (function (CustomObjectTypes) {
130
146
  CustomObjectTypes[CustomObjectTypes["Field"] = 1] = "Field";
@@ -134,6 +150,8 @@ var CustomObjectTypes;
134
150
  CustomObjectTypes[CustomObjectTypes["Query"] = 16] = "Query";
135
151
  CustomObjectTypes[CustomObjectTypes["Mutation"] = 32] = "Mutation";
136
152
  CustomObjectTypes[CustomObjectTypes["CustomTypes"] = 64] = "CustomTypes";
153
+ CustomObjectTypes[CustomObjectTypes["Interface"] = 128] = "Interface";
154
+ CustomObjectTypes[CustomObjectTypes["Union"] = 256] = "Union";
137
155
  })(CustomObjectTypes = exports.CustomObjectTypes || (exports.CustomObjectTypes = {}));
138
156
  // TODO what's a good name for this instead
139
157
  function validateNoCustom(...exceptions) {
@@ -151,6 +169,8 @@ function validateNoCustom(...exceptions) {
151
169
  validate(CustomObjectTypes.Mutation, validateNoCustomMutations);
152
170
  validate(CustomObjectTypes.InputObject, validateNoCustomInputObjects);
153
171
  validate(CustomObjectTypes.CustomTypes, validateNoCustomTypes);
172
+ validate(CustomObjectTypes.Interface, validateNoCustomInterfaces);
173
+ validate(CustomObjectTypes.Union, validateNoCustomUnions);
154
174
  }
155
175
  exports.validateNoCustom = validateNoCustom;
156
176
  function validateCustomTypes(expected) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.0-alpha127",
3
+ "version": "0.1.0-alpha129",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -22,7 +22,7 @@ async function processFields(src, patternName) {
22
22
  for (const name in m) {
23
23
  const field = m[name];
24
24
  //@ts-ignore type and other changed fields with different type in ProcessedField vs Field
25
- let f = { name, ...field };
25
+ let f = { ...field, name };
26
26
  f.hasDefaultValueOnCreate = field.defaultValueOnCreate != undefined;
27
27
  f.hasDefaultValueOnEdit = field.defaultValueOnEdit != undefined;
28
28
  f.hasFieldPrivacy = field.privacyPolicy !== undefined;
@@ -375,6 +375,8 @@ async function main() {
375
375
  let queries = gqlCapture.getProcessedCustomQueries();
376
376
  let mutations = gqlCapture.getProcessedCustomMutations();
377
377
  let objects = fromMap(gqlCapture.getCustomObjects());
378
+ let interfaces = fromMap(gqlCapture.getCustomInterfaces());
379
+ let unions = fromMap(gqlCapture.getCustomUnions());
378
380
  let customTypes = fromMap(gqlCapture.getCustomTypes());
379
381
  let classes = {};
380
382
  let allFiles = {};
@@ -437,6 +439,8 @@ async function main() {
437
439
  mutations,
438
440
  classes,
439
441
  objects,
442
+ interfaces,
443
+ unions,
440
444
  files: allFiles,
441
445
  customTypes,
442
446
  }));