@orion-js/graphql 4.0.0-next.9 → 4.0.1
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.d.ts
CHANGED
|
@@ -1,27 +1,36 @@
|
|
|
1
1
|
import * as _orion_js_resolvers from '@orion-js/resolvers';
|
|
2
|
-
import { ResolverOptions,
|
|
2
|
+
import { ResolverOptions, GlobalResolverOptions, GlobalResolver, ModelResolver as ModelResolver$1, GlobalResolverResolve, ModelResolverResolve, ResolverMiddleware } from '@orion-js/resolvers';
|
|
3
|
+
export { createResolver } from '@orion-js/resolvers';
|
|
4
|
+
import * as _orion_js_schema from '@orion-js/schema';
|
|
5
|
+
import { SchemaFieldType, InferSchemaType, Schema, SchemaWithMetadata, SchemaNode } from '@orion-js/schema';
|
|
3
6
|
import { GlobalResolversMap, ModelResolversMap } from '@orion-js/models';
|
|
4
7
|
import { express } from '@orion-js/http';
|
|
5
8
|
import { PubSubEngine } from 'graphql-subscriptions';
|
|
6
9
|
import { ApolloServerOptions } from '@apollo/server';
|
|
7
|
-
import { Schema, SchemaWithMetadata, SchemaNode } from '@orion-js/schema';
|
|
8
10
|
import * as graphql from 'graphql';
|
|
9
11
|
export { graphql as GraphQL };
|
|
10
12
|
|
|
11
|
-
declare class OrionSubscription<
|
|
13
|
+
declare class OrionSubscription<TParamsSchema extends SchemaFieldType = SchemaFieldType, TReturnsSchema extends SchemaFieldType = SchemaFieldType> {
|
|
12
14
|
name: string;
|
|
13
|
-
params:
|
|
14
|
-
subscribe: (callParams:
|
|
15
|
-
returns:
|
|
16
|
-
publish: (params:
|
|
15
|
+
params: TParamsSchema;
|
|
16
|
+
subscribe: (callParams: InferSchemaType<TParamsSchema>, viewer: InferSchemaType<TReturnsSchema>) => {};
|
|
17
|
+
returns: TReturnsSchema;
|
|
18
|
+
publish: (params: InferSchemaType<TParamsSchema>, data: InferSchemaType<TReturnsSchema>) => Promise<void>;
|
|
17
19
|
}
|
|
18
|
-
type CreateOrionSubscriptionFunction = <
|
|
20
|
+
type CreateOrionSubscriptionFunction = <TParamsSchema extends SchemaFieldType = SchemaFieldType, TReturnsSchema extends SchemaFieldType = SchemaFieldType, TViewer = any>(options: OrionSubscriptionOptions<TParamsSchema, TReturnsSchema, TViewer>) => OrionSubscription<TParamsSchema, TReturnsSchema>;
|
|
19
21
|
interface OrionSubscriptionsMap {
|
|
20
22
|
[key: string]: OrionSubscription;
|
|
21
23
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
type OrionSubscriptionOptions<TParamsSchema extends SchemaFieldType = SchemaFieldType, TReturnsSchema extends SchemaFieldType = SchemaFieldType, TViewer = any> = Omit<ResolverOptions<TParamsSchema, TReturnsSchema, TViewer>, 'resolve'> & {
|
|
25
|
+
/**
|
|
26
|
+
* This function is used to check if the user can subscribe to the subscription.
|
|
27
|
+
* If not provided, the subscription will be public.
|
|
28
|
+
* @param params - The parameters of the subscription.
|
|
29
|
+
* @param viewer - The viewer of the subscription.
|
|
30
|
+
* @returns true if the user can subscribe to the subscription, false otherwise.
|
|
31
|
+
*/
|
|
32
|
+
canSubscribe?: (params: InferSchemaType<TParamsSchema>, viewer: TViewer) => Promise<boolean>;
|
|
33
|
+
};
|
|
25
34
|
|
|
26
35
|
declare const createSubscription: CreateOrionSubscriptionFunction;
|
|
27
36
|
|
|
@@ -66,7 +75,7 @@ declare function export_default$1(options: StartGraphQLOptions): Promise<void>;
|
|
|
66
75
|
declare function export_default(apolloOptions: ApolloServerOptions<any>, options: StartGraphQLOptions): void;
|
|
67
76
|
|
|
68
77
|
declare const _default$1: {
|
|
69
|
-
params: _orion_js_resolvers.
|
|
78
|
+
params: _orion_js_resolvers.GlobalResolver<{
|
|
70
79
|
name: {
|
|
71
80
|
type: "ID";
|
|
72
81
|
};
|
|
@@ -86,7 +95,7 @@ declare const _default$1: {
|
|
|
86
95
|
basicResultQuery: {
|
|
87
96
|
type: "string";
|
|
88
97
|
};
|
|
89
|
-
}, any, any
|
|
98
|
+
}, any, any>;
|
|
90
99
|
};
|
|
91
100
|
|
|
92
101
|
declare const _default: {
|
|
@@ -108,43 +117,47 @@ declare function serializeSchema(params: Schema): Promise<SchemaWithMetadata>;
|
|
|
108
117
|
|
|
109
118
|
declare function getBasicQuery(field: SchemaNode): Promise<string>;
|
|
110
119
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
120
|
+
declare const createQuery: <TParams extends SchemaFieldType = any, TReturns extends SchemaFieldType = any, TViewer = any, TInfo = any>(options: GlobalResolverOptions<TParams, TReturns, TViewer, TInfo>) => GlobalResolver<TParams, TReturns, TViewer, TInfo>;
|
|
121
|
+
declare const createMutation: <TParams extends SchemaFieldType = any, TReturns extends SchemaFieldType = any, TViewer = any, TInfo = any>(options: Omit<GlobalResolverOptions<TParams, TReturns, TViewer, TInfo>, "mutation">) => GlobalResolver<TParams, TReturns, TViewer, TInfo>;
|
|
122
|
+
declare const internalResolversMetadata: WeakMap<any, Record<string, any>>;
|
|
115
123
|
declare function Resolvers(): (target: any, context: ClassDecoratorContext<any>) => void;
|
|
116
|
-
declare function Query
|
|
117
|
-
declare function
|
|
124
|
+
declare function Query(): (method: any, context: ClassFieldDecoratorContext | ClassMethodDecoratorContext) => any;
|
|
125
|
+
declare function Query(options?: Omit<ResolverOptions<any>, 'resolve' | 'mutation'>): (method: any, context: ClassMethodDecoratorContext) => any;
|
|
126
|
+
declare function Mutation(): (method: any, context: ClassFieldDecoratorContext | ClassMethodDecoratorContext) => any;
|
|
127
|
+
declare function Mutation(options?: Omit<ResolverOptions<any>, 'resolve' | 'mutation'>): (method: any, context: ClassMethodDecoratorContext) => any;
|
|
118
128
|
declare function getServiceResolvers(target: any): {
|
|
119
|
-
[key: string]:
|
|
129
|
+
[key: string]: GlobalResolver<any, any, any, any>;
|
|
120
130
|
};
|
|
121
131
|
|
|
132
|
+
declare const createModelResolver: <TItem = any, TParams extends _orion_js_schema.SchemaFieldType = any, TReturns extends _orion_js_schema.SchemaFieldType = any, TViewer = any, TInfo = any>(options: _orion_js_resolvers.ModelResolverOptions<TItem, TParams, TReturns, TViewer, TInfo>) => ModelResolver$1<TItem, TParams, TReturns, TViewer, TInfo>;
|
|
122
133
|
interface ModelResolversOptions {
|
|
123
134
|
modelName?: string;
|
|
124
135
|
}
|
|
125
136
|
declare function ModelResolvers(typedSchema: any, options?: ModelResolversOptions): (target: any, context: ClassDecoratorContext<any>) => void;
|
|
126
|
-
declare function ModelResolver
|
|
137
|
+
declare function ModelResolver(): (method: any, context: ClassFieldDecoratorContext | ClassMethodDecoratorContext) => any;
|
|
138
|
+
declare function ModelResolver(options?: Omit<ResolverOptions<any>, 'resolve' | 'middlewares'>): (method: any, context: ClassMethodDecoratorContext) => any;
|
|
127
139
|
declare function getServiceModelResolvers(target: any): {
|
|
128
140
|
[key: string]: {
|
|
129
|
-
[key: string]: ModelResolver$1
|
|
141
|
+
[key: string]: ModelResolver$1;
|
|
130
142
|
};
|
|
131
143
|
};
|
|
132
144
|
|
|
133
145
|
declare function Subscriptions(): (target: any, context: ClassDecoratorContext<any>) => void;
|
|
134
|
-
declare function Subscription
|
|
135
|
-
params: TParams;
|
|
136
|
-
returns: TReturns;
|
|
137
|
-
}): (_target: any, context: ClassFieldDecoratorContext) => void;
|
|
146
|
+
declare function Subscription(): (method: any, context: ClassFieldDecoratorContext) => any;
|
|
138
147
|
declare function getServiceSubscriptions(target: any): {
|
|
139
148
|
[key: string]: OrionSubscription;
|
|
140
149
|
};
|
|
141
150
|
|
|
151
|
+
type InternalGlobalResolverResolveAtDecorator<This, TParams, TReturns, TViewer, TInfo> = (this: This, ...args: Parameters<GlobalResolverResolve<TParams, TReturns, TViewer, TInfo>>) => ReturnType<GlobalResolverResolve<TParams, TReturns, TViewer, TInfo>>;
|
|
152
|
+
type InternalModelResolverResolveAtDecorator<This, TItem, TParams extends SchemaFieldType, TReturns extends SchemaFieldType, TViewer, TInfo> = (this: This, ...args: Parameters<ModelResolverResolve<TItem, TParams, TReturns, TViewer, TInfo>>) => ReturnType<ModelResolverResolve<TItem, TParams, TReturns, TViewer, TInfo>>;
|
|
153
|
+
type InternalDynamicResolverResolveAtDecorator<This, TParams extends SchemaFieldType = any, TReturns extends SchemaFieldType = any, TViewer = any, TItem = any, TInfo = any> = TItem extends undefined ? InternalGlobalResolverResolveAtDecorator<This, TParams, TReturns, TViewer, TInfo> : InternalModelResolverResolveAtDecorator<This, TItem, TParams, TReturns, TViewer, TInfo>;
|
|
154
|
+
|
|
142
155
|
declare function getTargetMetadata(target: any, propertyKey: string, metadataKey: string): any;
|
|
143
156
|
declare function UseMiddleware<TMiddleware extends ResolverMiddleware, This>(params: TMiddleware): (method: InternalDynamicResolverResolveAtDecorator<This, any, any, any, any, any>, context: ClassMethodDecoratorContext<This, typeof method>) => InternalGlobalResolverResolveAtDecorator<This, any, any, any, any> | InternalModelResolverResolveAtDecorator<This, any, any, any, any, any>;
|
|
144
|
-
declare function ResolverParams<TParams, This>(params: TParams): (method: InternalDynamicResolverResolveAtDecorator<This, TParams>, context: ClassMethodDecoratorContext<This, typeof method>) => InternalGlobalResolverResolveAtDecorator<This, TParams, any, any, any> | InternalModelResolverResolveAtDecorator<This, any, TParams, any, any, any>;
|
|
145
|
-
declare function ResolverReturns<This>(returns: any): (method: InternalDynamicResolverResolveAtDecorator<This, any, any>, context: ClassMethodDecoratorContext<This, typeof method>) => InternalGlobalResolverResolveAtDecorator<This, any, any, any, any> | InternalModelResolverResolveAtDecorator<This, any, any, any, any, any>;
|
|
157
|
+
declare function ResolverParams<TParams extends SchemaFieldType, This>(params: TParams): (method: InternalDynamicResolverResolveAtDecorator<This, TParams, any, any, any, any>, context: ClassMethodDecoratorContext<This, typeof method>) => InternalGlobalResolverResolveAtDecorator<This, TParams, any, any, any> | InternalModelResolverResolveAtDecorator<This, any, TParams, any, any, any>;
|
|
158
|
+
declare function ResolverReturns<This>(returns: any): (method: InternalDynamicResolverResolveAtDecorator<This, any, any, any, any, any>, context: ClassMethodDecoratorContext<This, typeof method>) => InternalGlobalResolverResolveAtDecorator<This, any, any, any, any> | InternalModelResolverResolveAtDecorator<This, any, any, any, any, any>;
|
|
146
159
|
|
|
147
160
|
declare const getWebsockerViewer: (connectionParams: any) => Promise<any>;
|
|
148
161
|
declare const setGetWebsockerViewer: (getViewerFunc: (connectionParams: any) => any) => void;
|
|
149
162
|
|
|
150
|
-
export { type CreateOrionSubscriptionFunction, type ExecuteGraphQLCache, ModelResolver, ModelResolvers, type ModelResolversOptions, type ModelsResolversMap, Mutation, OrionSubscription, type OrionSubscriptionOptions, type OrionSubscriptionsMap, Query, ResolverParams, _default as ResolverParamsInfo, ResolverReturns, Resolvers, type StartGraphQLOptions, Subscription, Subscriptions, UseMiddleware, getBasicQuery as getBasicResultQuery, getServiceModelResolvers, getServiceResolvers, getServiceSubscriptions, getTargetMetadata, getWebsockerViewer, _default$1 as resolversSchemas, serializeSchema, setGetWebsockerViewer, export_default$1 as startGraphQL, export_default as startGraphiQL, createSubscription as subscription };
|
|
163
|
+
export { type CreateOrionSubscriptionFunction, type ExecuteGraphQLCache, ModelResolver, ModelResolvers, type ModelResolversOptions, type ModelsResolversMap, Mutation, OrionSubscription, type OrionSubscriptionOptions, type OrionSubscriptionsMap, Query, ResolverParams, _default as ResolverParamsInfo, ResolverReturns, Resolvers, type StartGraphQLOptions, Subscription, Subscriptions, UseMiddleware, createModelResolver, createMutation, createQuery, getBasicQuery as getBasicResultQuery, getServiceModelResolvers, getServiceResolvers, getServiceSubscriptions, getTargetMetadata, getWebsockerViewer, internalResolversMetadata, _default$1 as resolversSchemas, serializeSchema, setGetWebsockerViewer, export_default$1 as startGraphQL, export_default as startGraphiQL, createSubscription as subscription };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// src/subscription/index.ts
|
|
2
|
+
import { getSchemaFromAnyOrionForm } from "@orion-js/schema";
|
|
3
|
+
|
|
1
4
|
// src/pubsub.ts
|
|
2
5
|
var pubsub = null;
|
|
3
6
|
var setPubsub = function(newPubsub) {
|
|
@@ -16,32 +19,36 @@ function getChannelName_default(name, params) {
|
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
// src/subscription/index.ts
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
name: options.name
|
|
22
|
+
var createSubscription = (options) => {
|
|
23
|
+
const subscription = {};
|
|
24
|
+
const getSubscriptionName = () => {
|
|
25
|
+
if (!subscription.name) {
|
|
26
|
+
throw new Error("This subscription is not yet initialized in GraphQL");
|
|
27
|
+
}
|
|
28
|
+
return subscription.name;
|
|
27
29
|
};
|
|
28
30
|
subscription.publish = async (params, data) => {
|
|
29
31
|
const pubsub2 = getPubsub();
|
|
30
|
-
const channelName = getChannelName_default(
|
|
31
|
-
await pubsub2.publish(channelName, { [
|
|
32
|
+
const channelName = getChannelName_default(getSubscriptionName(), params);
|
|
33
|
+
await pubsub2.publish(channelName, { [getSubscriptionName()]: data });
|
|
32
34
|
};
|
|
33
35
|
subscription.subscribe = async (params, viewer) => {
|
|
34
36
|
const pubsub2 = getPubsub();
|
|
35
37
|
try {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
if (options.canSubscribe) {
|
|
39
|
+
const canSubscribe = await options.canSubscribe(params, viewer);
|
|
40
|
+
if (!canSubscribe) {
|
|
41
|
+
return pubsub2.asyncIterator("unauthorized");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const channelName = getChannelName_default(getSubscriptionName(), params);
|
|
38
45
|
return pubsub2.asyncIterator(channelName);
|
|
39
|
-
} catch (
|
|
46
|
+
} catch (_error) {
|
|
40
47
|
return pubsub2.asyncIterator("unauthorized");
|
|
41
48
|
}
|
|
42
49
|
};
|
|
43
|
-
subscription.params =
|
|
44
|
-
subscription.returns =
|
|
50
|
+
subscription.params = getSchemaFromAnyOrionForm(options.params) ?? {};
|
|
51
|
+
subscription.returns = getSchemaFromAnyOrionForm(options.returns);
|
|
45
52
|
return subscription;
|
|
46
53
|
};
|
|
47
54
|
var subscription_default = createSubscription;
|
|
@@ -546,6 +553,9 @@ function getScalar_default(fieldType) {
|
|
|
546
553
|
if (fieldMap[fieldType.name]) {
|
|
547
554
|
return fieldMap[fieldType.name];
|
|
548
555
|
}
|
|
556
|
+
if (fieldType.name.startsWith("typedId:")) {
|
|
557
|
+
return fieldMap.string;
|
|
558
|
+
}
|
|
549
559
|
if (fieldType.toGraphQLType) {
|
|
550
560
|
const result = fieldType.toGraphQLType(GraphQL);
|
|
551
561
|
if (result.then) {
|
|
@@ -625,7 +635,7 @@ async function getArgs_default(params) {
|
|
|
625
635
|
if (!params) return;
|
|
626
636
|
if (Object.keys(params).length === 0) return;
|
|
627
637
|
const fields = {};
|
|
628
|
-
for (const key of Object.keys(params)) {
|
|
638
|
+
for (const key of Object.keys(params).filter((key2) => !key2.startsWith("__"))) {
|
|
629
639
|
try {
|
|
630
640
|
const type2 = getField_default(params[key].type);
|
|
631
641
|
fields[key] = { type: type2 };
|
|
@@ -669,18 +679,18 @@ function errorHandler(error, data) {
|
|
|
669
679
|
}
|
|
670
680
|
|
|
671
681
|
// src/buildSchema/getType/getTypeAsResolver.ts
|
|
672
|
-
function getTypeAsResolver_default({ resolver
|
|
673
|
-
const type2 = getGraphQLType2(
|
|
674
|
-
const args = getArgs_default(
|
|
682
|
+
function getTypeAsResolver_default({ resolver, getGraphQLType: getGraphQLType2, options, schema }) {
|
|
683
|
+
const type2 = getGraphQLType2(resolver.returns, options);
|
|
684
|
+
const args = getArgs_default(resolver.params);
|
|
675
685
|
return {
|
|
676
686
|
type: type2,
|
|
677
687
|
args,
|
|
678
688
|
async resolve(item, params, context, info) {
|
|
679
689
|
try {
|
|
680
|
-
const result = await
|
|
690
|
+
const result = await resolver.resolve(item, params, context, info);
|
|
681
691
|
return result;
|
|
682
692
|
} catch (error) {
|
|
683
|
-
errorHandler(error, { context, resolver
|
|
693
|
+
errorHandler(error, { context, resolver, options, schema });
|
|
684
694
|
throw error;
|
|
685
695
|
}
|
|
686
696
|
}
|
|
@@ -703,12 +713,12 @@ function getDynamicFields(schema) {
|
|
|
703
713
|
if (!resolvers) return [];
|
|
704
714
|
const keys = Object.keys(resolvers);
|
|
705
715
|
return keys.map((key) => {
|
|
706
|
-
const
|
|
716
|
+
const resolver = resolvers[key];
|
|
707
717
|
return {
|
|
708
|
-
...
|
|
718
|
+
...resolver,
|
|
709
719
|
key
|
|
710
720
|
};
|
|
711
|
-
}).filter((
|
|
721
|
+
}).filter((resolver) => !resolver.private);
|
|
712
722
|
}
|
|
713
723
|
|
|
714
724
|
// src/resolversSchemas/getModelLoadedResolvers.ts
|
|
@@ -718,12 +728,12 @@ function getModelLoadedResolvers(schema, options) {
|
|
|
718
728
|
if (!resolvers) return [];
|
|
719
729
|
const keys = Object.keys(resolvers);
|
|
720
730
|
return keys.map((key) => {
|
|
721
|
-
const
|
|
731
|
+
const resolver = resolvers[key];
|
|
722
732
|
return {
|
|
723
|
-
...
|
|
733
|
+
...resolver,
|
|
724
734
|
key
|
|
725
735
|
};
|
|
726
|
-
}).filter((
|
|
736
|
+
}).filter((resolver) => !resolver.private);
|
|
727
737
|
}
|
|
728
738
|
|
|
729
739
|
// src/buildSchema/getType/index.ts
|
|
@@ -743,23 +753,23 @@ var buildFields = (schema, options) => {
|
|
|
743
753
|
}
|
|
744
754
|
};
|
|
745
755
|
const addDynamicFields = () => {
|
|
746
|
-
for (const
|
|
756
|
+
for (const resolver of getDynamicFields(schema)) {
|
|
747
757
|
try {
|
|
748
|
-
fields[
|
|
758
|
+
fields[resolver.key] = getTypeAsResolver_default({ resolver, getGraphQLType, options, schema });
|
|
749
759
|
} catch (error) {
|
|
750
760
|
throw new Error(
|
|
751
|
-
`Error getting resolver type for resolver "${
|
|
761
|
+
`Error getting resolver type for resolver "${resolver.key}": ${error.message}`
|
|
752
762
|
);
|
|
753
763
|
}
|
|
754
764
|
}
|
|
755
765
|
};
|
|
756
766
|
const addModelLoadedResolvers = () => {
|
|
757
|
-
for (const
|
|
767
|
+
for (const resolver of getModelLoadedResolvers(schema, options)) {
|
|
758
768
|
try {
|
|
759
|
-
fields[
|
|
769
|
+
fields[resolver.key] = getTypeAsResolver_default({ resolver, getGraphQLType, options, schema });
|
|
760
770
|
} catch (error) {
|
|
761
771
|
throw new Error(
|
|
762
|
-
`Error getting resolver type for resolver "${
|
|
772
|
+
`Error getting resolver type for resolver "${resolver.key}": ${error.message}`
|
|
763
773
|
);
|
|
764
774
|
}
|
|
765
775
|
}
|
|
@@ -811,21 +821,21 @@ async function getResolvers_default(options, mutation) {
|
|
|
811
821
|
name: key,
|
|
812
822
|
resolver: resolvers[key]
|
|
813
823
|
};
|
|
814
|
-
}).filter(({ resolver
|
|
824
|
+
}).filter(({ resolver }) => !!resolver.mutation === !!mutation).filter(({ resolver }) => !resolver.private);
|
|
815
825
|
const fields = {};
|
|
816
|
-
for (const { resolver
|
|
817
|
-
resolversStore[name] =
|
|
818
|
-
const type2 = await getGraphQLType(
|
|
819
|
-
const args = await getArgs_default(
|
|
826
|
+
for (const { resolver, name } of filteredResolvers) {
|
|
827
|
+
resolversStore[name] = resolver;
|
|
828
|
+
const type2 = await getGraphQLType(resolver.returns, options);
|
|
829
|
+
const args = await getArgs_default(resolver.params);
|
|
820
830
|
fields[name] = {
|
|
821
831
|
type: type2,
|
|
822
832
|
args,
|
|
823
833
|
async resolve(_root, params, context, info) {
|
|
824
834
|
try {
|
|
825
|
-
const result = await
|
|
835
|
+
const result = await resolver.resolve(params, context, info);
|
|
826
836
|
return result;
|
|
827
837
|
} catch (error) {
|
|
828
|
-
errorHandler(error, { context, resolver
|
|
838
|
+
errorHandler(error, { context, resolver, options, name });
|
|
829
839
|
throw error;
|
|
830
840
|
}
|
|
831
841
|
}
|
|
@@ -1049,7 +1059,7 @@ var ResolverParamsInfo_default = schemaWithName("ResolverParams", {
|
|
|
1049
1059
|
});
|
|
1050
1060
|
|
|
1051
1061
|
// src/resolversSchemas/serializeSchema.ts
|
|
1052
|
-
import { getSchemaFromAnyOrionForm } from "@orion-js/schema";
|
|
1062
|
+
import { getSchemaFromAnyOrionForm as getSchemaFromAnyOrionForm2 } from "@orion-js/schema";
|
|
1053
1063
|
|
|
1054
1064
|
// src/resolversSchemas/getField.ts
|
|
1055
1065
|
import {
|
|
@@ -1059,7 +1069,6 @@ import {
|
|
|
1059
1069
|
} from "@orion-js/schema";
|
|
1060
1070
|
async function getParams(field) {
|
|
1061
1071
|
const { type: type2 } = field;
|
|
1062
|
-
console.log(type2, "getting parms");
|
|
1063
1072
|
if (Array.isArray(type2)) {
|
|
1064
1073
|
const serialized = await getParams({ ...field, type: type2[0] });
|
|
1065
1074
|
return {
|
|
@@ -1098,7 +1107,7 @@ async function getParams(field) {
|
|
|
1098
1107
|
// src/resolversSchemas/serializeSchema.ts
|
|
1099
1108
|
async function serializeSchema(params) {
|
|
1100
1109
|
if (!params) return;
|
|
1101
|
-
const schema =
|
|
1110
|
+
const schema = getSchemaFromAnyOrionForm2(params);
|
|
1102
1111
|
if (Object.keys(schema).length === 0) return;
|
|
1103
1112
|
const fields = {};
|
|
1104
1113
|
console.log(schema, "schema");
|
|
@@ -1112,10 +1121,10 @@ async function serializeSchema(params) {
|
|
|
1112
1121
|
}
|
|
1113
1122
|
|
|
1114
1123
|
// src/resolversSchemas/params.ts
|
|
1115
|
-
import { getSchemaFromAnyOrionForm as
|
|
1124
|
+
import { getSchemaFromAnyOrionForm as getSchemaFromAnyOrionForm4, isSchemaLike as isSchemaLike4 } from "@orion-js/schema";
|
|
1116
1125
|
|
|
1117
1126
|
// src/resolversSchemas/getBasicResultQuery.ts
|
|
1118
|
-
import { getSchemaFromAnyOrionForm as
|
|
1127
|
+
import { getSchemaFromAnyOrionForm as getSchemaFromAnyOrionForm3, isSchemaLike as isSchemaLike3 } from "@orion-js/schema";
|
|
1119
1128
|
async function getBasicQuery(field) {
|
|
1120
1129
|
if (!field.type) return "";
|
|
1121
1130
|
if (Array.isArray(field.type)) {
|
|
@@ -1127,7 +1136,7 @@ async function getBasicQuery(field) {
|
|
|
1127
1136
|
if (!isSchemaLike3(field.type)) {
|
|
1128
1137
|
return field.key;
|
|
1129
1138
|
}
|
|
1130
|
-
const schema =
|
|
1139
|
+
const schema = getSchemaFromAnyOrionForm3(field.type);
|
|
1131
1140
|
const fields = [];
|
|
1132
1141
|
for (const field2 of getStaticFields(schema)) {
|
|
1133
1142
|
fields.push(await getBasicQuery(field2));
|
|
@@ -1139,14 +1148,14 @@ async function getBasicQuery(field) {
|
|
|
1139
1148
|
// src/resolversSchemas/params.ts
|
|
1140
1149
|
function getResultTypeName(type2) {
|
|
1141
1150
|
const returns = Array.isArray(type2) ? type2[0] : type2;
|
|
1142
|
-
const schema =
|
|
1151
|
+
const schema = getSchemaFromAnyOrionForm4(returns);
|
|
1143
1152
|
if (schema == null ? void 0 : schema.__modelName) return schema.__modelName;
|
|
1144
1153
|
return;
|
|
1145
1154
|
}
|
|
1146
1155
|
async function getInternalBasicResultQuery(type2) {
|
|
1147
1156
|
const returns = Array.isArray(type2) ? type2[0] : type2;
|
|
1148
1157
|
if (isSchemaLike4(returns)) {
|
|
1149
|
-
const schema =
|
|
1158
|
+
const schema = getSchemaFromAnyOrionForm4(returns);
|
|
1150
1159
|
return await getBasicQuery({
|
|
1151
1160
|
type: schema
|
|
1152
1161
|
});
|
|
@@ -1165,21 +1174,21 @@ var params_default = createResolver({
|
|
|
1165
1174
|
returns: ResolverParamsInfo_default,
|
|
1166
1175
|
mutation: false,
|
|
1167
1176
|
async resolve({ mutation, name }) {
|
|
1168
|
-
const
|
|
1169
|
-
if (!
|
|
1177
|
+
const resolver = resolversStore[name];
|
|
1178
|
+
if (!resolver) {
|
|
1170
1179
|
throw new UserError(
|
|
1171
1180
|
"notFound",
|
|
1172
1181
|
`${mutation ? "Mutation" : "Query"} named "${name}" not found`
|
|
1173
1182
|
);
|
|
1174
1183
|
}
|
|
1175
|
-
if (!!
|
|
1184
|
+
if (!!resolver.mutation !== !!mutation) {
|
|
1176
1185
|
throw new UserError("incorrectType", `"${name}" is ${mutation ? "not" : ""} a mutation`);
|
|
1177
1186
|
}
|
|
1178
1187
|
return {
|
|
1179
1188
|
name,
|
|
1180
|
-
basicResultQuery: await getInternalBasicResultQuery(
|
|
1181
|
-
params: await serializeSchema(
|
|
1182
|
-
result: getResultTypeName(
|
|
1189
|
+
basicResultQuery: await getInternalBasicResultQuery(resolver.returns),
|
|
1190
|
+
params: await serializeSchema(resolver.params),
|
|
1191
|
+
result: getResultTypeName(resolver.returns)
|
|
1183
1192
|
};
|
|
1184
1193
|
}
|
|
1185
1194
|
});
|
|
@@ -1203,7 +1212,9 @@ var OrionSubscription = class {
|
|
|
1203
1212
|
|
|
1204
1213
|
// src/service/global.ts
|
|
1205
1214
|
import { getInstance, Service } from "@orion-js/services";
|
|
1206
|
-
import {
|
|
1215
|
+
import {
|
|
1216
|
+
createResolver as createResolver2
|
|
1217
|
+
} from "@orion-js/resolvers";
|
|
1207
1218
|
|
|
1208
1219
|
// src/service/middlewares.ts
|
|
1209
1220
|
var resolversMetadata = /* @__PURE__ */ new WeakMap();
|
|
@@ -1246,99 +1257,134 @@ function ResolverReturns(returns) {
|
|
|
1246
1257
|
}
|
|
1247
1258
|
|
|
1248
1259
|
// src/service/global.ts
|
|
1260
|
+
var createQuery = createResolver2;
|
|
1261
|
+
var createMutation = (options) => {
|
|
1262
|
+
return createResolver2({
|
|
1263
|
+
...options,
|
|
1264
|
+
mutation: true
|
|
1265
|
+
});
|
|
1266
|
+
};
|
|
1249
1267
|
var serviceMetadata = /* @__PURE__ */ new WeakMap();
|
|
1250
|
-
var
|
|
1268
|
+
var internalResolversMetadata = /* @__PURE__ */ new WeakMap();
|
|
1251
1269
|
function Resolvers() {
|
|
1252
1270
|
return (target, context) => {
|
|
1253
1271
|
Service()(target, context);
|
|
1254
|
-
|
|
1255
|
-
serviceMetadata.set(this, { _serviceType: "resolvers" });
|
|
1256
|
-
});
|
|
1272
|
+
serviceMetadata.set(target, { _serviceType: "resolvers" });
|
|
1257
1273
|
};
|
|
1258
1274
|
}
|
|
1259
1275
|
function Query(options = {}) {
|
|
1260
1276
|
return (method, context) => {
|
|
1261
1277
|
const propertyKey = String(context.name);
|
|
1262
1278
|
context.addInitializer(function() {
|
|
1263
|
-
const resolvers =
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1279
|
+
const resolvers = internalResolversMetadata.get(this) || {};
|
|
1280
|
+
if (context.kind === "method") {
|
|
1281
|
+
resolvers[propertyKey] = createResolver2({
|
|
1282
|
+
resolverId: propertyKey,
|
|
1283
|
+
params: getTargetMetadata(method, propertyKey, "params") || {},
|
|
1284
|
+
returns: getTargetMetadata(method, propertyKey, "returns") || "string",
|
|
1285
|
+
middlewares: getTargetMetadata(method, propertyKey, "middlewares") || [],
|
|
1286
|
+
...options,
|
|
1287
|
+
resolve: this[propertyKey].bind(this)
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
if (context.kind === "field") {
|
|
1291
|
+
resolvers[propertyKey] = this[propertyKey];
|
|
1292
|
+
}
|
|
1293
|
+
internalResolversMetadata.set(this, resolvers);
|
|
1273
1294
|
});
|
|
1274
1295
|
return method;
|
|
1275
1296
|
};
|
|
1276
1297
|
}
|
|
1277
|
-
function Mutation(options) {
|
|
1298
|
+
function Mutation(options = {}) {
|
|
1278
1299
|
return (method, context) => {
|
|
1279
1300
|
const propertyKey = String(context.name);
|
|
1280
1301
|
context.addInitializer(function() {
|
|
1281
|
-
const resolvers =
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1302
|
+
const resolvers = internalResolversMetadata.get(this) || {};
|
|
1303
|
+
if (context.kind === "method") {
|
|
1304
|
+
resolvers[propertyKey] = createResolver2({
|
|
1305
|
+
resolverId: propertyKey,
|
|
1306
|
+
params: getTargetMetadata(method, propertyKey, "params") || {},
|
|
1307
|
+
returns: getTargetMetadata(method, propertyKey, "returns") || "string",
|
|
1308
|
+
middlewares: getTargetMetadata(method, propertyKey, "middlewares") || [],
|
|
1309
|
+
...options,
|
|
1310
|
+
mutation: true,
|
|
1311
|
+
resolve: this[propertyKey].bind(this)
|
|
1312
|
+
});
|
|
1313
|
+
}
|
|
1314
|
+
if (context.kind === "field") {
|
|
1315
|
+
this[propertyKey].mutation = true;
|
|
1316
|
+
resolvers[propertyKey] = this[propertyKey];
|
|
1317
|
+
}
|
|
1318
|
+
internalResolversMetadata.set(this, resolvers);
|
|
1292
1319
|
});
|
|
1293
1320
|
return method;
|
|
1294
1321
|
};
|
|
1295
1322
|
}
|
|
1296
1323
|
function getServiceResolvers(target) {
|
|
1297
1324
|
const instance = getInstance(target);
|
|
1325
|
+
const className = instance.constructor.name;
|
|
1326
|
+
const errorMessage = `You must pass a class decorated with @Resolvers to getServiceResolvers. Check the class ${className}`;
|
|
1298
1327
|
if (!serviceMetadata.has(instance.constructor)) {
|
|
1299
|
-
throw new Error(
|
|
1328
|
+
throw new Error(errorMessage);
|
|
1300
1329
|
}
|
|
1301
1330
|
const instanceMetadata = serviceMetadata.get(instance.constructor);
|
|
1302
1331
|
if (instanceMetadata._serviceType !== "resolvers") {
|
|
1303
|
-
throw new Error(
|
|
1332
|
+
throw new Error(`${errorMessage}. Got class type ${instanceMetadata._serviceType}`);
|
|
1304
1333
|
}
|
|
1305
|
-
const resolversMap =
|
|
1334
|
+
const resolversMap = internalResolversMetadata.get(instance) || {};
|
|
1306
1335
|
return resolversMap;
|
|
1307
1336
|
}
|
|
1308
1337
|
|
|
1309
1338
|
// src/service/model.ts
|
|
1310
1339
|
import { getInstance as getInstance2, Service as Service2 } from "@orion-js/services";
|
|
1311
1340
|
import {
|
|
1312
|
-
|
|
1341
|
+
createModelResolver as createModelResolverFromResolvers
|
|
1313
1342
|
} from "@orion-js/resolvers";
|
|
1343
|
+
var createModelResolver = createModelResolverFromResolvers;
|
|
1314
1344
|
var serviceMetadata2 = /* @__PURE__ */ new WeakMap();
|
|
1315
1345
|
var modelResolversMetadata = /* @__PURE__ */ new WeakMap();
|
|
1316
1346
|
function ModelResolvers(typedSchema, options = {}) {
|
|
1317
1347
|
return (target, context) => {
|
|
1318
1348
|
Service2()(target, context);
|
|
1319
|
-
const
|
|
1349
|
+
const className = context.name;
|
|
1350
|
+
const modelName = (() => {
|
|
1351
|
+
if (options.modelName) {
|
|
1352
|
+
return options.modelName;
|
|
1353
|
+
}
|
|
1354
|
+
if (typeof typedSchema.name === "string") {
|
|
1355
|
+
return typedSchema.name;
|
|
1356
|
+
}
|
|
1357
|
+
if (typedSchema.__modelName) {
|
|
1358
|
+
return typedSchema.__modelName;
|
|
1359
|
+
}
|
|
1360
|
+
throw new Error(`You must specify a model name for the model resolvers (at: ${className})`);
|
|
1361
|
+
})();
|
|
1320
1362
|
context.addInitializer(function() {
|
|
1321
1363
|
serviceMetadata2.set(this, {
|
|
1322
1364
|
_serviceType: "modelResolvers",
|
|
1323
1365
|
options,
|
|
1324
|
-
_typedSchema: typedSchema,
|
|
1325
1366
|
_modelName: modelName
|
|
1326
1367
|
});
|
|
1327
1368
|
});
|
|
1328
1369
|
};
|
|
1329
1370
|
}
|
|
1330
|
-
function ModelResolver(options) {
|
|
1371
|
+
function ModelResolver(options = {}) {
|
|
1331
1372
|
return (method, context) => {
|
|
1332
1373
|
const propertyKey = String(context.name);
|
|
1333
1374
|
context.addInitializer(function() {
|
|
1334
1375
|
const modelResolvers = modelResolversMetadata.get(this) || {};
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1376
|
+
if (context.kind === "method") {
|
|
1377
|
+
modelResolvers[propertyKey] = createModelResolver({
|
|
1378
|
+
params: getTargetMetadata(method, propertyKey, "params") || {},
|
|
1379
|
+
returns: getTargetMetadata(method, propertyKey, "returns") || "string",
|
|
1380
|
+
middlewares: getTargetMetadata(method, propertyKey, "middlewares") || [],
|
|
1381
|
+
...options,
|
|
1382
|
+
resolve: this[propertyKey].bind(this)
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
if (context.kind === "field") {
|
|
1386
|
+
modelResolvers[propertyKey] = this[propertyKey];
|
|
1387
|
+
}
|
|
1342
1388
|
modelResolversMetadata.set(this, modelResolvers);
|
|
1343
1389
|
});
|
|
1344
1390
|
return method;
|
|
@@ -1375,8 +1421,8 @@ function Subscriptions() {
|
|
|
1375
1421
|
});
|
|
1376
1422
|
};
|
|
1377
1423
|
}
|
|
1378
|
-
function Subscription(
|
|
1379
|
-
return (
|
|
1424
|
+
function Subscription() {
|
|
1425
|
+
return (_method, context) => {
|
|
1380
1426
|
const propertyKey = String(context.name);
|
|
1381
1427
|
context.addInitializer(function() {
|
|
1382
1428
|
const repo = serviceMetadata3.get(this.constructor);
|
|
@@ -1386,10 +1432,7 @@ function Subscription(options) {
|
|
|
1386
1432
|
);
|
|
1387
1433
|
}
|
|
1388
1434
|
const subscriptions = subscriptionsMetadata.get(this) || {};
|
|
1389
|
-
subscriptions[propertyKey] =
|
|
1390
|
-
name: propertyKey,
|
|
1391
|
-
...options
|
|
1392
|
-
});
|
|
1435
|
+
subscriptions[propertyKey] = this[propertyKey];
|
|
1393
1436
|
subscriptionsMetadata.set(this, subscriptions);
|
|
1394
1437
|
this[propertyKey] = subscriptions[propertyKey];
|
|
1395
1438
|
});
|
|
@@ -1425,12 +1468,17 @@ export {
|
|
|
1425
1468
|
Subscription,
|
|
1426
1469
|
Subscriptions,
|
|
1427
1470
|
UseMiddleware,
|
|
1471
|
+
createModelResolver,
|
|
1472
|
+
createMutation,
|
|
1473
|
+
createQuery,
|
|
1474
|
+
createResolver2 as createResolver,
|
|
1428
1475
|
getBasicQuery as getBasicResultQuery,
|
|
1429
1476
|
getServiceModelResolvers,
|
|
1430
1477
|
getServiceResolvers,
|
|
1431
1478
|
getServiceSubscriptions,
|
|
1432
1479
|
getTargetMetadata,
|
|
1433
1480
|
getWebsockerViewer,
|
|
1481
|
+
internalResolversMetadata,
|
|
1434
1482
|
resolversSchemas_default as resolversSchemas,
|
|
1435
1483
|
serializeSchema,
|
|
1436
1484
|
setGetWebsockerViewer,
|