graphql-mocks
Version:
Tools for setting up graphql test resolvers
114 lines (85 loc) • 4.71 kB
JavaScript
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
Object.defineProperty(exports, '__esModule', {
value: true
});
var graphql = require('graphql');
var pack = require('../pack/pack.js');
require('../pack/utils/embed-pack-options-wrapper.js');
var attachResolversToSchema = require('./utils/attach-resolvers-to-schema.js');
var attachScalarsToSchema = require('./utils/attach-scalars-to-schema.js');
var createSchema = require('./utils/create-schema.js');
var buildContext = require('./utils/build-context.js');
var normalizePackOptions = require('../pack/utils/normalize-pack-options.js');
class GraphQLHandler {
constructor(options) {
var _options$dependencies, _options$resolverMap, _options$state, _options$middlewares, _options$scalarMap;
this.packed = false;
const graphqlSchema = createSchema.createSchema((_options$dependencies = options.dependencies) === null || _options$dependencies === void 0 ? void 0 : _options$dependencies.graphqlSchema);
const dependencies = _objectSpread(_objectSpread({}, options.dependencies), {}, {
graphqlSchema
});
this.packOptions = normalizePackOptions.normalizePackOptions({
dependencies,
state: options.state
});
this.graphqlSchema = graphqlSchema;
this.initialContext = options.initialContext;
this.initialResolverMap = (_options$resolverMap = options.resolverMap) !== null && _options$resolverMap !== void 0 ? _options$resolverMap : {};
this.state = (_options$state = options.state) !== null && _options$state !== void 0 ? _options$state : {};
this.middlewares = (_options$middlewares = options.middlewares) !== null && _options$middlewares !== void 0 ? _options$middlewares : [];
this.scalarMap = (_options$scalarMap = options.scalarMap) !== null && _options$scalarMap !== void 0 ? _options$scalarMap : {};
}
applyMiddlewares(middlewares, options) {
var _options$reset;
const reset = (_options$reset = options === null || options === void 0 ? void 0 : options.reset) !== null && _options$reset !== void 0 ? _options$reset : false;
if (!reset) {
middlewares = [...this.middlewares, ...middlewares];
}
this.middlewares = middlewares;
this.packed = false;
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
async query(query, variableValues, queryContext, graphqlArgs) {
var _variableValues, _queryContext;
const initialContext = this.initialContext;
const packOptions = this.packOptions;
const schema = this.graphqlSchema;
variableValues = (_variableValues = variableValues) !== null && _variableValues !== void 0 ? _variableValues : {};
queryContext = (_queryContext = queryContext) !== null && _queryContext !== void 0 ? _queryContext : {};
await this.pack();
const contextValue = buildContext.buildContext({
initialContext,
queryContext,
packOptions
});
return graphql.graphql(_objectSpread(_objectSpread({}, graphqlArgs), {}, {
source: query,
schema,
variableValues,
contextValue
}));
}
async pack() {
const {
initialResolverMap,
middlewares,
packOptions,
graphqlSchema,
scalarMap
} = this;
if (!this.packed) {
const {
resolverMap,
state
} = await pack.pack(initialResolverMap, middlewares, packOptions);
this.state = state;
attachResolversToSchema.attachResolversToSchema(graphqlSchema, resolverMap);
attachScalarsToSchema.attachScalarsToSchema(graphqlSchema, scalarMap);
}
this.packed = true;
}
}
exports.GraphQLHandler = GraphQLHandler;
//# sourceMappingURL=handler.js.map
;