Skip to content

Commit 2779640

Browse files
jbl428imdudu1
andcommitted
feat: handle graphql variable
Co-authored-by: imdudu1 <[email protected]>
1 parent 5911eba commit 2779640

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

lib/builders/http-request.builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class HttpRequestBuilder {
4747
this.pathVariableBuilder,
4848
this.requestParamBuilder,
4949
);
50-
const payload = this.payloadBuilder.build(args);
50+
const payload = this.payloadBuilder.build(args, this.gqlQuery);
5151
const headers = this.requestHeaderBuilder?.build(args);
5252

5353
return new Request(urlBuilder.build(), {

lib/builders/payload.builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export class PayloadBuilder {
1919
return undefined;
2020
}
2121

22-
build(args: any[]): BodyInit | undefined {
22+
build(args: any[], gqlQuery?: string): BodyInit | undefined {
2323
return (
24-
this.requestBodyBuilder?.build(args) ??
24+
this.requestBodyBuilder?.build(args, gqlQuery) ??
2525
this.requestFormBuilder?.build(args)
2626
);
2727
}

lib/builders/request-body.builder.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,23 @@ describe('RequestBodyBuilder', () => {
3737
// then
3838
expect(actual).toBe('{"foo":"bar"}');
3939
});
40+
41+
test('should include gql query', () => {
42+
// given
43+
const builder = new RequestBodyBuilder(0, 'name');
44+
const args = ['param'];
45+
const gqlQuery = /* GraphQL */ `
46+
query hello($name: String!) {
47+
hello(name: $name)
48+
}
49+
`;
50+
51+
// when
52+
const actual = builder.build(args, gqlQuery);
53+
54+
// then
55+
expect(actual).toBe(
56+
JSON.stringify({ query: gqlQuery, variables: { name: 'param' } }),
57+
);
58+
});
4059
});

lib/builders/request-body.builder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class RequestBodyBuilder {
1313
this.metadata.push([index, [key, defaultValue]]);
1414
}
1515

16-
build(args: any[]): string {
16+
build(args: any[], gqlQuery?: string): string {
1717
const result = this.metadata.reduce<Record<string, any>>(
1818
(acc, [index, [key, defaultValue]]) => {
1919
if (key != null) {
@@ -32,6 +32,8 @@ export class RequestBodyBuilder {
3232
{},
3333
);
3434

35-
return JSON.stringify(result);
35+
return JSON.stringify(
36+
gqlQuery != null ? { query: gqlQuery, variables: result } : result,
37+
);
3638
}
3739
}

lib/supports/node-fetch.injector.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
RequestHeader,
1818
RequestParam,
1919
} from '../decorators';
20+
import { GraphQLExchange } from '../decorators/graphql-exchange.decorator';
2021
import { ResponseBody } from '../decorators/response-body.decorator';
2122
import { StubDiscoveryService } from '../fixtures/stub-discovery.service';
2223
import { StubHttpClient } from '../fixtures/stub-http-client';
@@ -428,4 +429,36 @@ describe('NodeFetchInjector', () => {
428429
expect(httpClient.requestInfo).toHaveLength(1);
429430
expect(httpClient.requestInfo[0].headers.get('Cookie')).toBe('BAR');
430431
});
432+
433+
test('should make graphql request', async () => {
434+
// given
435+
const query = /* GraphQL */ `
436+
query hello($name: String!) {
437+
hello(name: $name)
438+
}
439+
`;
440+
@HttpInterface('https://example.com')
441+
class SampleClient {
442+
@GraphQLExchange(query)
443+
async request(
444+
@RequestBody() body: Record<string, unknown>,
445+
): Promise<string> {
446+
return 'request';
447+
}
448+
}
449+
450+
const instance = discoveryService.addProvider(SampleClient);
451+
httpClient.addResponse({ status: 'ok' });
452+
nodeFetchInjector.onModuleInit();
453+
454+
// when
455+
await instance.request({ name: 'int' });
456+
457+
// then
458+
expect(httpClient.requestInfo).toHaveLength(1);
459+
expect(await httpClient.requestInfo[0].json()).toEqual({
460+
query,
461+
variables: { name: 'int' },
462+
});
463+
});
431464
});

0 commit comments

Comments
 (0)