|
| 1 | +import { assertOk, ResultAsync } from '@aave/types'; |
| 2 | +import { gql, stringifyDocument } from '@urql/core'; |
| 3 | +import * as msw from 'msw'; |
| 4 | +import { setupServer } from 'msw/node'; |
| 5 | +import { |
| 6 | + afterAll, |
| 7 | + afterEach, |
| 8 | + beforeAll, |
| 9 | + beforeEach, |
| 10 | + describe, |
| 11 | + expect, |
| 12 | + it, |
| 13 | +} from 'vitest'; |
| 14 | + |
| 15 | +import type { Context } from './context'; |
| 16 | +import { GqlClient } from './GqlClient'; |
| 17 | +import { delay } from './utils'; |
| 18 | + |
| 19 | +// see: https://mswjs.io/docs/graphql/mocking-responses/query-batching |
| 20 | +function batched(url: string, handlers: Array<msw.RequestHandler>) { |
| 21 | + return msw.http.post(url, async ({ request }) => { |
| 22 | + const requestClone = request.clone(); |
| 23 | + const payload = await request.clone().json(); |
| 24 | + |
| 25 | + // Ignore non-batched GraphQL queries. |
| 26 | + if (!Array.isArray(payload)) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + const responses = await Promise.all( |
| 31 | + payload.map(async (query) => { |
| 32 | + // Construct an individual query request |
| 33 | + // to the same URL but with an unwrapped query body. |
| 34 | + const queryRequest = new Request(requestClone, { |
| 35 | + body: JSON.stringify(query), |
| 36 | + }); |
| 37 | + |
| 38 | + // Resolve the individual query request |
| 39 | + // against the list of request handlers you provide. |
| 40 | + const response = await msw.getResponse(handlers, queryRequest); |
| 41 | + |
| 42 | + // Return the mocked response, if found. |
| 43 | + // Otherwise, perform the individual query as-is, |
| 44 | + // so it can be resolved against an original server. |
| 45 | + return response || fetch(msw.bypass(queryRequest)); |
| 46 | + }), |
| 47 | + ); |
| 48 | + |
| 49 | + // Read the mocked response JSON bodies to use |
| 50 | + // in the response to the entire batched query. |
| 51 | + const queryData = await Promise.all( |
| 52 | + responses.map((response) => response?.json()), |
| 53 | + ); |
| 54 | + |
| 55 | + return msw.HttpResponse.json(queryData); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +export const TestQuery = gql` |
| 60 | + query TestQuery($id: Int!) { |
| 61 | + value(id: $id) |
| 62 | + } |
| 63 | +`; |
| 64 | + |
| 65 | +const context: Context = { |
| 66 | + displayName: 'GqlClient', |
| 67 | + environment: { |
| 68 | + name: 'test', |
| 69 | + backend: 'https://api.aave.com', |
| 70 | + indexingTimeout: 1000, |
| 71 | + pollingInterval: 1000, |
| 72 | + }, |
| 73 | + headers: {}, |
| 74 | + cache: null, |
| 75 | + debug: false, |
| 76 | + fragments: [], |
| 77 | +}; |
| 78 | + |
| 79 | +const api = msw.graphql.link(context.environment.backend); |
| 80 | + |
| 81 | +const server = setupServer( |
| 82 | + batched(context.environment.backend, [ |
| 83 | + api.query(TestQuery, ({ variables }) => |
| 84 | + msw.HttpResponse.json({ |
| 85 | + data: { |
| 86 | + value: variables.id, |
| 87 | + }, |
| 88 | + }), |
| 89 | + ), |
| 90 | + ]), |
| 91 | + api.query(TestQuery, ({ variables }) => |
| 92 | + msw.HttpResponse.json({ |
| 93 | + data: { |
| 94 | + value: variables.id, |
| 95 | + }, |
| 96 | + }), |
| 97 | + ), |
| 98 | +); |
| 99 | + |
| 100 | +describe(`Given an instance of the ${GqlClient.name}`, () => { |
| 101 | + let requests: Request[]; |
| 102 | + |
| 103 | + beforeAll(() => { |
| 104 | + server.events.on('request:start', ({ request }) => { |
| 105 | + requests.push(request.clone()); |
| 106 | + }); |
| 107 | + |
| 108 | + server.listen(); |
| 109 | + }); |
| 110 | + |
| 111 | + beforeEach(() => { |
| 112 | + requests = []; |
| 113 | + }); |
| 114 | + |
| 115 | + afterEach(() => { |
| 116 | + server.resetHandlers(); |
| 117 | + }); |
| 118 | + |
| 119 | + afterAll(() => { |
| 120 | + server.close(); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('When executing a single isolated query', () => { |
| 124 | + it('Then it should execute it in its own HTTP request', async () => { |
| 125 | + const client = new GqlClient(context); |
| 126 | + |
| 127 | + const result = await client.query(TestQuery, { id: 1 }); |
| 128 | + |
| 129 | + assertOk(result); |
| 130 | + expect(await requests[0]!.json()).toEqual({ |
| 131 | + operationName: 'TestQuery', |
| 132 | + query: stringifyDocument(TestQuery), |
| 133 | + variables: { id: 1 }, |
| 134 | + }); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('When executing concurrent queries', () => { |
| 139 | + it('Then it should batch queries fired in the same event-loop tick into a single HTTP request', async () => { |
| 140 | + const client = new GqlClient(context); |
| 141 | + |
| 142 | + const resul = await ResultAsync.combine([ |
| 143 | + client.query(TestQuery, { id: 1 }), |
| 144 | + client.query(TestQuery, { id: 2 }), |
| 145 | + // on a separate tick, fire the third query |
| 146 | + ResultAsync.fromSafePromise(delay(1)).andThen(() => |
| 147 | + client.query(TestQuery, { id: 3 }), |
| 148 | + ), |
| 149 | + ]); |
| 150 | + |
| 151 | + assertOk(resul); |
| 152 | + expect(requests).toHaveLength(2); // 2 HTTP requests were made |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('When batching concurrent queries', () => { |
| 157 | + it('Then it should limit batching to a maximum of 10 queries', async () => { |
| 158 | + const client = new GqlClient(context); |
| 159 | + |
| 160 | + const result = await ResultAsync.combine( |
| 161 | + Array.from({ length: 15 }, (_, i) => |
| 162 | + client.query(TestQuery, { id: String(i + 1) }), |
| 163 | + ), |
| 164 | + ); |
| 165 | + |
| 166 | + assertOk(result); |
| 167 | + expect(requests).toHaveLength(2); // 2 HTTP requests were made |
| 168 | + }); |
| 169 | + |
| 170 | + it('Then it should allow single queries to skip the batching', async () => { |
| 171 | + const client = new GqlClient(context); |
| 172 | + |
| 173 | + const result = await ResultAsync.combine([ |
| 174 | + client.query(TestQuery, { id: 1 }), |
| 175 | + client.query(TestQuery, { id: 2 }), |
| 176 | + client.query(TestQuery, { id: 3 }, { batch: false }), |
| 177 | + ]); |
| 178 | + |
| 179 | + assertOk(result); |
| 180 | + expect(requests).toHaveLength(2); // 2 HTTP requests were made (one batched, one not) |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
0 commit comments