@graphql-inspector/cli
Version:
Tooling for GraphQL. Compare GraphQL Schemas, check documents, find breaking changes, find similar types.
46 lines (45 loc) • 1.5 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.mockGraphQLServer = mockGraphQLServer;
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(async (unformattedQuery, _) => {
const query = new URL(host + unformattedQuery).searchParams.get('query');
try {
const result = await (0, graphql_1.execute)({
schema,
document: (0, graphql_1.parse)(query || ''),
});
return [200, result];
}
catch (error) {
return [500, error];
}
});
}
else {
scope.post(path).reply(async (_, body) => {
try {
const result = await (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();
};
}
;