UNPKG

@graphql-inspector/action

Version:

GraphQL Inspector functionality for GitHub Actions

43 lines (42 loc) 1.36 kB
import { __awaiter } from "tslib"; import { execute, parse } from 'graphql'; import nock from 'nock'; export function mockGraphQLServer({ schema, host, path, method = 'POST', }) { const scope = nock(host); if (method === 'GET') { scope .get(path => path.startsWith(path)) .reply((unformattedQuery, _) => __awaiter(this, void 0, void 0, function* () { const query = new URL(host + unformattedQuery).searchParams.get('query'); try { const result = yield execute({ schema, document: parse(query || ''), }); return [200, result]; } catch (error) { return [500, error]; } })); } else { scope.post(path).reply((_, body) => __awaiter(this, void 0, void 0, function* () { try { const result = yield execute({ schema, document: parse(body.query), operationName: body.operationName, variableValues: body.variables, }); return [200, result]; } catch (error) { return [500, error]; } })); } return () => { scope.done(); }; }