Skip to content

Commit 7183653

Browse files
jbl428imdudu1
andcommitted
test: add graphql request test
Co-authored-by: imdudu1 <[email protected]>
1 parent 06fa1ff commit 7183653

File tree

5 files changed

+323
-1
lines changed

5 files changed

+323
-1
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ pnpm-lock.yaml
1313
tsconfig.json
1414
tsconfig.build.json
1515
vitest.config.ts
16+
sonar-project.properties

lib/supports/fetch-http-client.spec.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { type AddressInfo } from 'node:net';
2-
import Fastify, { type FastifyInstance } from 'fastify';
2+
import Fastify, {
3+
type FastifyInstance,
4+
type FastifyReply,
5+
type FastifyRequest,
6+
} from 'fastify';
7+
import { createSchema, createYoga } from 'graphql-yoga';
38
import { describe, test, expect, beforeAll, afterAll } from 'vitest';
49
import { FetchHttpClient } from './fetch-http-client';
510

@@ -18,6 +23,35 @@ describe('FetchHttpClient', () => {
1823
await reply.send({ hello: 'world' });
1924
});
2025

26+
const yoga = createYoga<{
27+
req: FastifyRequest;
28+
reply: FastifyReply;
29+
}>({
30+
schema: createSchema({
31+
typeDefs: /* GraphQL */ `
32+
type Query {
33+
item(id: Int): String
34+
}
35+
`,
36+
resolvers: { Query: { item: () => 'success' } },
37+
}),
38+
});
39+
40+
fastify.route({
41+
url: '/graphql',
42+
method: 'POST',
43+
handler: async (req, reply) => {
44+
const response = await yoga.handleNodeRequest(req, {
45+
req,
46+
reply,
47+
});
48+
void reply.status(response.status);
49+
void reply.send(response.body);
50+
51+
return await reply;
52+
},
53+
});
54+
2155
await fastify.listen({ port: 0 });
2256
});
2357

@@ -38,6 +72,30 @@ describe('FetchHttpClient', () => {
3872
expect(await response.json()).toEqual({ hello: 'world' });
3973
});
4074

75+
test('should request graphql successfully', async () => {
76+
// given
77+
const address = fastify.server.address() as AddressInfo;
78+
const httpClient = new FetchHttpClient(5000);
79+
const request = new Request(`http://localhost:${address.port}/graphql`, {
80+
method: 'POST',
81+
headers: { 'Content-Type': 'application/json' },
82+
body: JSON.stringify({
83+
query: /* GraphQL */ `
84+
query item($id: Int!) {
85+
item(id: $id)
86+
}
87+
`,
88+
variables: { id: 1 },
89+
}),
90+
});
91+
92+
// when
93+
const response = await httpClient.request(request);
94+
95+
// then
96+
expect(await response.json()).toEqual({ data: { item: 'success' } });
97+
});
98+
4199
test('should throw error when timeout', async () => {
42100
// given
43101
const address = fastify.server.address() as AddressInfo;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
"eslint-plugin-import": "^2.27.5",
4545
"eslint-plugin-prettier": "^4.2.1",
4646
"fastify": "^4.15.0",
47+
"graphql": "^16.6.0",
48+
"graphql-yoga": "^3.9.1",
4749
"husky": "^8.0.3",
4850
"lint-staged": "^13.2.1",
4951
"prettier": "^2.8.7",

0 commit comments

Comments
 (0)