@orion-js/graphql 4.0.0-next.9 → 4.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.
- package/dist/index.cjs +168 -113
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +42 -29
- package/dist/index.d.ts +42 -29
- package/dist/index.js +149 -101
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
package/dist/index.cjs
CHANGED
|
@@ -42,12 +42,17 @@ __export(index_exports, {
|
|
|
42
42
|
Subscription: () => Subscription,
|
|
43
43
|
Subscriptions: () => Subscriptions,
|
|
44
44
|
UseMiddleware: () => UseMiddleware,
|
|
45
|
+
createModelResolver: () => createModelResolver,
|
|
46
|
+
createMutation: () => createMutation,
|
|
47
|
+
createQuery: () => createQuery,
|
|
48
|
+
createResolver: () => import_resolvers2.createResolver,
|
|
45
49
|
getBasicResultQuery: () => getBasicQuery,
|
|
46
50
|
getServiceModelResolvers: () => getServiceModelResolvers,
|
|
47
51
|
getServiceResolvers: () => getServiceResolvers,
|
|
48
52
|
getServiceSubscriptions: () => getServiceSubscriptions,
|
|
49
53
|
getTargetMetadata: () => getTargetMetadata,
|
|
50
54
|
getWebsockerViewer: () => getWebsockerViewer,
|
|
55
|
+
internalResolversMetadata: () => internalResolversMetadata,
|
|
51
56
|
resolversSchemas: () => resolversSchemas_default,
|
|
52
57
|
serializeSchema: () => serializeSchema,
|
|
53
58
|
setGetWebsockerViewer: () => setGetWebsockerViewer,
|
|
@@ -57,6 +62,9 @@ __export(index_exports, {
|
|
|
57
62
|
});
|
|
58
63
|
module.exports = __toCommonJS(index_exports);
|
|
59
64
|
|
|
65
|
+
// src/subscription/index.ts
|
|
66
|
+
var import_schema = require("@orion-js/schema");
|
|
67
|
+
|
|
60
68
|
// src/pubsub.ts
|
|
61
69
|
var pubsub = null;
|
|
62
70
|
var setPubsub = function(newPubsub) {
|
|
@@ -75,28 +83,36 @@ function getChannelName_default(name, params) {
|
|
|
75
83
|
}
|
|
76
84
|
|
|
77
85
|
// src/subscription/index.ts
|
|
78
|
-
var
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
86
|
+
var createSubscription = (options) => {
|
|
87
|
+
const subscription = {};
|
|
88
|
+
const getSubscriptionName = () => {
|
|
89
|
+
if (!subscription.name) {
|
|
90
|
+
throw new Error("This subscription is not yet initialized in GraphQL");
|
|
91
|
+
}
|
|
92
|
+
return subscription.name;
|
|
82
93
|
};
|
|
83
94
|
subscription.publish = async (params, data) => {
|
|
84
95
|
const pubsub2 = getPubsub();
|
|
85
|
-
const channelName = getChannelName_default(
|
|
86
|
-
await pubsub2.publish(channelName, { [
|
|
96
|
+
const channelName = getChannelName_default(getSubscriptionName(), params);
|
|
97
|
+
await pubsub2.publish(channelName, { [getSubscriptionName()]: data });
|
|
87
98
|
};
|
|
88
99
|
subscription.subscribe = async (params, viewer) => {
|
|
89
100
|
const pubsub2 = getPubsub();
|
|
90
101
|
try {
|
|
91
|
-
|
|
92
|
-
|
|
102
|
+
if (options.canSubscribe) {
|
|
103
|
+
const canSubscribe = await options.canSubscribe(params, viewer);
|
|
104
|
+
if (!canSubscribe) {
|
|
105
|
+
return pubsub2.asyncIterator("unauthorized");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const channelName = getChannelName_default(getSubscriptionName(), params);
|
|
93
109
|
return pubsub2.asyncIterator(channelName);
|
|
94
|
-
} catch (
|
|
110
|
+
} catch (_error) {
|
|
95
111
|
return pubsub2.asyncIterator("unauthorized");
|
|
96
112
|
}
|
|
97
113
|
};
|
|
98
|
-
subscription.params = (0,
|
|
99
|
-
subscription.returns = (0,
|
|
114
|
+
subscription.params = (0, import_schema.getSchemaFromAnyOrionForm)(options.params) ?? {};
|
|
115
|
+
subscription.returns = (0, import_schema.getSchemaFromAnyOrionForm)(options.returns);
|
|
100
116
|
return subscription;
|
|
101
117
|
};
|
|
102
118
|
var subscription_default = createSubscription;
|
|
@@ -310,7 +326,7 @@ var import_graphql7 = require("graphql");
|
|
|
310
326
|
|
|
311
327
|
// src/buildSchema/getType/index.ts
|
|
312
328
|
var import_graphql6 = require("graphql");
|
|
313
|
-
var
|
|
329
|
+
var import_schema3 = require("@orion-js/schema");
|
|
314
330
|
|
|
315
331
|
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/type.js
|
|
316
332
|
function type(input) {
|
|
@@ -597,6 +613,9 @@ function getScalar_default(fieldType) {
|
|
|
597
613
|
if (fieldMap[fieldType.name]) {
|
|
598
614
|
return fieldMap[fieldType.name];
|
|
599
615
|
}
|
|
616
|
+
if (fieldType.name.startsWith("typedId:")) {
|
|
617
|
+
return fieldMap.string;
|
|
618
|
+
}
|
|
600
619
|
if (fieldType.toGraphQLType) {
|
|
601
620
|
const result = fieldType.toGraphQLType(GraphQL);
|
|
602
621
|
if (result.then) {
|
|
@@ -609,7 +628,7 @@ function getScalar_default(fieldType) {
|
|
|
609
628
|
|
|
610
629
|
// src/buildSchema/getArgs/getField.ts
|
|
611
630
|
var import_graphql4 = require("graphql");
|
|
612
|
-
var
|
|
631
|
+
var import_schema2 = require("@orion-js/schema");
|
|
613
632
|
|
|
614
633
|
// src/resolversSchemas/getStaticFields.ts
|
|
615
634
|
function getStaticFields(schema) {
|
|
@@ -666,7 +685,7 @@ var resolveType = (type2) => {
|
|
|
666
685
|
if (!type2.__isFieldType && (isType("Object", type2) || type2.__isModel)) {
|
|
667
686
|
return resolvePlainObjectOrModelType(type2);
|
|
668
687
|
}
|
|
669
|
-
const schemaType = (0,
|
|
688
|
+
const schemaType = (0, import_schema2.getFieldType)(type2);
|
|
670
689
|
return getScalar_default(schemaType);
|
|
671
690
|
};
|
|
672
691
|
var getField_default = resolveType;
|
|
@@ -676,7 +695,7 @@ async function getArgs_default(params) {
|
|
|
676
695
|
if (!params) return;
|
|
677
696
|
if (Object.keys(params).length === 0) return;
|
|
678
697
|
const fields = {};
|
|
679
|
-
for (const key of Object.keys(params)) {
|
|
698
|
+
for (const key of Object.keys(params).filter((key2) => !key2.startsWith("__"))) {
|
|
680
699
|
try {
|
|
681
700
|
const type2 = getField_default(params[key].type);
|
|
682
701
|
fields[key] = { type: type2 };
|
|
@@ -720,18 +739,18 @@ function errorHandler(error, data) {
|
|
|
720
739
|
}
|
|
721
740
|
|
|
722
741
|
// src/buildSchema/getType/getTypeAsResolver.ts
|
|
723
|
-
function getTypeAsResolver_default({ resolver
|
|
724
|
-
const type2 = getGraphQLType2(
|
|
725
|
-
const args = getArgs_default(
|
|
742
|
+
function getTypeAsResolver_default({ resolver, getGraphQLType: getGraphQLType2, options, schema }) {
|
|
743
|
+
const type2 = getGraphQLType2(resolver.returns, options);
|
|
744
|
+
const args = getArgs_default(resolver.params);
|
|
726
745
|
return {
|
|
727
746
|
type: type2,
|
|
728
747
|
args,
|
|
729
748
|
async resolve(item, params, context, info) {
|
|
730
749
|
try {
|
|
731
|
-
const result = await
|
|
750
|
+
const result = await resolver.resolve(item, params, context, info);
|
|
732
751
|
return result;
|
|
733
752
|
} catch (error) {
|
|
734
|
-
errorHandler(error, { context, resolver
|
|
753
|
+
errorHandler(error, { context, resolver, options, schema });
|
|
735
754
|
throw error;
|
|
736
755
|
}
|
|
737
756
|
}
|
|
@@ -754,12 +773,12 @@ function getDynamicFields(schema) {
|
|
|
754
773
|
if (!resolvers) return [];
|
|
755
774
|
const keys = Object.keys(resolvers);
|
|
756
775
|
return keys.map((key) => {
|
|
757
|
-
const
|
|
776
|
+
const resolver = resolvers[key];
|
|
758
777
|
return {
|
|
759
|
-
...
|
|
778
|
+
...resolver,
|
|
760
779
|
key
|
|
761
780
|
};
|
|
762
|
-
}).filter((
|
|
781
|
+
}).filter((resolver) => !resolver.private);
|
|
763
782
|
}
|
|
764
783
|
|
|
765
784
|
// src/resolversSchemas/getModelLoadedResolvers.ts
|
|
@@ -769,12 +788,12 @@ function getModelLoadedResolvers(schema, options) {
|
|
|
769
788
|
if (!resolvers) return [];
|
|
770
789
|
const keys = Object.keys(resolvers);
|
|
771
790
|
return keys.map((key) => {
|
|
772
|
-
const
|
|
791
|
+
const resolver = resolvers[key];
|
|
773
792
|
return {
|
|
774
|
-
...
|
|
793
|
+
...resolver,
|
|
775
794
|
key
|
|
776
795
|
};
|
|
777
|
-
}).filter((
|
|
796
|
+
}).filter((resolver) => !resolver.private);
|
|
778
797
|
}
|
|
779
798
|
|
|
780
799
|
// src/buildSchema/getType/index.ts
|
|
@@ -794,23 +813,23 @@ var buildFields = (schema, options) => {
|
|
|
794
813
|
}
|
|
795
814
|
};
|
|
796
815
|
const addDynamicFields = () => {
|
|
797
|
-
for (const
|
|
816
|
+
for (const resolver of getDynamicFields(schema)) {
|
|
798
817
|
try {
|
|
799
|
-
fields[
|
|
818
|
+
fields[resolver.key] = getTypeAsResolver_default({ resolver, getGraphQLType, options, schema });
|
|
800
819
|
} catch (error) {
|
|
801
820
|
throw new Error(
|
|
802
|
-
`Error getting resolver type for resolver "${
|
|
821
|
+
`Error getting resolver type for resolver "${resolver.key}": ${error.message}`
|
|
803
822
|
);
|
|
804
823
|
}
|
|
805
824
|
}
|
|
806
825
|
};
|
|
807
826
|
const addModelLoadedResolvers = () => {
|
|
808
|
-
for (const
|
|
827
|
+
for (const resolver of getModelLoadedResolvers(schema, options)) {
|
|
809
828
|
try {
|
|
810
|
-
fields[
|
|
829
|
+
fields[resolver.key] = getTypeAsResolver_default({ resolver, getGraphQLType, options, schema });
|
|
811
830
|
} catch (error) {
|
|
812
831
|
throw new Error(
|
|
813
|
-
`Error getting resolver type for resolver "${
|
|
832
|
+
`Error getting resolver type for resolver "${resolver.key}": ${error.message}`
|
|
814
833
|
);
|
|
815
834
|
}
|
|
816
835
|
}
|
|
@@ -826,8 +845,8 @@ function getGraphQLType(type2, options) {
|
|
|
826
845
|
if (Array.isArray(type2)) {
|
|
827
846
|
return new import_graphql6.GraphQLList(getGraphQLType(type2[0], options));
|
|
828
847
|
}
|
|
829
|
-
if ((0,
|
|
830
|
-
const schema = (0,
|
|
848
|
+
if ((0, import_schema3.isSchemaLike)(type2)) {
|
|
849
|
+
const schema = (0, import_schema3.getSchemaWithMetadataFromAnyOrionForm)(type2);
|
|
831
850
|
const modelName = schema.__modelName;
|
|
832
851
|
if (schema.__graphQLType) {
|
|
833
852
|
return schema.__graphQLType;
|
|
@@ -848,7 +867,7 @@ function getGraphQLType(type2, options) {
|
|
|
848
867
|
registeredGraphQLTypes.set(modelName, { schema, graphQLType });
|
|
849
868
|
return graphQLType;
|
|
850
869
|
}
|
|
851
|
-
return getScalar_default((0,
|
|
870
|
+
return getScalar_default((0, import_schema3.getFieldType)(type2));
|
|
852
871
|
}
|
|
853
872
|
|
|
854
873
|
// src/buildSchema/getResolvers/resolversStore.ts
|
|
@@ -862,21 +881,21 @@ async function getResolvers_default(options, mutation) {
|
|
|
862
881
|
name: key,
|
|
863
882
|
resolver: resolvers[key]
|
|
864
883
|
};
|
|
865
|
-
}).filter(({ resolver
|
|
884
|
+
}).filter(({ resolver }) => !!resolver.mutation === !!mutation).filter(({ resolver }) => !resolver.private);
|
|
866
885
|
const fields = {};
|
|
867
|
-
for (const { resolver
|
|
868
|
-
resolversStore[name] =
|
|
869
|
-
const type2 = await getGraphQLType(
|
|
870
|
-
const args = await getArgs_default(
|
|
886
|
+
for (const { resolver, name } of filteredResolvers) {
|
|
887
|
+
resolversStore[name] = resolver;
|
|
888
|
+
const type2 = await getGraphQLType(resolver.returns, options);
|
|
889
|
+
const args = await getArgs_default(resolver.params);
|
|
871
890
|
fields[name] = {
|
|
872
891
|
type: type2,
|
|
873
892
|
args,
|
|
874
893
|
async resolve(_root, params, context, info) {
|
|
875
894
|
try {
|
|
876
|
-
const result = await
|
|
895
|
+
const result = await resolver.resolve(params, context, info);
|
|
877
896
|
return result;
|
|
878
897
|
} catch (error) {
|
|
879
|
-
errorHandler(error, { context, resolver
|
|
898
|
+
errorHandler(error, { context, resolver, options, name });
|
|
880
899
|
throw error;
|
|
881
900
|
}
|
|
882
901
|
}
|
|
@@ -1079,12 +1098,12 @@ async function startGraphQL_default(options) {
|
|
|
1079
1098
|
}
|
|
1080
1099
|
|
|
1081
1100
|
// src/resolversSchemas/params.ts
|
|
1082
|
-
var
|
|
1101
|
+
var import_resolvers = require("@orion-js/resolvers");
|
|
1083
1102
|
var import_helpers2 = require("@orion-js/helpers");
|
|
1084
1103
|
|
|
1085
1104
|
// src/resolversSchemas/ResolverParamsInfo.ts
|
|
1086
|
-
var
|
|
1087
|
-
var ResolverParamsInfo_default = (0,
|
|
1105
|
+
var import_schema4 = require("@orion-js/schema");
|
|
1106
|
+
var ResolverParamsInfo_default = (0, import_schema4.schemaWithName)("ResolverParams", {
|
|
1088
1107
|
name: {
|
|
1089
1108
|
type: "string"
|
|
1090
1109
|
},
|
|
@@ -1100,13 +1119,12 @@ var ResolverParamsInfo_default = (0, import_schema3.schemaWithName)("ResolverPar
|
|
|
1100
1119
|
});
|
|
1101
1120
|
|
|
1102
1121
|
// src/resolversSchemas/serializeSchema.ts
|
|
1103
|
-
var
|
|
1122
|
+
var import_schema6 = require("@orion-js/schema");
|
|
1104
1123
|
|
|
1105
1124
|
// src/resolversSchemas/getField.ts
|
|
1106
|
-
var
|
|
1125
|
+
var import_schema5 = require("@orion-js/schema");
|
|
1107
1126
|
async function getParams(field) {
|
|
1108
1127
|
const { type: type2 } = field;
|
|
1109
|
-
console.log(type2, "getting parms");
|
|
1110
1128
|
if (Array.isArray(type2)) {
|
|
1111
1129
|
const serialized = await getParams({ ...field, type: type2[0] });
|
|
1112
1130
|
return {
|
|
@@ -1115,9 +1133,9 @@ async function getParams(field) {
|
|
|
1115
1133
|
__graphQLType: `[${serialized.__graphQLType}]`
|
|
1116
1134
|
};
|
|
1117
1135
|
}
|
|
1118
|
-
const isSchema = (0,
|
|
1136
|
+
const isSchema = (0, import_schema5.isSchemaLike)(type2);
|
|
1119
1137
|
if (isSchema) {
|
|
1120
|
-
const schemaOfType = (0,
|
|
1138
|
+
const schemaOfType = (0, import_schema5.getSchemaWithMetadataFromAnyOrionForm)(type2);
|
|
1121
1139
|
const modelName = schemaOfType.__modelName;
|
|
1122
1140
|
if (!modelName) {
|
|
1123
1141
|
throw new Error("The schema needs a model name to be serialized for GraphQL");
|
|
@@ -1132,7 +1150,7 @@ async function getParams(field) {
|
|
|
1132
1150
|
__graphQLType: `${modelName}Input`
|
|
1133
1151
|
};
|
|
1134
1152
|
}
|
|
1135
|
-
const schemaType = (0,
|
|
1153
|
+
const schemaType = (0, import_schema5.getFieldType)(type2);
|
|
1136
1154
|
const graphQLType = await getScalar_default(schemaType);
|
|
1137
1155
|
const withoutKey = isType("Object", field) ? omit(["key"], field) : {};
|
|
1138
1156
|
return {
|
|
@@ -1145,7 +1163,7 @@ async function getParams(field) {
|
|
|
1145
1163
|
// src/resolversSchemas/serializeSchema.ts
|
|
1146
1164
|
async function serializeSchema(params) {
|
|
1147
1165
|
if (!params) return;
|
|
1148
|
-
const schema = (0,
|
|
1166
|
+
const schema = (0, import_schema6.getSchemaFromAnyOrionForm)(params);
|
|
1149
1167
|
if (Object.keys(schema).length === 0) return;
|
|
1150
1168
|
const fields = {};
|
|
1151
1169
|
console.log(schema, "schema");
|
|
@@ -1159,10 +1177,10 @@ async function serializeSchema(params) {
|
|
|
1159
1177
|
}
|
|
1160
1178
|
|
|
1161
1179
|
// src/resolversSchemas/params.ts
|
|
1162
|
-
var
|
|
1180
|
+
var import_schema8 = require("@orion-js/schema");
|
|
1163
1181
|
|
|
1164
1182
|
// src/resolversSchemas/getBasicResultQuery.ts
|
|
1165
|
-
var
|
|
1183
|
+
var import_schema7 = require("@orion-js/schema");
|
|
1166
1184
|
async function getBasicQuery(field) {
|
|
1167
1185
|
if (!field.type) return "";
|
|
1168
1186
|
if (Array.isArray(field.type)) {
|
|
@@ -1171,10 +1189,10 @@ async function getBasicQuery(field) {
|
|
|
1171
1189
|
type: field.type[0]
|
|
1172
1190
|
});
|
|
1173
1191
|
}
|
|
1174
|
-
if (!(0,
|
|
1192
|
+
if (!(0, import_schema7.isSchemaLike)(field.type)) {
|
|
1175
1193
|
return field.key;
|
|
1176
1194
|
}
|
|
1177
|
-
const schema = (0,
|
|
1195
|
+
const schema = (0, import_schema7.getSchemaFromAnyOrionForm)(field.type);
|
|
1178
1196
|
const fields = [];
|
|
1179
1197
|
for (const field2 of getStaticFields(schema)) {
|
|
1180
1198
|
fields.push(await getBasicQuery(field2));
|
|
@@ -1186,21 +1204,21 @@ async function getBasicQuery(field) {
|
|
|
1186
1204
|
// src/resolversSchemas/params.ts
|
|
1187
1205
|
function getResultTypeName(type2) {
|
|
1188
1206
|
const returns = Array.isArray(type2) ? type2[0] : type2;
|
|
1189
|
-
const schema = (0,
|
|
1207
|
+
const schema = (0, import_schema8.getSchemaFromAnyOrionForm)(returns);
|
|
1190
1208
|
if (schema == null ? void 0 : schema.__modelName) return schema.__modelName;
|
|
1191
1209
|
return;
|
|
1192
1210
|
}
|
|
1193
1211
|
async function getInternalBasicResultQuery(type2) {
|
|
1194
1212
|
const returns = Array.isArray(type2) ? type2[0] : type2;
|
|
1195
|
-
if ((0,
|
|
1196
|
-
const schema = (0,
|
|
1213
|
+
if ((0, import_schema8.isSchemaLike)(returns)) {
|
|
1214
|
+
const schema = (0, import_schema8.getSchemaFromAnyOrionForm)(returns);
|
|
1197
1215
|
return await getBasicQuery({
|
|
1198
1216
|
type: schema
|
|
1199
1217
|
});
|
|
1200
1218
|
}
|
|
1201
1219
|
return "";
|
|
1202
1220
|
}
|
|
1203
|
-
var params_default = (0,
|
|
1221
|
+
var params_default = (0, import_resolvers.createResolver)({
|
|
1204
1222
|
params: {
|
|
1205
1223
|
name: {
|
|
1206
1224
|
type: "ID"
|
|
@@ -1212,21 +1230,21 @@ var params_default = (0, import_resolvers2.createResolver)({
|
|
|
1212
1230
|
returns: ResolverParamsInfo_default,
|
|
1213
1231
|
mutation: false,
|
|
1214
1232
|
async resolve({ mutation, name }) {
|
|
1215
|
-
const
|
|
1216
|
-
if (!
|
|
1233
|
+
const resolver = resolversStore[name];
|
|
1234
|
+
if (!resolver) {
|
|
1217
1235
|
throw new import_helpers2.UserError(
|
|
1218
1236
|
"notFound",
|
|
1219
1237
|
`${mutation ? "Mutation" : "Query"} named "${name}" not found`
|
|
1220
1238
|
);
|
|
1221
1239
|
}
|
|
1222
|
-
if (!!
|
|
1240
|
+
if (!!resolver.mutation !== !!mutation) {
|
|
1223
1241
|
throw new import_helpers2.UserError("incorrectType", `"${name}" is ${mutation ? "not" : ""} a mutation`);
|
|
1224
1242
|
}
|
|
1225
1243
|
return {
|
|
1226
1244
|
name,
|
|
1227
|
-
basicResultQuery: await getInternalBasicResultQuery(
|
|
1228
|
-
params: await serializeSchema(
|
|
1229
|
-
result: getResultTypeName(
|
|
1245
|
+
basicResultQuery: await getInternalBasicResultQuery(resolver.returns),
|
|
1246
|
+
params: await serializeSchema(resolver.params),
|
|
1247
|
+
result: getResultTypeName(resolver.returns)
|
|
1230
1248
|
};
|
|
1231
1249
|
}
|
|
1232
1250
|
});
|
|
@@ -1250,7 +1268,7 @@ var OrionSubscription = class {
|
|
|
1250
1268
|
|
|
1251
1269
|
// src/service/global.ts
|
|
1252
1270
|
var import_services = require("@orion-js/services");
|
|
1253
|
-
var
|
|
1271
|
+
var import_resolvers2 = require("@orion-js/resolvers");
|
|
1254
1272
|
|
|
1255
1273
|
// src/service/middlewares.ts
|
|
1256
1274
|
var resolversMetadata = /* @__PURE__ */ new WeakMap();
|
|
@@ -1293,97 +1311,132 @@ function ResolverReturns(returns) {
|
|
|
1293
1311
|
}
|
|
1294
1312
|
|
|
1295
1313
|
// src/service/global.ts
|
|
1314
|
+
var createQuery = import_resolvers2.createResolver;
|
|
1315
|
+
var createMutation = (options) => {
|
|
1316
|
+
return (0, import_resolvers2.createResolver)({
|
|
1317
|
+
...options,
|
|
1318
|
+
mutation: true
|
|
1319
|
+
});
|
|
1320
|
+
};
|
|
1296
1321
|
var serviceMetadata = /* @__PURE__ */ new WeakMap();
|
|
1297
|
-
var
|
|
1322
|
+
var internalResolversMetadata = /* @__PURE__ */ new WeakMap();
|
|
1298
1323
|
function Resolvers() {
|
|
1299
1324
|
return (target, context) => {
|
|
1300
1325
|
(0, import_services.Service)()(target, context);
|
|
1301
|
-
|
|
1302
|
-
serviceMetadata.set(this, { _serviceType: "resolvers" });
|
|
1303
|
-
});
|
|
1326
|
+
serviceMetadata.set(target, { _serviceType: "resolvers" });
|
|
1304
1327
|
};
|
|
1305
1328
|
}
|
|
1306
1329
|
function Query(options = {}) {
|
|
1307
1330
|
return (method, context) => {
|
|
1308
1331
|
const propertyKey = String(context.name);
|
|
1309
1332
|
context.addInitializer(function() {
|
|
1310
|
-
const resolvers =
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1333
|
+
const resolvers = internalResolversMetadata.get(this) || {};
|
|
1334
|
+
if (context.kind === "method") {
|
|
1335
|
+
resolvers[propertyKey] = (0, import_resolvers2.createResolver)({
|
|
1336
|
+
resolverId: propertyKey,
|
|
1337
|
+
params: getTargetMetadata(method, propertyKey, "params") || {},
|
|
1338
|
+
returns: getTargetMetadata(method, propertyKey, "returns") || "string",
|
|
1339
|
+
middlewares: getTargetMetadata(method, propertyKey, "middlewares") || [],
|
|
1340
|
+
...options,
|
|
1341
|
+
resolve: this[propertyKey].bind(this)
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
if (context.kind === "field") {
|
|
1345
|
+
resolvers[propertyKey] = this[propertyKey];
|
|
1346
|
+
}
|
|
1347
|
+
internalResolversMetadata.set(this, resolvers);
|
|
1320
1348
|
});
|
|
1321
1349
|
return method;
|
|
1322
1350
|
};
|
|
1323
1351
|
}
|
|
1324
|
-
function Mutation(options) {
|
|
1352
|
+
function Mutation(options = {}) {
|
|
1325
1353
|
return (method, context) => {
|
|
1326
1354
|
const propertyKey = String(context.name);
|
|
1327
1355
|
context.addInitializer(function() {
|
|
1328
|
-
const resolvers =
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1356
|
+
const resolvers = internalResolversMetadata.get(this) || {};
|
|
1357
|
+
if (context.kind === "method") {
|
|
1358
|
+
resolvers[propertyKey] = (0, import_resolvers2.createResolver)({
|
|
1359
|
+
resolverId: propertyKey,
|
|
1360
|
+
params: getTargetMetadata(method, propertyKey, "params") || {},
|
|
1361
|
+
returns: getTargetMetadata(method, propertyKey, "returns") || "string",
|
|
1362
|
+
middlewares: getTargetMetadata(method, propertyKey, "middlewares") || [],
|
|
1363
|
+
...options,
|
|
1364
|
+
mutation: true,
|
|
1365
|
+
resolve: this[propertyKey].bind(this)
|
|
1366
|
+
});
|
|
1367
|
+
}
|
|
1368
|
+
if (context.kind === "field") {
|
|
1369
|
+
this[propertyKey].mutation = true;
|
|
1370
|
+
resolvers[propertyKey] = this[propertyKey];
|
|
1371
|
+
}
|
|
1372
|
+
internalResolversMetadata.set(this, resolvers);
|
|
1339
1373
|
});
|
|
1340
1374
|
return method;
|
|
1341
1375
|
};
|
|
1342
1376
|
}
|
|
1343
1377
|
function getServiceResolvers(target) {
|
|
1344
1378
|
const instance = (0, import_services.getInstance)(target);
|
|
1379
|
+
const className = instance.constructor.name;
|
|
1380
|
+
const errorMessage = `You must pass a class decorated with @Resolvers to getServiceResolvers. Check the class ${className}`;
|
|
1345
1381
|
if (!serviceMetadata.has(instance.constructor)) {
|
|
1346
|
-
throw new Error(
|
|
1382
|
+
throw new Error(errorMessage);
|
|
1347
1383
|
}
|
|
1348
1384
|
const instanceMetadata = serviceMetadata.get(instance.constructor);
|
|
1349
1385
|
if (instanceMetadata._serviceType !== "resolvers") {
|
|
1350
|
-
throw new Error(
|
|
1386
|
+
throw new Error(`${errorMessage}. Got class type ${instanceMetadata._serviceType}`);
|
|
1351
1387
|
}
|
|
1352
|
-
const resolversMap =
|
|
1388
|
+
const resolversMap = internalResolversMetadata.get(instance) || {};
|
|
1353
1389
|
return resolversMap;
|
|
1354
1390
|
}
|
|
1355
1391
|
|
|
1356
1392
|
// src/service/model.ts
|
|
1357
1393
|
var import_services2 = require("@orion-js/services");
|
|
1358
|
-
var
|
|
1394
|
+
var import_resolvers3 = require("@orion-js/resolvers");
|
|
1395
|
+
var createModelResolver = import_resolvers3.createModelResolver;
|
|
1359
1396
|
var serviceMetadata2 = /* @__PURE__ */ new WeakMap();
|
|
1360
1397
|
var modelResolversMetadata = /* @__PURE__ */ new WeakMap();
|
|
1361
1398
|
function ModelResolvers(typedSchema, options = {}) {
|
|
1362
1399
|
return (target, context) => {
|
|
1363
1400
|
(0, import_services2.Service)()(target, context);
|
|
1364
|
-
const
|
|
1401
|
+
const className = context.name;
|
|
1402
|
+
const modelName = (() => {
|
|
1403
|
+
if (options.modelName) {
|
|
1404
|
+
return options.modelName;
|
|
1405
|
+
}
|
|
1406
|
+
if (typeof typedSchema.name === "string") {
|
|
1407
|
+
return typedSchema.name;
|
|
1408
|
+
}
|
|
1409
|
+
if (typedSchema.__modelName) {
|
|
1410
|
+
return typedSchema.__modelName;
|
|
1411
|
+
}
|
|
1412
|
+
throw new Error(`You must specify a model name for the model resolvers (at: ${className})`);
|
|
1413
|
+
})();
|
|
1365
1414
|
context.addInitializer(function() {
|
|
1366
1415
|
serviceMetadata2.set(this, {
|
|
1367
1416
|
_serviceType: "modelResolvers",
|
|
1368
1417
|
options,
|
|
1369
|
-
_typedSchema: typedSchema,
|
|
1370
1418
|
_modelName: modelName
|
|
1371
1419
|
});
|
|
1372
1420
|
});
|
|
1373
1421
|
};
|
|
1374
1422
|
}
|
|
1375
|
-
function ModelResolver(options) {
|
|
1423
|
+
function ModelResolver(options = {}) {
|
|
1376
1424
|
return (method, context) => {
|
|
1377
1425
|
const propertyKey = String(context.name);
|
|
1378
1426
|
context.addInitializer(function() {
|
|
1379
1427
|
const modelResolvers = modelResolversMetadata.get(this) || {};
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1428
|
+
if (context.kind === "method") {
|
|
1429
|
+
modelResolvers[propertyKey] = createModelResolver({
|
|
1430
|
+
params: getTargetMetadata(method, propertyKey, "params") || {},
|
|
1431
|
+
returns: getTargetMetadata(method, propertyKey, "returns") || "string",
|
|
1432
|
+
middlewares: getTargetMetadata(method, propertyKey, "middlewares") || [],
|
|
1433
|
+
...options,
|
|
1434
|
+
resolve: this[propertyKey].bind(this)
|
|
1435
|
+
});
|
|
1436
|
+
}
|
|
1437
|
+
if (context.kind === "field") {
|
|
1438
|
+
modelResolvers[propertyKey] = this[propertyKey];
|
|
1439
|
+
}
|
|
1387
1440
|
modelResolversMetadata.set(this, modelResolvers);
|
|
1388
1441
|
});
|
|
1389
1442
|
return method;
|
|
@@ -1420,8 +1473,8 @@ function Subscriptions() {
|
|
|
1420
1473
|
});
|
|
1421
1474
|
};
|
|
1422
1475
|
}
|
|
1423
|
-
function Subscription(
|
|
1424
|
-
return (
|
|
1476
|
+
function Subscription() {
|
|
1477
|
+
return (_method, context) => {
|
|
1425
1478
|
const propertyKey = String(context.name);
|
|
1426
1479
|
context.addInitializer(function() {
|
|
1427
1480
|
const repo = serviceMetadata3.get(this.constructor);
|
|
@@ -1431,10 +1484,7 @@ function Subscription(options) {
|
|
|
1431
1484
|
);
|
|
1432
1485
|
}
|
|
1433
1486
|
const subscriptions = subscriptionsMetadata.get(this) || {};
|
|
1434
|
-
subscriptions[propertyKey] =
|
|
1435
|
-
name: propertyKey,
|
|
1436
|
-
...options
|
|
1437
|
-
});
|
|
1487
|
+
subscriptions[propertyKey] = this[propertyKey];
|
|
1438
1488
|
subscriptionsMetadata.set(this, subscriptions);
|
|
1439
1489
|
this[propertyKey] = subscriptions[propertyKey];
|
|
1440
1490
|
});
|
|
@@ -1471,12 +1521,17 @@ function getServiceSubscriptions(target) {
|
|
|
1471
1521
|
Subscription,
|
|
1472
1522
|
Subscriptions,
|
|
1473
1523
|
UseMiddleware,
|
|
1524
|
+
createModelResolver,
|
|
1525
|
+
createMutation,
|
|
1526
|
+
createQuery,
|
|
1527
|
+
createResolver,
|
|
1474
1528
|
getBasicResultQuery,
|
|
1475
1529
|
getServiceModelResolvers,
|
|
1476
1530
|
getServiceResolvers,
|
|
1477
1531
|
getServiceSubscriptions,
|
|
1478
1532
|
getTargetMetadata,
|
|
1479
1533
|
getWebsockerViewer,
|
|
1534
|
+
internalResolversMetadata,
|
|
1480
1535
|
resolversSchemas,
|
|
1481
1536
|
serializeSchema,
|
|
1482
1537
|
setGetWebsockerViewer,
|