Skip to content

Commit 30d3819

Browse files
jbl428imdudu1
andcommitted
fix: include gql query if request body decorator not exist
Co-authored-by: imdudu1 <[email protected]>
1 parent 2779640 commit 30d3819

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lib/builders/http-request.builder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class HttpRequestBuilder {
3232
this.payloadBuilder = new PayloadBuilder(
3333
this.getMetadata(REQUEST_BODY_METADATA),
3434
this.getMetadata(REQUEST_FORM_METADATA),
35+
gqlQuery,
3536
);
3637
}
3738

@@ -47,7 +48,7 @@ export class HttpRequestBuilder {
4748
this.pathVariableBuilder,
4849
this.requestParamBuilder,
4950
);
50-
const payload = this.payloadBuilder.build(args, this.gqlQuery);
51+
const payload = this.payloadBuilder.build(args);
5152
const headers = this.requestHeaderBuilder?.build(args);
5253

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

lib/builders/payload.builder.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ export class PayloadBuilder {
55
constructor(
66
private readonly requestBodyBuilder?: RequestBodyBuilder,
77
private readonly requestFormBuilder?: RequestFormBuilder,
8+
private readonly gqlQuery?: string,
89
) {}
910

1011
get contentType(): { 'Content-Type': string } | undefined {
11-
if (this.requestBodyBuilder != null) {
12+
if (this.gqlQuery != null || this.requestBodyBuilder != null) {
1213
return { 'Content-Type': 'application/json' };
1314
}
1415

@@ -19,9 +20,13 @@ export class PayloadBuilder {
1920
return undefined;
2021
}
2122

22-
build(args: any[], gqlQuery?: string): BodyInit | undefined {
23+
build(args: any[]): BodyInit | undefined {
24+
if (this.gqlQuery != null && this.requestBodyBuilder == null) {
25+
return JSON.stringify({ query: this.gqlQuery });
26+
}
27+
2328
return (
24-
this.requestBodyBuilder?.build(args, gqlQuery) ??
29+
this.requestBodyBuilder?.build(args, this.gqlQuery) ??
2530
this.requestFormBuilder?.build(args)
2631
);
2732
}

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,37 @@ describe('NodeFetchInjector', () => {
430430
expect(httpClient.requestInfo[0].headers.get('Cookie')).toBe('BAR');
431431
});
432432

433-
test('should make graphql request', async () => {
433+
test('should make graphql request without variables', async () => {
434+
// given
435+
const query = /* GraphQL */ `
436+
query hello {
437+
hello
438+
}
439+
`;
440+
@HttpInterface('https://example.com')
441+
class SampleClient {
442+
@GraphQLExchange(query)
443+
async request(): Promise<string> {
444+
return 'request';
445+
}
446+
}
447+
448+
const instance = discoveryService.addProvider(SampleClient);
449+
httpClient.addResponse({ status: 'ok' });
450+
nodeFetchInjector.onModuleInit();
451+
452+
// when
453+
await instance.request();
454+
455+
// then
456+
expect(httpClient.requestInfo).toHaveLength(1);
457+
expect(await httpClient.requestInfo[0].json()).toEqual({ query });
458+
expect(httpClient.requestInfo[0].headers.get('Content-Type')).toBe(
459+
'application/json',
460+
);
461+
});
462+
463+
test('should make graphql request with variables', async () => {
434464
// given
435465
const query = /* GraphQL */ `
436466
query hello($name: String!) {
@@ -460,5 +490,8 @@ describe('NodeFetchInjector', () => {
460490
query,
461491
variables: { name: 'int' },
462492
});
493+
expect(httpClient.requestInfo[0].headers.get('Content-Type')).toBe(
494+
'application/json',
495+
);
463496
});
464497
});

0 commit comments

Comments
 (0)