@tinkerstack/graphql-annotation 0.0.4 → 0.0.7

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.
@@ -1,28 +1,28 @@
1
- import { Module } from "@nestjs/common";
2
- import { Query, Resolver } from "@nestjs/graphql";
3
- import { InjectableArg, ProtectedResolver } from "../consumer/consumer.example";
4
-
5
- @Resolver()
6
- export class ExampleProducerResolver {
7
- @Query(() => [String])
8
- @ProtectedResolver(["admin"])
9
- testProtectedQuery() {
10
- return ["This resolver should only be available to if guard check passed"];
11
- }
12
- @Query(() => [String])
13
- testQueryWithParam(
14
- @InjectableArg("username", "session:username")
15
- username: string
16
- ) {
17
- return [`Received params: ${username}`];
18
- }
19
- @Query(() => [String])
20
- testPublicQuery() {
21
- return ["This resolver should be available to everyone"];
22
- }
23
- }
24
-
25
- @Module({
26
- providers: [ExampleProducerResolver],
27
- })
28
- export class ExampleProducerModule {}
1
+ import { Module } from "@nestjs/common";
2
+ import { Query, Resolver } from "@nestjs/graphql";
3
+ import { InjectableArg, ProtectedResolver } from "../consumer/consumer.example";
4
+
5
+ @Resolver()
6
+ export class ExampleProducerResolver {
7
+ @Query(() => [String])
8
+ @ProtectedResolver(["admin"])
9
+ testProtectedQuery() {
10
+ return ["This resolver should only be available to if guard check passed"];
11
+ }
12
+ @Query(() => [String])
13
+ testQueryWithParam(
14
+ @InjectableArg("username", "session:username")
15
+ username: string
16
+ ) {
17
+ return [`Received params: ${username}`];
18
+ }
19
+ @Query(() => [String])
20
+ testPublicQuery() {
21
+ return ["This resolver should be available to everyone"];
22
+ }
23
+ }
24
+
25
+ @Module({
26
+ providers: [ExampleProducerResolver],
27
+ })
28
+ export class ExampleProducerModule {}
@@ -1,37 +1,37 @@
1
- export const ANNOTATION_METADATA = "graphql-annotation"; // Should not conflict
2
- export interface MethodInfo {
3
- type: "method";
4
- name: string;
5
- }
6
- export interface ParameterInfo {
7
- type: "parameter";
8
- paramIndex: number;
9
- paramName: string;
10
- }
11
- export type AnnotationTarget = MethodInfo | ParameterInfo;
12
- export interface AnnotationInfo<T = unknown> {
13
- annotation: string;
14
- target: AnnotationTarget;
15
- data: T;
16
- }
17
-
18
- export const ANNOTATION_RESOLVER_METADATA = "graphql-annotation-resolver";
19
- export interface AnnotationResolverMetadata {
20
- annotation: string;
21
- }
22
-
23
- type SourceName = string;
24
- type RemoteURL = string;
25
- export type AnnotationSchemaSources = Record<SourceName, RemoteURL>;
26
-
27
- export type ResolverName = string;
28
- export type GraphQLResolversAnnotations = Record<
29
- ResolverName,
30
- AnnotationInfo[]
31
- >;
32
-
33
- export const GRAPHQL_ANNOTATION_RESOLVER_METADATA =
34
- "graphql-annotation-provider";
35
- export type GraphQLAnnotationResolverMetadata = {
36
- name: string;
37
- };
1
+ export const ANNOTATION_METADATA = "graphql-annotation"; // Should not conflict
2
+ export interface MethodInfo {
3
+ type: "method";
4
+ name: string;
5
+ }
6
+ export interface ParameterInfo {
7
+ type: "parameter";
8
+ paramIndex: number;
9
+ paramName: string;
10
+ }
11
+ export type AnnotationTarget = MethodInfo | ParameterInfo;
12
+ export interface AnnotationInfo<T = unknown> {
13
+ annotation: string;
14
+ target: AnnotationTarget;
15
+ data: T;
16
+ }
17
+
18
+ export const ANNOTATION_RESOLVER_METADATA = "graphql-annotation-resolver";
19
+ export interface AnnotationResolverMetadata {
20
+ annotation: string;
21
+ }
22
+
23
+ type SourceName = string;
24
+ type RemoteURL = string;
25
+ export type AnnotationSchemaSources = Record<SourceName, RemoteURL>;
26
+
27
+ export type ResolverName = string;
28
+ export type GraphQLResolversAnnotations = Record<
29
+ ResolverName,
30
+ AnnotationInfo[]
31
+ >;
32
+
33
+ export const GRAPHQL_ANNOTATION_RESOLVER_METADATA =
34
+ "graphql-annotation-provider";
35
+ export type GraphQLAnnotationResolverMetadata = {
36
+ name: string;
37
+ };
@@ -1,161 +1,161 @@
1
- import { SetMetadata } from "@nestjs/common";
2
- import { Args, ArgsOptions } from "@nestjs/graphql";
3
- import "reflect-metadata";
4
- import {
5
- ANNOTATION_METADATA,
6
- ANNOTATION_RESOLVER_METADATA,
7
- AnnotationInfo,
8
- AnnotationResolverMetadata,
9
- } from "./annotation.constants";
10
-
11
- /**
12
- * Applies an annotation to a Query or Mutation handler or parameter for
13
- * external stitching party to interpret.
14
- *
15
- * For parameter decorator, you must specify `parameter` option for external providers.
16
- *
17
- * @publicApi
18
- */
19
- export function Annotate(
20
- name: string,
21
- opts?: { data?: Record<string, any>; parameter?: undefined },
22
- ): MethodDecorator;
23
- /**
24
- * Applies an annotation to a Query or Mutation handler or parameter for
25
- * external stitching party to interpret.
26
- *
27
- * For parameter decorator, you must specify `parameter` option for external providers.
28
- *
29
- * @publicApi
30
- */
31
- export function Annotate(
32
- name: string,
33
- opts: {
34
- data?: Record<string, any>;
35
- parameter: string;
36
- description?: string;
37
- type?: () => any;
38
- },
39
- ): ParameterDecorator & MethodDecorator;
40
- /**
41
- * Applies an annotation to a Query or Mutation handler or parameter for
42
- * external stitching party to interpret.
43
- *
44
- * For parameter decorator, you must specify `parameter` option for external providers.
45
- *
46
- * @publicApi
47
- */
48
- export function Annotate(
49
- name: string,
50
- opts?: {
51
- data?: Record<string, any>;
52
- parameter?: string;
53
- description?: string;
54
- type?: () => any;
55
- },
56
- ): MethodDecorator & ParameterDecorator {
57
- const annotationName = name;
58
- const annotationData = opts?.data;
59
-
60
- return (
61
- target: Object,
62
- propertyKey: string,
63
- descriptorOrParameterIndex: number | TypedPropertyDescriptor<unknown>,
64
- ) => {
65
- switch (typeof descriptorOrParameterIndex) {
66
- // Method
67
- case "object": {
68
- const descriptor = descriptorOrParameterIndex;
69
- _updateAnnotation(descriptor.value ?? target, (meta) => ({
70
- ...meta,
71
- [annotationName]: {
72
- annotation: annotationName,
73
- data: annotationData,
74
- target: {
75
- type: "method",
76
- name: propertyKey,
77
- },
78
- },
79
- }));
80
- break;
81
- }
82
- // Parameter
83
- case "number": {
84
- const paramIndex = descriptorOrParameterIndex;
85
- const paramName = opts?.parameter;
86
-
87
- if (!paramName)
88
- throw new Error(
89
- `Unable to apply annotation "${annotationName}". Parameter name not specified.`,
90
- );
91
-
92
- Args({
93
- name: paramName,
94
- type: opts?.type,
95
- description: opts?.description,
96
- })(target, propertyKey, paramIndex);
97
- _updateAnnotation(
98
- target.constructor,
99
- (meta) => ({
100
- ...meta,
101
- [`${annotationName}@${paramIndex}`]: {
102
- annotation: annotationName,
103
- data: annotationData,
104
- target: {
105
- type: "parameter",
106
- paramIndex,
107
- paramName,
108
- },
109
- },
110
- }),
111
- propertyKey,
112
- );
113
-
114
- break;
115
- }
116
- default:
117
- throw new Error(
118
- `Unable to attach annotation "${name}". Unsupported target.`,
119
- );
120
- }
121
- };
122
- }
123
- type AnnotationList = Record<string, AnnotationInfo>;
124
- function _updateAnnotation(
125
- target: any,
126
- updateFn: (old: AnnotationList) => AnnotationList,
127
- propertyKey?: string | symbol,
128
- ) {
129
- const meta = Reflect.getMetadata(ANNOTATION_METADATA, target) ?? {};
130
-
131
- if (propertyKey)
132
- Reflect.defineMetadata(
133
- ANNOTATION_METADATA,
134
- updateFn(meta),
135
- target,
136
- propertyKey,
137
- );
138
- else Reflect.defineMetadata(ANNOTATION_METADATA, updateFn(meta), target);
139
- }
140
-
141
- /**
142
- * Declare `Resolver` method for resolving annotations
143
- * @publicApi
144
- */
145
- export function ResolveAnnotation(): MethodDecorator;
146
- /**
147
- * Declare `Resolver` method for resolving annotations
148
- * @publicApi
149
- */
150
- export function ResolveAnnotation(annotation: string): MethodDecorator;
151
- /**
152
- * Declare `Resolver` method for resolving annotations
153
- * @publicApi
154
- */
155
- export function ResolveAnnotation(name?: string): MethodDecorator {
156
- return (target, property, descriptor) => {
157
- SetMetadata(ANNOTATION_RESOLVER_METADATA, {
158
- annotation: name ?? (property as string),
159
- } satisfies AnnotationResolverMetadata)(target, property, descriptor);
160
- };
161
- }
1
+ import { SetMetadata } from "@nestjs/common";
2
+ import { Args, ArgsOptions } from "@nestjs/graphql";
3
+ import "reflect-metadata";
4
+ import {
5
+ ANNOTATION_METADATA,
6
+ ANNOTATION_RESOLVER_METADATA,
7
+ AnnotationInfo,
8
+ AnnotationResolverMetadata,
9
+ } from "./annotation.constants";
10
+
11
+ /**
12
+ * Applies an annotation to a Query or Mutation handler or parameter for
13
+ * external stitching party to interpret.
14
+ *
15
+ * For parameter decorator, you must specify `parameter` option for external providers.
16
+ *
17
+ * @publicApi
18
+ */
19
+ export function Annotate(
20
+ name: string,
21
+ opts?: { data?: Record<string, any>; parameter?: undefined },
22
+ ): MethodDecorator;
23
+ /**
24
+ * Applies an annotation to a Query or Mutation handler or parameter for
25
+ * external stitching party to interpret.
26
+ *
27
+ * For parameter decorator, you must specify `parameter` option for external providers.
28
+ *
29
+ * @publicApi
30
+ */
31
+ export function Annotate(
32
+ name: string,
33
+ opts: {
34
+ data?: Record<string, any>;
35
+ parameter: string;
36
+ description?: string;
37
+ type?: () => any;
38
+ },
39
+ ): ParameterDecorator & MethodDecorator;
40
+ /**
41
+ * Applies an annotation to a Query or Mutation handler or parameter for
42
+ * external stitching party to interpret.
43
+ *
44
+ * For parameter decorator, you must specify `parameter` option for external providers.
45
+ *
46
+ * @publicApi
47
+ */
48
+ export function Annotate(
49
+ name: string,
50
+ opts?: {
51
+ data?: Record<string, any>;
52
+ parameter?: string;
53
+ description?: string;
54
+ type?: () => any;
55
+ },
56
+ ): MethodDecorator & ParameterDecorator {
57
+ const annotationName = name;
58
+ const annotationData = opts?.data;
59
+
60
+ return (
61
+ target: Object,
62
+ propertyKey: string,
63
+ descriptorOrParameterIndex: number | TypedPropertyDescriptor<unknown>,
64
+ ) => {
65
+ switch (typeof descriptorOrParameterIndex) {
66
+ // Method
67
+ case "object": {
68
+ const descriptor = descriptorOrParameterIndex;
69
+ _updateAnnotation(descriptor.value ?? target, (meta) => ({
70
+ ...meta,
71
+ [annotationName]: {
72
+ annotation: annotationName,
73
+ data: annotationData,
74
+ target: {
75
+ type: "method",
76
+ name: propertyKey,
77
+ },
78
+ },
79
+ }));
80
+ break;
81
+ }
82
+ // Parameter
83
+ case "number": {
84
+ const paramIndex = descriptorOrParameterIndex;
85
+ const paramName = opts?.parameter;
86
+
87
+ if (!paramName)
88
+ throw new Error(
89
+ `Unable to apply annotation "${annotationName}". Parameter name not specified.`,
90
+ );
91
+
92
+ Args({
93
+ name: paramName,
94
+ type: opts?.type,
95
+ description: opts?.description,
96
+ })(target, propertyKey, paramIndex);
97
+ _updateAnnotation(
98
+ target.constructor,
99
+ (meta) => ({
100
+ ...meta,
101
+ [`${annotationName}@${paramIndex}`]: {
102
+ annotation: annotationName,
103
+ data: annotationData,
104
+ target: {
105
+ type: "parameter",
106
+ paramIndex,
107
+ paramName,
108
+ },
109
+ },
110
+ }),
111
+ propertyKey,
112
+ );
113
+
114
+ break;
115
+ }
116
+ default:
117
+ throw new Error(
118
+ `Unable to attach annotation "${name}". Unsupported target.`,
119
+ );
120
+ }
121
+ };
122
+ }
123
+ type AnnotationList = Record<string, AnnotationInfo>;
124
+ function _updateAnnotation(
125
+ target: any,
126
+ updateFn: (old: AnnotationList) => AnnotationList,
127
+ propertyKey?: string | symbol,
128
+ ) {
129
+ const meta = Reflect.getMetadata(ANNOTATION_METADATA, target) ?? {};
130
+
131
+ if (propertyKey)
132
+ Reflect.defineMetadata(
133
+ ANNOTATION_METADATA,
134
+ updateFn(meta),
135
+ target,
136
+ propertyKey,
137
+ );
138
+ else Reflect.defineMetadata(ANNOTATION_METADATA, updateFn(meta), target);
139
+ }
140
+
141
+ /**
142
+ * Declare `Resolver` method for resolving annotations
143
+ * @publicApi
144
+ */
145
+ export function ResolveAnnotation(): MethodDecorator;
146
+ /**
147
+ * Declare `Resolver` method for resolving annotations
148
+ * @publicApi
149
+ */
150
+ export function ResolveAnnotation(annotation: string): MethodDecorator;
151
+ /**
152
+ * Declare `Resolver` method for resolving annotations
153
+ * @publicApi
154
+ */
155
+ export function ResolveAnnotation(name?: string): MethodDecorator {
156
+ return (target, property, descriptor) => {
157
+ SetMetadata(ANNOTATION_RESOLVER_METADATA, {
158
+ annotation: name ?? (property as string),
159
+ } satisfies AnnotationResolverMetadata)(target, property, descriptor);
160
+ };
161
+ }