UNPKG

@graphql-inspector/action

Version:

GraphQL Inspector functionality for GitHub Actions

47 lines (46 loc) 1.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mockGraphQLServer = void 0; const tslib_1 = require("tslib"); const graphql_1 = require("graphql"); const nock_1 = tslib_1.__importDefault(require("nock")); function mockGraphQLServer({ schema, host, path, method = 'POST', }) { const scope = (0, nock_1.default)(host); if (method === 'GET') { scope .get(path => path.startsWith(path)) .reply((unformattedQuery, _) => tslib_1.__awaiter(this, void 0, void 0, function* () { const query = new URL(host + unformattedQuery).searchParams.get('query'); try { const result = yield (0, graphql_1.execute)({ schema, document: (0, graphql_1.parse)(query || ''), }); return [200, result]; } catch (error) { return [500, error]; } })); } else { scope.post(path).reply((_, body) => tslib_1.__awaiter(this, void 0, void 0, function* () { try { const result = yield (0, graphql_1.execute)({ schema, document: (0, graphql_1.parse)(body.query), operationName: body.operationName, variableValues: body.variables, }); return [200, result]; } catch (error) { return [500, error]; } })); } return () => { scope.done(); }; } exports.mockGraphQLServer = mockGraphQLServer;