@hazeljs/graphql 0.2.0-alpha.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.
Files changed (63) hide show
  1. package/LICENSE +192 -0
  2. package/README.md +124 -0
  3. package/dist/client/decorators.d.ts +44 -0
  4. package/dist/client/decorators.d.ts.map +1 -0
  5. package/dist/client/decorators.js +72 -0
  6. package/dist/client/decorators.test.d.ts +2 -0
  7. package/dist/client/decorators.test.d.ts.map +1 -0
  8. package/dist/client/decorators.test.js +102 -0
  9. package/dist/client/graphql.client.d.ts +25 -0
  10. package/dist/client/graphql.client.d.ts.map +1 -0
  11. package/dist/client/graphql.client.js +47 -0
  12. package/dist/client/graphql.client.test.d.ts +2 -0
  13. package/dist/client/graphql.client.test.d.ts.map +1 -0
  14. package/dist/client/graphql.client.test.js +79 -0
  15. package/dist/decorators/field.decorator.d.ts +23 -0
  16. package/dist/decorators/field.decorator.d.ts.map +1 -0
  17. package/dist/decorators/field.decorator.js +37 -0
  18. package/dist/decorators/field.decorator.test.d.ts +2 -0
  19. package/dist/decorators/field.decorator.test.d.ts.map +1 -0
  20. package/dist/decorators/field.decorator.test.js +67 -0
  21. package/dist/decorators/object-type.decorator.d.ts +21 -0
  22. package/dist/decorators/object-type.decorator.d.ts.map +1 -0
  23. package/dist/decorators/object-type.decorator.js +34 -0
  24. package/dist/decorators/object-type.decorator.test.d.ts +2 -0
  25. package/dist/decorators/object-type.decorator.test.d.ts.map +1 -0
  26. package/dist/decorators/object-type.decorator.test.js +45 -0
  27. package/dist/decorators/query-mutation.decorator.d.ts +25 -0
  28. package/dist/decorators/query-mutation.decorator.d.ts.map +1 -0
  29. package/dist/decorators/query-mutation.decorator.js +62 -0
  30. package/dist/decorators/query-mutation.decorator.test.d.ts +2 -0
  31. package/dist/decorators/query-mutation.decorator.test.d.ts.map +1 -0
  32. package/dist/decorators/query-mutation.decorator.test.js +176 -0
  33. package/dist/decorators/resolver.decorator.d.ts +25 -0
  34. package/dist/decorators/resolver.decorator.d.ts.map +1 -0
  35. package/dist/decorators/resolver.decorator.js +35 -0
  36. package/dist/decorators/resolver.decorator.test.d.ts +2 -0
  37. package/dist/decorators/resolver.decorator.test.d.ts.map +1 -0
  38. package/dist/decorators/resolver.decorator.test.js +36 -0
  39. package/dist/graphql.module.d.ts +31 -0
  40. package/dist/graphql.module.d.ts.map +1 -0
  41. package/dist/graphql.module.js +63 -0
  42. package/dist/graphql.module.test.d.ts +2 -0
  43. package/dist/graphql.module.test.d.ts.map +1 -0
  44. package/dist/graphql.module.test.js +85 -0
  45. package/dist/graphql.server.d.ts +34 -0
  46. package/dist/graphql.server.d.ts.map +1 -0
  47. package/dist/graphql.server.js +74 -0
  48. package/dist/graphql.server.test.d.ts +2 -0
  49. package/dist/graphql.server.test.d.ts.map +1 -0
  50. package/dist/graphql.server.test.js +79 -0
  51. package/dist/graphql.types.d.ts +36 -0
  52. package/dist/graphql.types.d.ts.map +1 -0
  53. package/dist/graphql.types.js +5 -0
  54. package/dist/index.d.ts +16 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +39 -0
  57. package/dist/schema-builder.d.ts +9 -0
  58. package/dist/schema-builder.d.ts.map +1 -0
  59. package/dist/schema-builder.js +142 -0
  60. package/dist/schema-builder.test.d.ts +2 -0
  61. package/dist/schema-builder.test.d.ts.map +1 -0
  62. package/dist/schema-builder.test.js +182 -0
  63. package/package.json +56 -0
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const graphql_client_1 = require("./graphql.client");
4
+ describe('GraphQLClient', () => {
5
+ const mockFetch = jest.fn();
6
+ beforeEach(() => {
7
+ global.fetch = mockFetch;
8
+ mockFetch.mockReset();
9
+ });
10
+ it('should execute query', async () => {
11
+ mockFetch.mockResolvedValue({
12
+ ok: true,
13
+ json: async () => ({ data: { hello: 'world' } }),
14
+ });
15
+ const client = new graphql_client_1.GraphQLClient({ url: 'http://localhost/graphql' });
16
+ const result = await client.query('{ hello }');
17
+ expect(result).toEqual({ hello: 'world' });
18
+ expect(mockFetch).toHaveBeenCalledWith('http://localhost/graphql', expect.objectContaining({
19
+ method: 'POST',
20
+ headers: expect.objectContaining({ 'Content-Type': 'application/json' }),
21
+ body: JSON.stringify({ query: '{ hello }', variables: undefined }),
22
+ }));
23
+ });
24
+ it('should execute query with variables', async () => {
25
+ mockFetch.mockResolvedValue({
26
+ ok: true,
27
+ json: async () => ({ data: { user: { id: '1' } } }),
28
+ });
29
+ const client = new graphql_client_1.GraphQLClient({ url: 'http://localhost/graphql' });
30
+ await client.query('query($id: ID!) { user(id: $id) { id } }', { id: '1' });
31
+ expect(mockFetch).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
32
+ body: JSON.stringify({
33
+ query: 'query($id: ID!) { user(id: $id) { id } }',
34
+ variables: { id: '1' },
35
+ }),
36
+ }));
37
+ });
38
+ it('should execute mutation', async () => {
39
+ mockFetch.mockResolvedValue({
40
+ ok: true,
41
+ json: async () => ({ data: { createUser: { id: '1' } } }),
42
+ });
43
+ const client = new graphql_client_1.GraphQLClient({ url: 'http://localhost/graphql' });
44
+ const result = await client.mutate('mutation { createUser(name: "x") { id } }');
45
+ expect(result).toEqual({ createUser: { id: '1' } });
46
+ });
47
+ it('should pass custom headers', async () => {
48
+ mockFetch.mockResolvedValue({
49
+ ok: true,
50
+ json: async () => ({ data: {} }),
51
+ });
52
+ const client = new graphql_client_1.GraphQLClient({
53
+ url: 'http://localhost/graphql',
54
+ headers: { Authorization: 'Bearer token' },
55
+ });
56
+ await client.query('{ x }');
57
+ expect(mockFetch).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
58
+ headers: expect.objectContaining({
59
+ 'Content-Type': 'application/json',
60
+ Authorization: 'Bearer token',
61
+ }),
62
+ }));
63
+ });
64
+ it('should throw on HTTP error', async () => {
65
+ mockFetch.mockResolvedValue({ ok: false, status: 500, statusText: 'Internal Error' });
66
+ const client = new graphql_client_1.GraphQLClient({ url: 'http://localhost/graphql' });
67
+ await expect(client.query('{ x }')).rejects.toThrow('GraphQL request failed: 500 Internal Error');
68
+ });
69
+ it('should throw on GraphQL errors', async () => {
70
+ mockFetch.mockResolvedValue({
71
+ ok: true,
72
+ json: async () => ({
73
+ errors: [{ message: 'Invalid query' }, { message: 'Second error' }],
74
+ }),
75
+ });
76
+ const client = new graphql_client_1.GraphQLClient({ url: 'http://localhost/graphql' });
77
+ await expect(client.query('{ bad }')).rejects.toThrow('Invalid query; Second error');
78
+ });
79
+ });
@@ -0,0 +1,23 @@
1
+ import 'reflect-metadata';
2
+ import type { FieldMetadata } from '../graphql.types';
3
+ export declare const FIELD_METADATA_KEY: unique symbol;
4
+ /**
5
+ * Marks a property or method as a GraphQL field
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * @ObjectType()
10
+ * class User {
11
+ * @Field()
12
+ * id: string;
13
+ *
14
+ * @Field()
15
+ * fullName(): string {
16
+ * return `${this.firstName} ${this.lastName}`;
17
+ * }
18
+ * }
19
+ * ```
20
+ */
21
+ export declare function Field(nameOrOptions?: string | Partial<FieldMetadata>): PropertyDecorator & MethodDecorator;
22
+ export declare function getFieldMetadata(target: object): FieldMetadata[];
23
+ //# sourceMappingURL=field.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"field.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/field.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEtD,eAAO,MAAM,kBAAkB,eAA0B,CAAC;AAE1D;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,KAAK,CACnB,aAAa,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,GAC9C,iBAAiB,GAAG,eAAe,CAUrC;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,aAAa,EAAE,CAEhE"}
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FIELD_METADATA_KEY = void 0;
4
+ exports.Field = Field;
5
+ exports.getFieldMetadata = getFieldMetadata;
6
+ require("reflect-metadata");
7
+ exports.FIELD_METADATA_KEY = Symbol('graphql:field');
8
+ /**
9
+ * Marks a property or method as a GraphQL field
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * @ObjectType()
14
+ * class User {
15
+ * @Field()
16
+ * id: string;
17
+ *
18
+ * @Field()
19
+ * fullName(): string {
20
+ * return `${this.firstName} ${this.lastName}`;
21
+ * }
22
+ * }
23
+ * ```
24
+ */
25
+ function Field(nameOrOptions) {
26
+ return (target, propertyKey, _descriptor) => {
27
+ const meta = typeof nameOrOptions === 'string'
28
+ ? { name: nameOrOptions, type: undefined }
29
+ : { name: String(propertyKey), type: undefined, ...nameOrOptions };
30
+ const existing = Reflect.getMetadata(exports.FIELD_METADATA_KEY, target) || [];
31
+ existing.push({ ...meta, name: meta.name || String(propertyKey) });
32
+ Reflect.defineMetadata(exports.FIELD_METADATA_KEY, existing, target);
33
+ };
34
+ }
35
+ function getFieldMetadata(target) {
36
+ return Reflect.getMetadata(exports.FIELD_METADATA_KEY, target) || [];
37
+ }
@@ -0,0 +1,2 @@
1
+ import 'reflect-metadata';
2
+ //# sourceMappingURL=field.decorator.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"field.decorator.test.d.ts","sourceRoot":"","sources":["../../src/decorators/field.decorator.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ require("reflect-metadata");
13
+ const field_decorator_1 = require("./field.decorator");
14
+ describe('Field decorator', () => {
15
+ it('should add metadata for property with default name', () => {
16
+ class User {
17
+ }
18
+ __decorate([
19
+ (0, field_decorator_1.Field)(),
20
+ __metadata("design:type", String)
21
+ ], User.prototype, "id", void 0);
22
+ const meta = (0, field_decorator_1.getFieldMetadata)(User.prototype);
23
+ expect(meta).toHaveLength(1);
24
+ expect(meta[0].name).toBe('id');
25
+ });
26
+ it('should add metadata with custom name as string', () => {
27
+ class User {
28
+ }
29
+ __decorate([
30
+ (0, field_decorator_1.Field)('userId'),
31
+ __metadata("design:type", String)
32
+ ], User.prototype, "id", void 0);
33
+ const meta = (0, field_decorator_1.getFieldMetadata)(User.prototype);
34
+ expect(meta[0].name).toBe('userId');
35
+ });
36
+ it('should add metadata with options object', () => {
37
+ class User {
38
+ }
39
+ __decorate([
40
+ (0, field_decorator_1.Field)({ name: 'fullName', description: 'Full name' }),
41
+ __metadata("design:type", String)
42
+ ], User.prototype, "name", void 0);
43
+ const meta = (0, field_decorator_1.getFieldMetadata)(User.prototype);
44
+ expect(meta[0]).toMatchObject({ name: 'fullName', description: 'Full name' });
45
+ });
46
+ it('should add metadata for multiple fields', () => {
47
+ class User {
48
+ }
49
+ __decorate([
50
+ (0, field_decorator_1.Field)(),
51
+ __metadata("design:type", String)
52
+ ], User.prototype, "id", void 0);
53
+ __decorate([
54
+ (0, field_decorator_1.Field)(),
55
+ __metadata("design:type", String)
56
+ ], User.prototype, "name", void 0);
57
+ const meta = (0, field_decorator_1.getFieldMetadata)(User.prototype);
58
+ expect(meta).toHaveLength(2);
59
+ expect(meta.map((m) => m.name)).toEqual(['id', 'name']);
60
+ });
61
+ it('should return empty array for class without @Field', () => {
62
+ class Plain {
63
+ }
64
+ const meta = (0, field_decorator_1.getFieldMetadata)(Plain.prototype);
65
+ expect(meta).toEqual([]);
66
+ });
67
+ });
@@ -0,0 +1,21 @@
1
+ import 'reflect-metadata';
2
+ import type { ObjectTypeMetadata } from '../graphql.types';
3
+ export declare const OBJECT_TYPE_METADATA_KEY: unique symbol;
4
+ /**
5
+ * Marks a class as a GraphQL object type
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * @ObjectType()
10
+ * class User {
11
+ * @Field()
12
+ * id: string;
13
+ *
14
+ * @Field()
15
+ * name: string;
16
+ * }
17
+ * ```
18
+ */
19
+ export declare function ObjectType(nameOrOptions?: string | ObjectTypeMetadata): ClassDecorator;
20
+ export declare function getObjectTypeMetadata(target: object): ObjectTypeMetadata | undefined;
21
+ //# sourceMappingURL=object-type.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object-type.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/object-type.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAE3D,eAAO,MAAM,wBAAwB,eAAgC,CAAC;AAEtE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,kBAAkB,GAAG,cAAc,CAQtF;AAED,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,kBAAkB,GAAG,SAAS,CAIpF"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OBJECT_TYPE_METADATA_KEY = void 0;
4
+ exports.ObjectType = ObjectType;
5
+ exports.getObjectTypeMetadata = getObjectTypeMetadata;
6
+ require("reflect-metadata");
7
+ exports.OBJECT_TYPE_METADATA_KEY = Symbol('graphql:object-type');
8
+ /**
9
+ * Marks a class as a GraphQL object type
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * @ObjectType()
14
+ * class User {
15
+ * @Field()
16
+ * id: string;
17
+ *
18
+ * @Field()
19
+ * name: string;
20
+ * }
21
+ * ```
22
+ */
23
+ function ObjectType(nameOrOptions) {
24
+ return (target) => {
25
+ const meta = typeof nameOrOptions === 'string'
26
+ ? { name: nameOrOptions }
27
+ : (nameOrOptions ?? { name: target.name });
28
+ Reflect.defineMetadata(exports.OBJECT_TYPE_METADATA_KEY, meta, target);
29
+ };
30
+ }
31
+ function getObjectTypeMetadata(target) {
32
+ const ctor = typeof target === 'function' ? target : target.constructor;
33
+ return Reflect.getMetadata(exports.OBJECT_TYPE_METADATA_KEY, ctor ?? target);
34
+ }
@@ -0,0 +1,2 @@
1
+ import 'reflect-metadata';
2
+ //# sourceMappingURL=object-type.decorator.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object-type.decorator.test.d.ts","sourceRoot":"","sources":["../../src/decorators/object-type.decorator.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ require("reflect-metadata");
10
+ const object_type_decorator_1 = require("./object-type.decorator");
11
+ describe('ObjectType decorator', () => {
12
+ it('should add metadata with class name when no options', () => {
13
+ let User = class User {
14
+ };
15
+ User = __decorate([
16
+ (0, object_type_decorator_1.ObjectType)()
17
+ ], User);
18
+ const meta = (0, object_type_decorator_1.getObjectTypeMetadata)(User);
19
+ expect(meta).toEqual({ name: 'User' });
20
+ });
21
+ it('should add metadata with custom name as string', () => {
22
+ let User = class User {
23
+ };
24
+ User = __decorate([
25
+ (0, object_type_decorator_1.ObjectType)('CustomUser')
26
+ ], User);
27
+ const meta = (0, object_type_decorator_1.getObjectTypeMetadata)(User);
28
+ expect(meta).toEqual({ name: 'CustomUser' });
29
+ });
30
+ it('should add metadata with options object', () => {
31
+ let Person = class Person {
32
+ };
33
+ Person = __decorate([
34
+ (0, object_type_decorator_1.ObjectType)({ name: 'Person', description: 'A person type' })
35
+ ], Person);
36
+ const meta = (0, object_type_decorator_1.getObjectTypeMetadata)(Person);
37
+ expect(meta).toEqual({ name: 'Person', description: 'A person type' });
38
+ });
39
+ it('should return undefined for class without decorator', () => {
40
+ class Plain {
41
+ }
42
+ const meta = (0, object_type_decorator_1.getObjectTypeMetadata)(Plain.prototype);
43
+ expect(meta).toBeUndefined();
44
+ });
45
+ });
@@ -0,0 +1,25 @@
1
+ import 'reflect-metadata';
2
+ import type { QueryMetadata, MutationMetadata, ArgMetadata } from '../graphql.types';
3
+ export declare const QUERY_METADATA_KEY: unique symbol;
4
+ export declare const MUTATION_METADATA_KEY: unique symbol;
5
+ export declare const ARG_METADATA_KEY: unique symbol;
6
+ /**
7
+ * Marks a method as a GraphQL Query
8
+ */
9
+ export declare function Query(nameOrOptions?: string | Partial<QueryMetadata>): MethodDecorator;
10
+ /**
11
+ * Marks a method as a GraphQL Mutation
12
+ */
13
+ export declare function Mutation(nameOrOptions?: string | Partial<MutationMetadata>): MethodDecorator;
14
+ /**
15
+ * Marks a parameter as a GraphQL argument
16
+ */
17
+ export declare function Arg(nameOrOptions: string | Partial<ArgMetadata>, type?: unknown): ParameterDecorator;
18
+ export declare function getQueryMetadata(target: object): Array<QueryMetadata & {
19
+ method: string;
20
+ }>;
21
+ export declare function getMutationMetadata(target: object): Array<MutationMetadata & {
22
+ method: string;
23
+ }>;
24
+ export declare function getArgMetadata(target: object, propertyKey: string | symbol): ArgMetadata[];
25
+ //# sourceMappingURL=query-mutation.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query-mutation.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/query-mutation.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAErF,eAAO,MAAM,kBAAkB,eAA0B,CAAC;AAC1D,eAAO,MAAM,qBAAqB,eAA6B,CAAC;AAChE,eAAO,MAAM,gBAAgB,eAAwB,CAAC;AAEtD;;GAEG;AACH,wBAAgB,KAAK,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,GAAG,eAAe,CAUtF;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,GAAG,eAAe,CAU5F;AAED;;GAEG;AACH,wBAAgB,GAAG,CACjB,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,EAC5C,IAAI,CAAC,EAAE,OAAO,GACb,kBAAkB,CAWpB;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC,aAAa,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAE1F;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC,gBAAgB,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAEhG;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,WAAW,EAAE,CAE1F"}
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ARG_METADATA_KEY = exports.MUTATION_METADATA_KEY = exports.QUERY_METADATA_KEY = void 0;
4
+ exports.Query = Query;
5
+ exports.Mutation = Mutation;
6
+ exports.Arg = Arg;
7
+ exports.getQueryMetadata = getQueryMetadata;
8
+ exports.getMutationMetadata = getMutationMetadata;
9
+ exports.getArgMetadata = getArgMetadata;
10
+ require("reflect-metadata");
11
+ exports.QUERY_METADATA_KEY = Symbol('graphql:query');
12
+ exports.MUTATION_METADATA_KEY = Symbol('graphql:mutation');
13
+ exports.ARG_METADATA_KEY = Symbol('graphql:arg');
14
+ /**
15
+ * Marks a method as a GraphQL Query
16
+ */
17
+ function Query(nameOrOptions) {
18
+ return (target, propertyKey, _descriptor) => {
19
+ const meta = typeof nameOrOptions === 'string'
20
+ ? { name: nameOrOptions }
21
+ : { name: String(propertyKey), ...nameOrOptions };
22
+ const existing = Reflect.getMetadata(exports.QUERY_METADATA_KEY, target.constructor) || [];
23
+ existing.push({ ...meta, name: meta.name || String(propertyKey), method: String(propertyKey) });
24
+ Reflect.defineMetadata(exports.QUERY_METADATA_KEY, existing, target.constructor);
25
+ };
26
+ }
27
+ /**
28
+ * Marks a method as a GraphQL Mutation
29
+ */
30
+ function Mutation(nameOrOptions) {
31
+ return (target, propertyKey, _descriptor) => {
32
+ const meta = typeof nameOrOptions === 'string'
33
+ ? { name: nameOrOptions }
34
+ : { name: String(propertyKey), ...nameOrOptions };
35
+ const existing = Reflect.getMetadata(exports.MUTATION_METADATA_KEY, target.constructor) || [];
36
+ existing.push({ ...meta, name: meta.name || String(propertyKey), method: String(propertyKey) });
37
+ Reflect.defineMetadata(exports.MUTATION_METADATA_KEY, existing, target.constructor);
38
+ };
39
+ }
40
+ /**
41
+ * Marks a parameter as a GraphQL argument
42
+ */
43
+ function Arg(nameOrOptions, type) {
44
+ return (target, propertyKey, parameterIndex) => {
45
+ const meta = typeof nameOrOptions === 'string'
46
+ ? { name: nameOrOptions, type }
47
+ : { name: nameOrOptions.name, type: nameOrOptions.type, ...nameOrOptions };
48
+ const key = propertyKey ?? '';
49
+ const existing = Reflect.getMetadata(exports.ARG_METADATA_KEY, target, key) || [];
50
+ existing[parameterIndex] = meta;
51
+ Reflect.defineMetadata(exports.ARG_METADATA_KEY, existing, target, key);
52
+ };
53
+ }
54
+ function getQueryMetadata(target) {
55
+ return Reflect.getMetadata(exports.QUERY_METADATA_KEY, target) || [];
56
+ }
57
+ function getMutationMetadata(target) {
58
+ return Reflect.getMetadata(exports.MUTATION_METADATA_KEY, target) || [];
59
+ }
60
+ function getArgMetadata(target, propertyKey) {
61
+ return Reflect.getMetadata(exports.ARG_METADATA_KEY, target, propertyKey) || [];
62
+ }
@@ -0,0 +1,2 @@
1
+ import 'reflect-metadata';
2
+ //# sourceMappingURL=query-mutation.decorator.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query-mutation.decorator.test.d.ts","sourceRoot":"","sources":["../../src/decorators/query-mutation.decorator.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ require("reflect-metadata");
16
+ const query_mutation_decorator_1 = require("./query-mutation.decorator");
17
+ describe('Query decorator', () => {
18
+ it('should add metadata with method name as default', () => {
19
+ class TestResolver {
20
+ hello() {
21
+ return 'hi';
22
+ }
23
+ }
24
+ __decorate([
25
+ (0, query_mutation_decorator_1.Query)(),
26
+ __metadata("design:type", Function),
27
+ __metadata("design:paramtypes", []),
28
+ __metadata("design:returntype", void 0)
29
+ ], TestResolver.prototype, "hello", null);
30
+ const meta = (0, query_mutation_decorator_1.getQueryMetadata)(TestResolver);
31
+ expect(meta).toHaveLength(1);
32
+ expect(meta[0].name).toBe('hello');
33
+ expect(meta[0].method).toBe('hello');
34
+ });
35
+ it('should add metadata with custom name', () => {
36
+ class TestResolver {
37
+ hello() {
38
+ return 'hi';
39
+ }
40
+ }
41
+ __decorate([
42
+ (0, query_mutation_decorator_1.Query)('getHello'),
43
+ __metadata("design:type", Function),
44
+ __metadata("design:paramtypes", []),
45
+ __metadata("design:returntype", void 0)
46
+ ], TestResolver.prototype, "hello", null);
47
+ const meta = (0, query_mutation_decorator_1.getQueryMetadata)(TestResolver);
48
+ expect(meta[0].name).toBe('getHello');
49
+ expect(meta[0].method).toBe('hello');
50
+ });
51
+ it('should support multiple queries', () => {
52
+ class TestResolver {
53
+ one() { }
54
+ two() { }
55
+ }
56
+ __decorate([
57
+ (0, query_mutation_decorator_1.Query)(),
58
+ __metadata("design:type", Function),
59
+ __metadata("design:paramtypes", []),
60
+ __metadata("design:returntype", void 0)
61
+ ], TestResolver.prototype, "one", null);
62
+ __decorate([
63
+ (0, query_mutation_decorator_1.Query)(),
64
+ __metadata("design:type", Function),
65
+ __metadata("design:paramtypes", []),
66
+ __metadata("design:returntype", void 0)
67
+ ], TestResolver.prototype, "two", null);
68
+ const meta = (0, query_mutation_decorator_1.getQueryMetadata)(TestResolver);
69
+ expect(meta).toHaveLength(2);
70
+ expect(meta.map((m) => m.name)).toEqual(['one', 'two']);
71
+ });
72
+ });
73
+ describe('Mutation decorator', () => {
74
+ it('should add metadata with method name as default', () => {
75
+ class TestResolver {
76
+ createUser() {
77
+ return { id: '1' };
78
+ }
79
+ }
80
+ __decorate([
81
+ (0, query_mutation_decorator_1.Mutation)(),
82
+ __metadata("design:type", Function),
83
+ __metadata("design:paramtypes", []),
84
+ __metadata("design:returntype", void 0)
85
+ ], TestResolver.prototype, "createUser", null);
86
+ const meta = (0, query_mutation_decorator_1.getMutationMetadata)(TestResolver);
87
+ expect(meta).toHaveLength(1);
88
+ expect(meta[0].name).toBe('createUser');
89
+ expect(meta[0].method).toBe('createUser');
90
+ });
91
+ it('should add metadata with custom name', () => {
92
+ class TestResolver {
93
+ createUser() {
94
+ return {};
95
+ }
96
+ }
97
+ __decorate([
98
+ (0, query_mutation_decorator_1.Mutation)('addUser'),
99
+ __metadata("design:type", Function),
100
+ __metadata("design:paramtypes", []),
101
+ __metadata("design:returntype", void 0)
102
+ ], TestResolver.prototype, "createUser", null);
103
+ const meta = (0, query_mutation_decorator_1.getMutationMetadata)(TestResolver);
104
+ expect(meta[0].name).toBe('addUser');
105
+ });
106
+ });
107
+ describe('Arg decorator', () => {
108
+ it('should add metadata for parameter with string name', () => {
109
+ class TestResolver {
110
+ user(_id) {
111
+ return {};
112
+ }
113
+ }
114
+ __decorate([
115
+ (0, query_mutation_decorator_1.Query)(),
116
+ __param(0, (0, query_mutation_decorator_1.Arg)('id')),
117
+ __metadata("design:type", Function),
118
+ __metadata("design:paramtypes", [String]),
119
+ __metadata("design:returntype", void 0)
120
+ ], TestResolver.prototype, "user", null);
121
+ const meta = (0, query_mutation_decorator_1.getArgMetadata)(TestResolver.prototype, 'user');
122
+ expect(meta).toHaveLength(1);
123
+ expect(meta[0]?.name).toBe('id');
124
+ });
125
+ it('should add metadata with type', () => {
126
+ class TestResolver {
127
+ item(_id) {
128
+ return {};
129
+ }
130
+ }
131
+ __decorate([
132
+ (0, query_mutation_decorator_1.Query)(),
133
+ __param(0, (0, query_mutation_decorator_1.Arg)('id', String)),
134
+ __metadata("design:type", Function),
135
+ __metadata("design:paramtypes", [String]),
136
+ __metadata("design:returntype", void 0)
137
+ ], TestResolver.prototype, "item", null);
138
+ const meta = (0, query_mutation_decorator_1.getArgMetadata)(TestResolver.prototype, 'item');
139
+ expect(meta[0]?.name).toBe('id');
140
+ expect(meta[0]?.type).toBe(String);
141
+ });
142
+ it('should add metadata for multiple args in order', () => {
143
+ class TestResolver {
144
+ create(_a, _b) {
145
+ return {};
146
+ }
147
+ }
148
+ __decorate([
149
+ (0, query_mutation_decorator_1.Mutation)(),
150
+ __param(0, (0, query_mutation_decorator_1.Arg)('a')),
151
+ __param(1, (0, query_mutation_decorator_1.Arg)('b')),
152
+ __metadata("design:type", Function),
153
+ __metadata("design:paramtypes", [String, Number]),
154
+ __metadata("design:returntype", void 0)
155
+ ], TestResolver.prototype, "create", null);
156
+ const meta = (0, query_mutation_decorator_1.getArgMetadata)(TestResolver.prototype, 'create');
157
+ expect(meta).toHaveLength(2);
158
+ expect(meta[0]?.name).toBe('a');
159
+ expect(meta[1]?.name).toBe('b');
160
+ });
161
+ it('should return empty array when no args', () => {
162
+ class TestResolver {
163
+ hello() {
164
+ return 'hi';
165
+ }
166
+ }
167
+ __decorate([
168
+ (0, query_mutation_decorator_1.Query)(),
169
+ __metadata("design:type", Function),
170
+ __metadata("design:paramtypes", []),
171
+ __metadata("design:returntype", void 0)
172
+ ], TestResolver.prototype, "hello", null);
173
+ const meta = (0, query_mutation_decorator_1.getArgMetadata)(TestResolver.prototype, 'hello');
174
+ expect(meta).toEqual([]);
175
+ });
176
+ });
@@ -0,0 +1,25 @@
1
+ import 'reflect-metadata';
2
+ import type { ResolverMetadata } from '../graphql.types';
3
+ export declare const RESOLVER_METADATA_KEY: unique symbol;
4
+ /**
5
+ * Marks a class as a GraphQL resolver (contains @Query and @Mutation handlers)
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * @Resolver()
10
+ * class UserResolver {
11
+ * @Query()
12
+ * user(@Arg('id') id: string) {
13
+ * return { id, name: 'John' };
14
+ * }
15
+ *
16
+ * @Mutation()
17
+ * createUser(@Arg('name') name: string) {
18
+ * return { id: '1', name };
19
+ * }
20
+ * }
21
+ * ```
22
+ */
23
+ export declare function Resolver(name?: string): ClassDecorator;
24
+ export declare function getResolverMetadata(target: object): ResolverMetadata | undefined;
25
+ //# sourceMappingURL=resolver.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolver.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/resolver.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEzD,eAAO,MAAM,qBAAqB,eAA6B,CAAC;AAEhE;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,cAAc,CAKtD;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS,CAEhF"}