Skip to content

Commit 5911eba

Browse files
jbl428imdudu1
andcommitted
feat: add graphql exchange decorator
Co-authored-by: imdudu1 <[email protected]>
1 parent 7183653 commit 5911eba

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed

lib/builders/http-request.builder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
REQUEST_HEADER_METADATA,
1111
REQUEST_PARAM_METADATA,
1212
} from '../decorators';
13-
import { type HttpMethod } from '../types/http-method';
13+
import { type HttpMethod } from '../types';
1414

1515
export class HttpRequestBuilder {
1616
private baseUrl = '';
@@ -24,6 +24,7 @@ export class HttpRequestBuilder {
2424
readonly propertyKey: string,
2525
readonly method: HttpMethod,
2626
readonly url: string,
27+
readonly gqlQuery?: string,
2728
) {
2829
this.pathVariableBuilder = this.getMetadata(PATH_VARIABLE_METADATA);
2930
this.requestParamBuilder = this.getMetadata(REQUEST_PARAM_METADATA);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, test, expect } from 'vitest';
2+
import { HTTP_EXCHANGE_METADATA } from './constants';
3+
import { GraphQLExchange } from './graphql-exchange.decorator';
4+
import { type HttpRequestBuilder } from '../builders/http-request.builder';
5+
6+
describe('GraphQLExchange', () => {
7+
test('should set graphql exchange metadata', () => {
8+
// given
9+
class TestService {
10+
@GraphQLExchange(`query { hello }`, '/api/graphql')
11+
async request(): Promise<string> {
12+
return '';
13+
}
14+
}
15+
16+
// when
17+
const result: HttpRequestBuilder = Reflect.getMetadata(
18+
HTTP_EXCHANGE_METADATA,
19+
TestService.prototype,
20+
'request',
21+
);
22+
23+
// then
24+
expect(result.target).toBe(TestService.prototype);
25+
expect(result.propertyKey).toBe('request');
26+
expect(result.method).toBe('POST');
27+
expect(result.url).toBe('/api/graphql');
28+
expect(result.gqlQuery).toBe(`query { hello }`);
29+
});
30+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { HTTP_EXCHANGE_METADATA } from './constants';
2+
import { HttpRequestBuilder } from '../builders/http-request.builder';
3+
import { type AsyncFunction } from '../types';
4+
5+
export function GraphQLExchange(query: string, url = '/graphql') {
6+
return <P extends string>(
7+
target: Record<P, AsyncFunction>,
8+
propertyKey: P,
9+
) => {
10+
Reflect.defineMetadata(
11+
HTTP_EXCHANGE_METADATA,
12+
new HttpRequestBuilder(target, propertyKey, 'POST', url, query),
13+
target,
14+
propertyKey,
15+
);
16+
};
17+
}

lib/decorators/http-exchange.decorator.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { HTTP_EXCHANGE_METADATA } from './constants';
22
import { HttpRequestBuilder } from '../builders/http-request.builder';
3-
import { type HttpMethod } from '../types';
4-
5-
type AsyncFunction = (...args: any[]) => Promise<unknown>;
3+
import { type AsyncFunction, type HttpMethod } from '../types';
64

75
export const HttpExchange =
86
(method: HttpMethod, url: string) =>

lib/types/async-function.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type AsyncFunction = (...args: any[]) => Promise<unknown>;

lib/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from './http-client.interface';
22
export * from './http-method';
3+
export * from './param-decorator-option.interface';
4+
export * from './async-function';

0 commit comments

Comments
 (0)