@snowtop/ent 0.1.0-alpha3 → 0.1.0-alpha6

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.
@@ -8,3 +8,4 @@ export { GraphQLConnectionInterface } from "./builtins/connection";
8
8
  export { GraphQLEdgeInterface } from "./builtins/edge";
9
9
  export { NodeResolver, EntNodeResolver, registerResolver, clearResolvers, resolveID, nodeIDEncoder, mustDecodeIDFromGQLID, mustDecodeNullableIDFromGQLID, encodeGQLID, } from "./node_resolver";
10
10
  export { convertFromGQLEnum, convertToGQLEnum } from "./enums";
11
+ export { transformUnionTypes } from "./mutations/union";
package/graphql/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertToGQLEnum = exports.convertFromGQLEnum = exports.encodeGQLID = exports.mustDecodeNullableIDFromGQLID = exports.mustDecodeIDFromGQLID = exports.nodeIDEncoder = exports.resolveID = exports.clearResolvers = exports.registerResolver = exports.EntNodeResolver = exports.GraphQLEdgeInterface = exports.GraphQLConnectionInterface = exports.GraphQLNodeInterface = exports.GraphQLConnectionType = exports.GraphQLEdgeType = exports.GraphQLEdgeConnection = exports.GraphQLPageInfo = exports.GraphQLTime = exports.gqlFileUpload = exports.GQLCapture = exports.gqlConnection = exports.gqlContextType = exports.gqlMutation = exports.gqlQuery = exports.gqlObjectType = exports.gqlInputObjectType = exports.gqlArgType = exports.gqlArg = exports.gqlField = void 0;
3
+ exports.transformUnionTypes = exports.convertToGQLEnum = exports.convertFromGQLEnum = exports.encodeGQLID = exports.mustDecodeNullableIDFromGQLID = exports.mustDecodeIDFromGQLID = exports.nodeIDEncoder = exports.resolveID = exports.clearResolvers = exports.registerResolver = exports.EntNodeResolver = exports.GraphQLEdgeInterface = exports.GraphQLConnectionInterface = exports.GraphQLNodeInterface = exports.GraphQLConnectionType = exports.GraphQLEdgeType = exports.GraphQLEdgeConnection = exports.GraphQLPageInfo = exports.GraphQLTime = exports.gqlFileUpload = exports.GQLCapture = exports.gqlConnection = exports.gqlContextType = exports.gqlMutation = exports.gqlQuery = exports.gqlObjectType = exports.gqlInputObjectType = exports.gqlArgType = exports.gqlArg = exports.gqlField = void 0;
4
4
  var graphql_1 = require("./graphql");
5
5
  Object.defineProperty(exports, "gqlField", { enumerable: true, get: function () { return graphql_1.gqlField; } });
6
6
  Object.defineProperty(exports, "gqlArg", { enumerable: true, get: function () { return graphql_1.gqlArg; } });
@@ -40,3 +40,5 @@ Object.defineProperty(exports, "encodeGQLID", { enumerable: true, get: function
40
40
  var enums_1 = require("./enums");
41
41
  Object.defineProperty(exports, "convertFromGQLEnum", { enumerable: true, get: function () { return enums_1.convertFromGQLEnum; } });
42
42
  Object.defineProperty(exports, "convertToGQLEnum", { enumerable: true, get: function () { return enums_1.convertToGQLEnum; } });
43
+ var union_1 = require("./mutations/union");
44
+ Object.defineProperty(exports, "transformUnionTypes", { enumerable: true, get: function () { return union_1.transformUnionTypes; } });
@@ -0,0 +1,2 @@
1
+ import { Data } from "../../core/base";
2
+ export declare function transformUnionTypes<T extends Data>(input: T, pathsList: string[][]): T;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transformUnionTypes = void 0;
4
+ // this transforms an input for union types from graphql format to TS format
5
+ // in graphql, we represent it as UnionType = {foo: FooType, bar: BarType, baz: BazType}
6
+ // in TS, we repseent it as UnionType = FooType | BarType | BazType
7
+ // this takes an input, paths to unions and transforms them as needed
8
+ // only works on fields that are defined. depends on graphql to handle nullable/missing fields
9
+ function transformUnionTypes(input, pathsList) {
10
+ for (const paths of pathsList) {
11
+ const lastPath = paths[paths.length - 1];
12
+ let last = input;
13
+ for (const path of paths) {
14
+ let curr = last[path];
15
+ if (curr === undefined) {
16
+ break;
17
+ }
18
+ if (path === lastPath) {
19
+ let count = 0;
20
+ let lastKey = undefined;
21
+ for (const k in curr) {
22
+ count++;
23
+ lastKey = k;
24
+ }
25
+ if (count != 1) {
26
+ throw new Error(`can only only pass one key of union. passed ${count}`);
27
+ }
28
+ last[path] = curr[lastKey];
29
+ }
30
+ last = curr;
31
+ }
32
+ }
33
+ return input;
34
+ }
35
+ exports.transformUnionTypes = transformUnionTypes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.0-alpha3",
3
+ "version": "0.1.0-alpha6",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -15,7 +15,8 @@ export declare class UnionField extends BaseField implements FieldOptions {
15
15
  type: Type;
16
16
  m: Map<Object, string>;
17
17
  constructor(options: UnionOptions);
18
- format(obj: any, nested?: boolean): string | Object;
18
+ format(obj: any): string | Object;
19
+ private validField;
19
20
  valid(obj: any): Promise<boolean>;
20
21
  }
21
22
  export declare function UnionType(options: UnionOptions): UnionField & UnionOptions;
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UnionListType = exports.UnionType = exports.UnionField = void 0;
4
4
  const schema_1 = require("./schema");
5
5
  const field_1 = require("./field");
6
+ // used to know which key in the union is valid.
7
+ // maybe there's a better way of doing this eventually
8
+ const KEY = "___valid___key___";
6
9
  class UnionField extends field_1.BaseField {
7
10
  constructor(options) {
8
11
  super();
@@ -19,21 +22,26 @@ class UnionField extends field_1.BaseField {
19
22
  this.type.dbType = schema_1.DBType.JSON;
20
23
  }
21
24
  }
22
- format(obj, nested) {
25
+ format(obj) {
23
26
  if (!(obj instanceof Object)) {
24
27
  throw new Error("valid was not called");
25
28
  }
26
- for (const k in this.options.fields) {
27
- const field = this.options.fields[k];
28
- const fmt = field.format(obj, nested);
29
- if (fmt !== "{}") {
30
- return fmt;
31
- }
29
+ const k = obj[KEY];
30
+ if (k === undefined) {
31
+ throw new Error(`need to call valid first`);
32
32
  }
33
- // TODO need better logic here
34
- // maybe add something ignored to the objec that indicates which key?
35
- // or store in map
36
- throw new Error(`couldn't format union`);
33
+ // now delete it since we don't need it anymore
34
+ delete obj[KEY];
35
+ const field = this.options.fields[k];
36
+ // always nested for now so pass through
37
+ return field.format(obj, true);
38
+ }
39
+ async validField(k, f, obj) {
40
+ const valid = await f.valid(obj);
41
+ return {
42
+ valid,
43
+ key: k,
44
+ };
37
45
  }
38
46
  async valid(obj) {
39
47
  if (!(obj instanceof Object)) {
@@ -42,11 +50,21 @@ class UnionField extends field_1.BaseField {
42
50
  let promises = [];
43
51
  for (const k in this.options.fields) {
44
52
  const field = this.options.fields[k];
45
- promises.push(field.valid(obj));
53
+ promises.push(this.validField(k, field, obj));
46
54
  }
55
+ let lastKey;
56
+ let validCt = 0;
47
57
  const ret = await Promise.all(promises);
48
- // only 1 should be valid
49
- return ret.filter((v) => v).length === 1;
58
+ for (const v of ret) {
59
+ if (v.valid) {
60
+ validCt++;
61
+ lastKey = v.key;
62
+ }
63
+ }
64
+ if (lastKey !== undefined) {
65
+ obj[KEY] = lastKey;
66
+ }
67
+ return validCt == 1;
50
68
  }
51
69
  }
52
70
  exports.UnionField = UnionField;