@soda-gql/core 0.5.3 → 0.6.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.
package/dist/index.js CHANGED
@@ -10,147 +10,26 @@ var VarRef = class {
10
10
  return varRef.inner;
11
11
  }
12
12
  };
13
- const isVarRef = (value) => {
14
- return typeof value === "object" && value !== null && value instanceof VarRef;
15
- };
16
13
  /**
17
- * Recursively checks if a NestedValue contains any VarRef.
18
- * Used by getVarRefValue to determine if it's safe to return as ConstValue.
14
+ * Creates a VarRef from a variable name.
15
+ * Returns AnyVarRef - type safety is enforced at assignment sites.
19
16
  */
20
- const hasVarRefInside = (value) => {
21
- if (isVarRef(value)) return true;
22
- if (Array.isArray(value)) return value.some(hasVarRefInside);
23
- if (typeof value === "object" && value !== null) return Object.values(value).some(hasVarRefInside);
24
- return false;
25
- };
26
- const createVarRefFromVariable = (name) => {
17
+ function createVarRefFromVariable(name) {
27
18
  return new VarRef({
28
19
  type: "variable",
29
20
  name
30
21
  });
31
- };
32
- const createVarRefFromNestedValue = (value) => {
22
+ }
23
+ /**
24
+ * Creates a VarRef from a nested value.
25
+ * Returns AnyVarRef - type safety is enforced at assignment sites.
26
+ */
27
+ function createVarRefFromNestedValue(value) {
33
28
  return new VarRef({
34
29
  type: "nested-value",
35
30
  value
36
31
  });
37
- };
38
- const getVarRefInner = (varRef) => {
39
- return VarRef.getInner(varRef);
40
- };
41
- /**
42
- * Get the variable name from a VarRef.
43
- * Throws if the VarRef contains a nested-value instead of a variable reference.
44
- */
45
- const getVarRefName = (varRef) => {
46
- const inner = VarRef.getInner(varRef);
47
- if (inner.type !== "variable") throw new Error("Expected variable reference, got nested-value");
48
- return inner.name;
49
- };
50
- /**
51
- * Get the const value from a VarRef.
52
- * Throws if the VarRef contains a variable reference instead of a nested-value,
53
- * or if the nested-value contains any VarRef inside.
54
- */
55
- const getVarRefValue = (varRef) => {
56
- const inner = VarRef.getInner(varRef);
57
- if (inner.type !== "nested-value") throw new Error("Expected nested-value, got variable reference");
58
- if (hasVarRefInside(inner.value)) throw new Error("Cannot get const value: nested-value contains VarRef");
59
- return inner.value;
60
- };
61
- const SelectableProxyInnerRegistry = /* @__PURE__ */ new WeakMap();
62
- const getSelectableProxyInner = (proxy) => {
63
- const inner = SelectableProxyInnerRegistry.get(proxy);
64
- if (!inner) throw new Error(`Proxy inner not found`);
65
- return inner;
66
- };
67
- const createSelectableProxy = (current) => {
68
- const proxy = new Proxy(Object.create(null), { get(_, property) {
69
- if (typeof property === "symbol") throw new Error(`Prohibited property access: ${String(property)}`);
70
- const nextSegments = [...current.segments, property];
71
- if (current.varInner.type === "virtual") return createSelectableProxy({
72
- varInner: current.varInner,
73
- segments: nextSegments
74
- });
75
- if (current.varInner.type === "variable") return createSelectableProxy({
76
- varInner: {
77
- type: "virtual",
78
- varName: current.varInner.name,
79
- varSegments: nextSegments
80
- },
81
- segments: nextSegments
82
- });
83
- if (typeof current.varInner.value === "object" && current.varInner.value !== null) {
84
- const value = current.varInner.value[property];
85
- return createSelectableProxy({
86
- varInner: isVarRef(value) ? getVarRefInner(value) : {
87
- type: "nested-value",
88
- value
89
- },
90
- segments: nextSegments
91
- });
92
- }
93
- throw new Error(`Cannot access children of primitive value at path [${current.segments.join(".")}]`);
94
- } });
95
- SelectableProxyInnerRegistry.set(proxy, current);
96
- return proxy;
97
- };
98
- /**
99
- * Get the variable name from a VarRef at a specific path.
100
- *
101
- * @param varRef - The VarRef containing a nested-value
102
- * @param selector - Path builder function, e.g., p => p.user.age
103
- * @returns The variable name at the specified path
104
- * @throws If path doesn't lead to a VarRef with type "variable"
105
- *
106
- * @example
107
- * const ref = createVarRefFromNestedValue({
108
- * user: { age: someVariableRef }
109
- * });
110
- * getNameAt(ref, p => p.user.age); // returns the variable name
111
- */
112
- const getNameAt = (varRef, selector) => {
113
- const inner = getSelectableProxyInner(selector(createSelectableProxy({
114
- varInner: VarRef.getInner(varRef),
115
- segments: []
116
- })));
117
- if (inner.varInner.type === "virtual") throw new Error(`Value at path [${inner.segments.join(".")}] is inside a variable`);
118
- if (inner.varInner.type !== "variable") throw new Error(`Value at path [${inner.segments.join(".")}] is not a variable`);
119
- return inner.varInner.name;
120
- };
121
- /**
122
- * Get the const value from a nested-value VarRef at a specific path.
123
- *
124
- * @param varRef - The VarRef containing a nested-value
125
- * @param pathFn - Path builder function, e.g., p => p.user.name
126
- * @returns The const value at the specified path
127
- * @throws If path leads to a VarRef or if value contains VarRef inside
128
- *
129
- * @example
130
- * const ref = createVarRefFromNestedValue({
131
- * user: { name: "Alice", age: someVariableRef }
132
- * });
133
- * getValueAt(ref, p => p.user.name); // returns "Alice"
134
- */
135
- const getValueAt = (varRef, selector) => {
136
- const inner = getSelectableProxyInner(selector(createSelectableProxy({
137
- varInner: VarRef.getInner(varRef),
138
- segments: []
139
- })));
140
- if (inner.varInner.type === "virtual") throw new Error(`Value at path [${inner.segments.join(".")}] is inside a variable`);
141
- if (inner.varInner.type !== "nested-value") throw new Error(`Value at path [${inner.segments.join(".")}] is not a nested-value`);
142
- if (hasVarRefInside(inner.varInner.value)) throw new Error(`Value at path [${inner.segments.join(".")}] contains nested VarRef`);
143
- return inner.varInner.value;
144
- };
145
- const getVariablePath = (varRef, selector) => {
146
- const inner = getSelectableProxyInner(selector(createSelectableProxy({
147
- varInner: VarRef.getInner(varRef),
148
- segments: []
149
- })));
150
- if (inner.varInner.type === "virtual") return [`$${inner.varInner.varName}`, ...inner.segments.slice(inner.varInner.varSegments.length)];
151
- if (inner.varInner.type === "variable") return [`$${inner.varInner.name}`];
152
- throw new Error(`Value at path [${inner.segments.join(".")}] is not a variable or inside a variable`);
153
- };
32
+ }
154
33
 
155
34
  //#endregion
156
35
  //#region packages/core/src/composer/build-document.ts
@@ -750,15 +629,15 @@ const recordFragmentUsage = (record) => {
750
629
  //#endregion
751
630
  //#region packages/core/src/composer/input.ts
752
631
  const createVarAssignments = (definitions, providedValues) => {
753
- return mapValues(definitions, (_definition, key) => {
632
+ return mapValues(definitions, (_, key) => {
754
633
  const varName = key;
755
634
  if (!providedValues || providedValues[varName] === void 0) return createVarRefFromNestedValue(void 0);
756
635
  const provided = providedValues[varName];
757
- if (isVarRef(provided)) return provided;
636
+ if (provided instanceof VarRef) return provided;
758
637
  return createVarRefFromNestedValue(provided);
759
638
  });
760
639
  };
761
- const createVarRefs = (definitions) => mapValues(definitions, (_ref, name) => createVarRefFromVariable(name));
640
+ const createVarRefs = (definitions) => mapValues(definitions, (_, name) => createVarRefFromVariable(name));
762
641
 
763
642
  //#endregion
764
643
  //#region packages/core/src/composer/fragment.ts
@@ -858,6 +737,132 @@ const createOperationComposerFactory = (schema, adapter) => {
858
737
  };
859
738
  };
860
739
 
740
+ //#endregion
741
+ //#region packages/core/src/composer/var-ref-tools.ts
742
+ /**
743
+ * Recursively checks if a NestedValue contains any VarRef.
744
+ * Used by getVarRefValue to determine if it's safe to return as ConstValue.
745
+ */
746
+ const hasVarRefInside = (value) => {
747
+ if (value instanceof VarRef) return true;
748
+ if (Array.isArray(value)) return value.some(hasVarRefInside);
749
+ if (typeof value === "object" && value !== null) return Object.values(value).some(hasVarRefInside);
750
+ return false;
751
+ };
752
+ /**
753
+ * Get the variable name from a VarRef.
754
+ * Throws if the VarRef contains a nested-value instead of a variable reference.
755
+ */
756
+ const getVarRefName = (varRef) => {
757
+ const inner = VarRef.getInner(varRef);
758
+ if (inner.type !== "variable") throw new Error("Expected variable reference, got nested-value");
759
+ return inner.name;
760
+ };
761
+ /**
762
+ * Get the const value from a VarRef.
763
+ * Throws if the VarRef contains a variable reference instead of a nested-value,
764
+ * or if the nested-value contains any VarRef inside.
765
+ */
766
+ const getVarRefValue = (varRef) => {
767
+ const inner = VarRef.getInner(varRef);
768
+ if (inner.type !== "nested-value") throw new Error("Expected nested-value, got variable reference");
769
+ if (hasVarRefInside(inner.value)) throw new Error("Cannot get const value: nested-value contains VarRef");
770
+ return inner.value;
771
+ };
772
+ const SelectableProxyInnerRegistry = /* @__PURE__ */ new WeakMap();
773
+ const getSelectableProxyInner = (proxy) => {
774
+ const inner = SelectableProxyInnerRegistry.get(proxy);
775
+ if (!inner) throw new Error(`Proxy inner not found`);
776
+ return inner;
777
+ };
778
+ const createSelectableProxy = (current) => {
779
+ const proxy = new Proxy(Object.create(null), { get(_, property) {
780
+ if (typeof property === "symbol") throw new Error(`Prohibited property access: ${String(property)}`);
781
+ const nextSegments = [...current.segments, property];
782
+ if (current.varInner.type === "virtual") return createSelectableProxy({
783
+ varInner: current.varInner,
784
+ segments: nextSegments
785
+ });
786
+ if (current.varInner.type === "variable") return createSelectableProxy({
787
+ varInner: {
788
+ type: "virtual",
789
+ varName: current.varInner.name,
790
+ varSegments: nextSegments
791
+ },
792
+ segments: nextSegments
793
+ });
794
+ if (typeof current.varInner.value === "object" && current.varInner.value !== null) {
795
+ const value = current.varInner.value[property];
796
+ return createSelectableProxy({
797
+ varInner: value instanceof VarRef ? VarRef.getInner(value) : {
798
+ type: "nested-value",
799
+ value
800
+ },
801
+ segments: nextSegments
802
+ });
803
+ }
804
+ throw new Error(`Cannot access children of primitive value at path [${current.segments.join(".")}]`);
805
+ } });
806
+ SelectableProxyInnerRegistry.set(proxy, current);
807
+ return proxy;
808
+ };
809
+ /**
810
+ * Get the variable name from a VarRef at a specific path.
811
+ *
812
+ * @param varRef - The VarRef containing a nested-value
813
+ * @param selector - Path builder function, e.g., p => p.user.age
814
+ * @returns The variable name at the specified path
815
+ * @throws If path doesn't lead to a VarRef with type "variable"
816
+ *
817
+ * @example
818
+ * const ref = createVarRefFromNestedValue({
819
+ * user: { age: someVariableRef }
820
+ * });
821
+ * getNameAt(ref, p => p.user.age); // returns the variable name
822
+ */
823
+ const getNameAt = (varRef, selector) => {
824
+ const inner = getSelectableProxyInner(selector(createSelectableProxy({
825
+ varInner: VarRef.getInner(varRef),
826
+ segments: []
827
+ })));
828
+ if (inner.varInner.type === "virtual") throw new Error(`Value at path [${inner.segments.join(".")}] is inside a variable`);
829
+ if (inner.varInner.type !== "variable") throw new Error(`Value at path [${inner.segments.join(".")}] is not a variable`);
830
+ return inner.varInner.name;
831
+ };
832
+ /**
833
+ * Get the const value from a nested-value VarRef at a specific path.
834
+ *
835
+ * @param varRef - The VarRef containing a nested-value
836
+ * @param pathFn - Path builder function, e.g., p => p.user.name
837
+ * @returns The const value at the specified path
838
+ * @throws If path leads to a VarRef or if value contains VarRef inside
839
+ *
840
+ * @example
841
+ * const ref = createVarRefFromNestedValue({
842
+ * user: { name: "Alice", age: someVariableRef }
843
+ * });
844
+ * getValueAt(ref, p => p.user.name); // returns "Alice"
845
+ */
846
+ const getValueAt = (varRef, selector) => {
847
+ const inner = getSelectableProxyInner(selector(createSelectableProxy({
848
+ varInner: VarRef.getInner(varRef),
849
+ segments: []
850
+ })));
851
+ if (inner.varInner.type === "virtual") throw new Error(`Value at path [${inner.segments.join(".")}] is inside a variable`);
852
+ if (inner.varInner.type !== "nested-value") throw new Error(`Value at path [${inner.segments.join(".")}] is not a nested-value`);
853
+ if (hasVarRefInside(inner.varInner.value)) throw new Error(`Value at path [${inner.segments.join(".")}] contains nested VarRef`);
854
+ return inner.varInner.value;
855
+ };
856
+ const getVariablePath = (varRef, selector) => {
857
+ const inner = getSelectableProxyInner(selector(createSelectableProxy({
858
+ varInner: VarRef.getInner(varRef),
859
+ segments: []
860
+ })));
861
+ if (inner.varInner.type === "virtual") return [`$${inner.varInner.varName}`, ...inner.segments.slice(inner.varInner.varSegments.length)];
862
+ if (inner.varInner.type === "variable") return [`$${inner.varInner.name}`];
863
+ throw new Error(`Value at path [${inner.segments.join(".")}] is not a variable or inside a variable`);
864
+ };
865
+
861
866
  //#endregion
862
867
  //#region packages/core/src/composer/var-builder.ts
863
868
  /**
@@ -914,7 +919,6 @@ const createVarBuilder = (inputTypeMethods) => {
914
919
  };
915
920
  varBuilder.getName = getVarRefName;
916
921
  varBuilder.getValue = getVarRefValue;
917
- varBuilder.getInner = getVarRefInner;
918
922
  varBuilder.getNameAt = getNameAt;
919
923
  varBuilder.getValueAt = getValueAt;
920
924
  varBuilder.getVariablePath = getVariablePath;
@@ -950,5 +954,5 @@ const createGqlElementComposer = (schema, options) => {
950
954
  };
951
955
 
952
956
  //#endregion
953
- export { Fragment, GqlElement, Operation, appendToPath, buildArgumentValue, buildConstValueNode, buildDocument, buildOperationTypeNode, buildWithTypeModifier, createColocateHelper, createDefaultAdapter, createFieldFactories, createGqlElementComposer, createGqlFragmentComposers, createOperationComposerFactory, createVarAssignments, createVarBuilder, createVarMethod, createVarMethodFactory, createVarRefs, defaultMetadataAdapter, define, defineOperationRoots, defineScalar, getCurrentFieldPath, getVarRefInner, getVarRefName, getVarRefValue, isListType, recordFragmentUsage, unsafeInputType, unsafeOutputType, withFieldPath, withFragmentUsageCollection };
957
+ export { Fragment, GqlElement, Operation, appendToPath, buildArgumentValue, buildConstValueNode, buildDocument, buildOperationTypeNode, buildWithTypeModifier, createColocateHelper, createDefaultAdapter, createFieldFactories, createGqlElementComposer, createGqlFragmentComposers, createOperationComposerFactory, createVarAssignments, createVarBuilder, createVarMethod, createVarMethodFactory, createVarRefs, defaultMetadataAdapter, define, defineOperationRoots, defineScalar, getCurrentFieldPath, isListType, recordFragmentUsage, unsafeInputType, unsafeOutputType, withFieldPath, withFragmentUsageCollection };
954
958
  //# sourceMappingURL=index.js.map