UNPKG

@graphql-codegen/testing

Version:
34 lines (33 loc) 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mockGraphQLServer = mockGraphQLServer; const tslib_1 = require("tslib"); const graphql_yoga_1 = require("graphql-yoga"); const nock_1 = tslib_1.__importDefault(require("nock")); function mockGraphQLServer({ schema, host, path, intercept, method = 'POST', }) { const yoga = (0, graphql_yoga_1.createYoga)({ schema, logging: false }); const handler = async function (uri, body) { if (intercept) { intercept(this); } // Create a generic Request object that can be consumed by Yoga's fetch API const request = new Request(new URL(host + uri), { method, headers: this.req.headers, body: method === 'GET' ? undefined : JSON.stringify(body), }); const response = await yoga.fetch(request); const headers = {}; for (const [name, value] of response.headers) { headers[name] = value; } return [response.status, await response.json(), headers]; }; switch (method) { case 'GET': return (0, nock_1.default)(host).get(path).reply(handler); case 'POST': return (0, nock_1.default)(host).post(path).reply(handler); } return null; }