@gqloom/core 0.4.0 → 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/README.md +9 -9
- package/dist/index.cjs +133 -60
- package/dist/index.d.cts +83 -62
- package/dist/index.d.ts +83 -62
- package/dist/index.js +130 -59
- package/package.json +6 -2
package/dist/index.js
CHANGED
|
@@ -156,7 +156,9 @@ function collectName(name, schema) {
|
|
|
156
156
|
}
|
|
157
157
|
|
|
158
158
|
// src/resolver/silk.ts
|
|
159
|
-
function silk(type, validate = (value) => ({
|
|
159
|
+
function silk(type, validate = (value) => ({
|
|
160
|
+
value: value ?? void 0
|
|
161
|
+
})) {
|
|
160
162
|
return {
|
|
161
163
|
[GET_GRAPHQL_TYPE]: typeof type === "function" ? type : () => type,
|
|
162
164
|
"~standard": {
|
|
@@ -261,13 +263,17 @@ function getFieldOptions({
|
|
|
261
263
|
}
|
|
262
264
|
|
|
263
265
|
// src/utils/middleware.ts
|
|
264
|
-
function applyMiddlewares(middlewares, resolveFunction,
|
|
266
|
+
function applyMiddlewares(middlewares, resolveFunction, options) {
|
|
265
267
|
const next = (index) => {
|
|
266
268
|
if (index >= middlewares.length) {
|
|
267
269
|
return resolveFunction();
|
|
268
270
|
}
|
|
269
271
|
const middleware = middlewares[index];
|
|
270
|
-
|
|
272
|
+
const callableOptions = Object.assign(() => next(index + 1), {
|
|
273
|
+
...options,
|
|
274
|
+
next: () => next(index + 1)
|
|
275
|
+
});
|
|
276
|
+
return middleware(callableOptions);
|
|
271
277
|
};
|
|
272
278
|
return next(0);
|
|
273
279
|
}
|
|
@@ -420,6 +426,13 @@ function deepMerge(...objects) {
|
|
|
420
426
|
return result;
|
|
421
427
|
}
|
|
422
428
|
|
|
429
|
+
// src/utils/string.ts
|
|
430
|
+
function pascalCase(str) {
|
|
431
|
+
return str.split(/[\s-_]+/).map(
|
|
432
|
+
(word, index) => index === 0 ? word.charAt(0).toUpperCase() + word.slice(1) : word.charAt(0).toUpperCase() + word.slice(1)
|
|
433
|
+
).join("");
|
|
434
|
+
}
|
|
435
|
+
|
|
423
436
|
// src/utils/error.ts
|
|
424
437
|
function markErrorLocation(error, ...locations) {
|
|
425
438
|
if (error instanceof Error) {
|
|
@@ -740,31 +753,38 @@ import {
|
|
|
740
753
|
isObjectType as isObjectType2,
|
|
741
754
|
isUnionType as isUnionType2
|
|
742
755
|
} from "graphql";
|
|
743
|
-
function inputToArgs(input) {
|
|
756
|
+
function inputToArgs(input, options) {
|
|
744
757
|
if (input === void 0) return void 0;
|
|
745
758
|
if (isSilk(input)) {
|
|
746
759
|
let inputType = getGraphQLType(input);
|
|
747
760
|
if (isNonNullType(inputType)) inputType = inputType.ofType;
|
|
748
761
|
if (isObjectType2(inputType)) {
|
|
749
|
-
return mapValue(
|
|
750
|
-
|
|
751
|
-
(
|
|
752
|
-
|
|
762
|
+
return mapValue(inputType.toConfig().fields, (it, key) => {
|
|
763
|
+
let fieldName;
|
|
764
|
+
if (options?.fieldName) {
|
|
765
|
+
fieldName = `${pascalCase(options.fieldName)}${pascalCase(key)}`;
|
|
766
|
+
}
|
|
767
|
+
return toInputFieldConfig(it, { fieldName });
|
|
768
|
+
});
|
|
753
769
|
}
|
|
754
770
|
throw new Error(`Cannot convert ${inputType.toString()} to input type`);
|
|
755
771
|
}
|
|
756
772
|
const args = {};
|
|
757
773
|
Object.entries(input).forEach(([name, field2]) => {
|
|
758
774
|
tryIn(() => {
|
|
775
|
+
let fieldName;
|
|
776
|
+
if (options?.fieldName) {
|
|
777
|
+
fieldName = `${pascalCase(options.fieldName)}${pascalCase(name)}`;
|
|
778
|
+
}
|
|
759
779
|
args[name] = {
|
|
760
780
|
...field2,
|
|
761
|
-
type: ensureInputType(field2)
|
|
781
|
+
type: ensureInputType(field2, { fieldName })
|
|
762
782
|
};
|
|
763
783
|
}, name);
|
|
764
784
|
});
|
|
765
785
|
return args;
|
|
766
786
|
}
|
|
767
|
-
function ensureInputType(silkOrType) {
|
|
787
|
+
function ensureInputType(silkOrType, options) {
|
|
768
788
|
const gqlType = (() => {
|
|
769
789
|
if (isSilk(silkOrType)) {
|
|
770
790
|
return getGraphQLType(silkOrType);
|
|
@@ -774,48 +794,56 @@ function ensureInputType(silkOrType) {
|
|
|
774
794
|
if (isUnionType2(gqlType))
|
|
775
795
|
throw new Error(`Cannot convert union type ${gqlType.name} to input type`);
|
|
776
796
|
if (isNonNullType(gqlType)) {
|
|
777
|
-
return new GraphQLNonNull2(ensureInputType(gqlType.ofType));
|
|
797
|
+
return new GraphQLNonNull2(ensureInputType(gqlType.ofType, options));
|
|
778
798
|
}
|
|
779
799
|
if (isListType(gqlType)) {
|
|
780
|
-
return new GraphQLList2(ensureInputType(gqlType.ofType));
|
|
800
|
+
return new GraphQLList2(ensureInputType(gqlType.ofType, options));
|
|
781
801
|
}
|
|
782
802
|
if (isObjectType2(gqlType) || isInterfaceType(gqlType))
|
|
783
|
-
return ensureInputObjectType(gqlType);
|
|
803
|
+
return ensureInputObjectType(gqlType, options);
|
|
784
804
|
return gqlType;
|
|
785
805
|
}
|
|
786
|
-
function ensureInputObjectType(object) {
|
|
806
|
+
function ensureInputObjectType(object, options) {
|
|
787
807
|
if (isInputObjectType(object)) return object;
|
|
788
808
|
const existing = weaverContext.inputMap?.get(object);
|
|
789
809
|
if (existing != null) return existing;
|
|
790
|
-
const {
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
810
|
+
const { astNode, extensionASTNodes, fields, ...config } = object.toConfig();
|
|
811
|
+
let name = object.name;
|
|
812
|
+
if (name === LoomObjectType.AUTO_ALIASING) {
|
|
813
|
+
name = `${pascalCase(options?.fieldName ?? "")}Input`;
|
|
814
|
+
}
|
|
815
|
+
const getInputObjectName = weaverContext.getConfig("gqloom.core.schema")?.getInputObjectName ?? ((n) => n);
|
|
816
|
+
name = getInputObjectName(name);
|
|
797
817
|
const input = new GraphQLInputObjectType({
|
|
798
818
|
...config,
|
|
799
|
-
name
|
|
819
|
+
name,
|
|
800
820
|
fields: provideWeaverContext.inherit(
|
|
801
|
-
() => mapValue(
|
|
821
|
+
() => mapValue(
|
|
822
|
+
fields,
|
|
823
|
+
(it, key) => toInputFieldConfig(it, {
|
|
824
|
+
fieldName: inputFieldName(name) + pascalCase(key)
|
|
825
|
+
})
|
|
826
|
+
)
|
|
802
827
|
)
|
|
803
828
|
});
|
|
804
829
|
weaverContext.inputMap?.set(object, input);
|
|
805
830
|
return input;
|
|
806
831
|
}
|
|
807
|
-
function toInputFieldConfig({
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
832
|
+
function toInputFieldConfig({ astNode, resolve, ...config }, options) {
|
|
833
|
+
return { ...config, type: ensureInputType(config.type, options) };
|
|
834
|
+
}
|
|
835
|
+
function inputFieldName(name) {
|
|
836
|
+
while (name.endsWith("Input")) {
|
|
837
|
+
name = name.slice(0, -"Input".length);
|
|
838
|
+
}
|
|
839
|
+
return name;
|
|
813
840
|
}
|
|
814
841
|
|
|
815
842
|
// src/schema/object.ts
|
|
816
|
-
var LoomObjectType = class extends GraphQLObjectType {
|
|
843
|
+
var LoomObjectType = class _LoomObjectType extends GraphQLObjectType {
|
|
817
844
|
extraFields = /* @__PURE__ */ new Map();
|
|
818
845
|
hiddenFields = /* @__PURE__ */ new Set();
|
|
846
|
+
static AUTO_ALIASING = "__gqloom_auto_aliasing";
|
|
819
847
|
weaverContext;
|
|
820
848
|
resolverOptions;
|
|
821
849
|
constructor(objectOrGetter, options = {}) {
|
|
@@ -832,6 +860,28 @@ var LoomObjectType = class extends GraphQLObjectType {
|
|
|
832
860
|
super(config);
|
|
833
861
|
this.resolverOptions = options.resolverOptions;
|
|
834
862
|
this.weaverContext = options.weaverContext ?? initWeaverContext();
|
|
863
|
+
if (this.name !== _LoomObjectType.AUTO_ALIASING) {
|
|
864
|
+
this.hasExplicitName = true;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
hasExplicitName;
|
|
868
|
+
_aliases = [];
|
|
869
|
+
get aliases() {
|
|
870
|
+
return this._aliases;
|
|
871
|
+
}
|
|
872
|
+
addAlias(name) {
|
|
873
|
+
if (this.hasExplicitName) return;
|
|
874
|
+
this._aliases.push(name);
|
|
875
|
+
this.renameByAliases();
|
|
876
|
+
}
|
|
877
|
+
renameByAliases() {
|
|
878
|
+
let name;
|
|
879
|
+
for (const alias of this.aliases) {
|
|
880
|
+
if (name === void 0 || alias.length < name.length) {
|
|
881
|
+
name = alias;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
if (name) this.name = name;
|
|
835
885
|
}
|
|
836
886
|
hideField(name) {
|
|
837
887
|
this.hiddenFields.add(name);
|
|
@@ -849,8 +899,8 @@ var LoomObjectType = class extends GraphQLObjectType {
|
|
|
849
899
|
extraFieldMap;
|
|
850
900
|
getFields() {
|
|
851
901
|
const fieldsBySuper = super.getFields();
|
|
852
|
-
Object.
|
|
853
|
-
(field2) => field2.type = this.getCacheType(field2.type)
|
|
902
|
+
Object.entries(fieldsBySuper).forEach(
|
|
903
|
+
([fieldName, field2]) => field2.type = this.getCacheType(field2.type, fieldName)
|
|
854
904
|
);
|
|
855
905
|
const extraFields = provideWeaverContext(
|
|
856
906
|
() => defineFieldMap(this.mapToFieldConfig(this.extraFields)),
|
|
@@ -871,17 +921,22 @@ var LoomObjectType = class extends GraphQLObjectType {
|
|
|
871
921
|
mapToFieldConfig(map) {
|
|
872
922
|
const record = {};
|
|
873
923
|
for (const [name, field2] of map.entries()) {
|
|
874
|
-
record[name] = this.toFieldConfig(field2);
|
|
924
|
+
record[name] = this.toFieldConfig(field2, name);
|
|
875
925
|
}
|
|
876
926
|
return record;
|
|
877
927
|
}
|
|
878
|
-
toFieldConfig(field2) {
|
|
928
|
+
toFieldConfig(field2, fieldName) {
|
|
879
929
|
try {
|
|
880
|
-
const outputType = this.getCacheType(
|
|
930
|
+
const outputType = this.getCacheType(
|
|
931
|
+
getGraphQLType(field2.output),
|
|
932
|
+
fieldName
|
|
933
|
+
);
|
|
881
934
|
return {
|
|
882
935
|
...extract(field2),
|
|
883
936
|
type: outputType,
|
|
884
|
-
args: inputToArgs(field2.input
|
|
937
|
+
args: inputToArgs(field2.input, {
|
|
938
|
+
fieldName: fieldName ? parentName(this.name) + fieldName : void 0
|
|
939
|
+
}),
|
|
885
940
|
...this.provideForResolve(field2),
|
|
886
941
|
...this.provideForSubscribe(field2)
|
|
887
942
|
};
|
|
@@ -914,8 +969,8 @@ var LoomObjectType = class extends GraphQLObjectType {
|
|
|
914
969
|
)
|
|
915
970
|
};
|
|
916
971
|
}
|
|
917
|
-
getCacheType(gqlType) {
|
|
918
|
-
return getCacheType(gqlType, this.options);
|
|
972
|
+
getCacheType(gqlType, fieldName) {
|
|
973
|
+
return getCacheType(gqlType, { ...this.options, fieldName, parent: this });
|
|
919
974
|
}
|
|
920
975
|
get options() {
|
|
921
976
|
const { resolverOptions, weaverContext: weaverContext2 } = this;
|
|
@@ -961,6 +1016,11 @@ function defineArguments(args) {
|
|
|
961
1016
|
astNode: argConfig.astNode
|
|
962
1017
|
}));
|
|
963
1018
|
}
|
|
1019
|
+
var OPERATION_OBJECT_NAMES = /* @__PURE__ */ new Set([
|
|
1020
|
+
"Query",
|
|
1021
|
+
"Mutation",
|
|
1022
|
+
"Subscription"
|
|
1023
|
+
]);
|
|
964
1024
|
function getCacheType(gqlType, options = {}) {
|
|
965
1025
|
const context = options.weaverContext ?? weaverContext;
|
|
966
1026
|
if (gqlType instanceof LoomObjectType) return gqlType;
|
|
@@ -969,6 +1029,11 @@ function getCacheType(gqlType, options = {}) {
|
|
|
969
1029
|
if (gqlObject != null) return gqlObject;
|
|
970
1030
|
const loomObject = new LoomObjectType(gqlType, options);
|
|
971
1031
|
context.loomObjectMap?.set(gqlType, loomObject);
|
|
1032
|
+
if (options.fieldName && options.parent) {
|
|
1033
|
+
loomObject.addAlias(
|
|
1034
|
+
parentName(options.parent.name) + pascalCase(options.fieldName)
|
|
1035
|
+
);
|
|
1036
|
+
}
|
|
972
1037
|
return loomObject;
|
|
973
1038
|
} else if (isListType2(gqlType)) {
|
|
974
1039
|
return new GraphQLList3(getCacheType(gqlType.ofType, options));
|
|
@@ -989,8 +1054,12 @@ function getCacheType(gqlType, options = {}) {
|
|
|
989
1054
|
}
|
|
990
1055
|
return gqlType;
|
|
991
1056
|
}
|
|
1057
|
+
function parentName(name) {
|
|
1058
|
+
if (OPERATION_OBJECT_NAMES.has(name)) name = "";
|
|
1059
|
+
return name;
|
|
1060
|
+
}
|
|
992
1061
|
|
|
993
|
-
// src/schema/schema-
|
|
1062
|
+
// src/schema/schema-weaver.ts
|
|
994
1063
|
function isSchemaVendorWeaver(some) {
|
|
995
1064
|
if (typeof some !== "object" && typeof some !== "function") return false;
|
|
996
1065
|
if (!("getGraphQLType" in some) || typeof some.getGraphQLType !== "function")
|
|
@@ -999,7 +1068,7 @@ function isSchemaVendorWeaver(some) {
|
|
|
999
1068
|
return true;
|
|
1000
1069
|
}
|
|
1001
1070
|
|
|
1002
|
-
// src/schema/schema-
|
|
1071
|
+
// src/schema/schema-loom.ts
|
|
1003
1072
|
import {
|
|
1004
1073
|
GraphQLSchema,
|
|
1005
1074
|
isEnumType as isEnumType2,
|
|
@@ -1007,7 +1076,7 @@ import {
|
|
|
1007
1076
|
isObjectType as isObjectType4,
|
|
1008
1077
|
isUnionType as isUnionType4
|
|
1009
1078
|
} from "graphql";
|
|
1010
|
-
var
|
|
1079
|
+
var GraphQLSchemaLoom = class _GraphQLSchemaLoom {
|
|
1011
1080
|
query;
|
|
1012
1081
|
mutation;
|
|
1013
1082
|
subscription;
|
|
@@ -1029,7 +1098,7 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
1029
1098
|
if (query2 != null) this.query = query2;
|
|
1030
1099
|
if (mutation2 != null) this.mutation = mutation2;
|
|
1031
1100
|
if (subscription2 != null) this.subscription = subscription2;
|
|
1032
|
-
|
|
1101
|
+
this.types = new Set(types ?? []);
|
|
1033
1102
|
this.context = context ?? initWeaverContext();
|
|
1034
1103
|
}
|
|
1035
1104
|
use(...middlewares) {
|
|
@@ -1063,8 +1132,7 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
1063
1132
|
`${gqlType2?.name ?? gqlType2.toString()} is not a named type`
|
|
1064
1133
|
);
|
|
1065
1134
|
}, this.context);
|
|
1066
|
-
this.types
|
|
1067
|
-
this.types.push(gqlType);
|
|
1135
|
+
this.types.add(gqlType);
|
|
1068
1136
|
return this;
|
|
1069
1137
|
}
|
|
1070
1138
|
setConfig(config) {
|
|
@@ -1090,16 +1158,17 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
1090
1158
|
if (parent == null) return void 0;
|
|
1091
1159
|
let gqlType = getGraphQLType(parent);
|
|
1092
1160
|
if (isNonNullType3(gqlType)) gqlType = gqlType.ofType;
|
|
1093
|
-
if (isObjectType4(gqlType)) {
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
this.context.loomObjectMap.set(gqlType, extraObject);
|
|
1098
|
-
return extraObject;
|
|
1161
|
+
if (!isObjectType4(gqlType)) {
|
|
1162
|
+
throw new Error(
|
|
1163
|
+
`${gqlType?.name ?? gqlType.toString()} is not an object type`
|
|
1164
|
+
);
|
|
1099
1165
|
}
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
);
|
|
1166
|
+
const existing = this.context.loomObjectMap.get(gqlType);
|
|
1167
|
+
if (existing != null) return existing;
|
|
1168
|
+
const extraObject = new LoomObjectType(gqlType, this.fieldOptions);
|
|
1169
|
+
this.context.loomObjectMap.set(gqlType, extraObject);
|
|
1170
|
+
this.types.add(extraObject);
|
|
1171
|
+
return extraObject;
|
|
1103
1172
|
})();
|
|
1104
1173
|
if (resolverOptions?.extensions && parentObject)
|
|
1105
1174
|
parentObject.mergeExtensions(resolverOptions.extensions);
|
|
@@ -1174,12 +1243,12 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
1174
1243
|
}
|
|
1175
1244
|
/**
|
|
1176
1245
|
* Weave a GraphQL Schema from resolvers
|
|
1177
|
-
* @param inputs Resolvers, Global Middlewares
|
|
1178
|
-
* @returns
|
|
1246
|
+
* @param inputs Resolvers, Global Middlewares, WeaverConfigs Or SchemaWeaver
|
|
1247
|
+
* @returns GraphQL Schema
|
|
1179
1248
|
*/
|
|
1180
1249
|
static weave(...inputs) {
|
|
1181
|
-
const { context, configs, middlewares, resolvers, silks, weavers } =
|
|
1182
|
-
const weaver = new
|
|
1250
|
+
const { context, configs, middlewares, resolvers, silks, weavers } = _GraphQLSchemaLoom.optionsFrom(...inputs);
|
|
1251
|
+
const weaver = new _GraphQLSchemaLoom({}, context);
|
|
1183
1252
|
configs.forEach((it) => weaver.setConfig(it));
|
|
1184
1253
|
weavers.forEach((it) => weaver.addVendor(it));
|
|
1185
1254
|
middlewares.forEach((it) => weaver.use(it));
|
|
@@ -1188,7 +1257,7 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
1188
1257
|
return weaver.weaveGraphQLSchema();
|
|
1189
1258
|
}
|
|
1190
1259
|
};
|
|
1191
|
-
var weave =
|
|
1260
|
+
var weave = GraphQLSchemaLoom.weave;
|
|
1192
1261
|
|
|
1193
1262
|
// src/schema/interface.ts
|
|
1194
1263
|
import {
|
|
@@ -1221,10 +1290,11 @@ function ensureInterfaceType(gqlType, interfaceConfig) {
|
|
|
1221
1290
|
}
|
|
1222
1291
|
export {
|
|
1223
1292
|
ContextMemoization,
|
|
1293
|
+
GraphQLSchemaLoom,
|
|
1224
1294
|
LoomObjectType,
|
|
1295
|
+
OPERATION_OBJECT_NAMES,
|
|
1225
1296
|
ResolverOptionsMap,
|
|
1226
1297
|
symbols_exports as SYMBOLS,
|
|
1227
|
-
SchemaWeaver,
|
|
1228
1298
|
applyMiddlewares,
|
|
1229
1299
|
baseResolver,
|
|
1230
1300
|
collectName,
|
|
@@ -1267,6 +1337,7 @@ export {
|
|
|
1267
1337
|
onlyMemoization,
|
|
1268
1338
|
parseInputValue,
|
|
1269
1339
|
parseSilk,
|
|
1340
|
+
pascalCase,
|
|
1270
1341
|
provideWeaverContext,
|
|
1271
1342
|
query,
|
|
1272
1343
|
resolver,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gqloom/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Create GraphQL schema and resolvers with TypeScript.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -14,6 +14,10 @@
|
|
|
14
14
|
"require": {
|
|
15
15
|
"types": "./dist/index.d.cts",
|
|
16
16
|
"default": "./dist/index.cjs"
|
|
17
|
+
},
|
|
18
|
+
"source": {
|
|
19
|
+
"types": "./src/index.ts",
|
|
20
|
+
"default": "./src/index.ts"
|
|
17
21
|
}
|
|
18
22
|
}
|
|
19
23
|
},
|
|
@@ -46,6 +50,6 @@
|
|
|
46
50
|
"access": "public"
|
|
47
51
|
},
|
|
48
52
|
"devDependencies": {
|
|
49
|
-
"@standard-schema/spec": "1.0.0-beta.
|
|
53
|
+
"@standard-schema/spec": "1.0.0-beta.4"
|
|
50
54
|
}
|
|
51
55
|
}
|