description and source-codefunction graphqlHTTP(options) {
if (!options) {
throw new Error('GraphQL middleware requires options.');
}
return function (request, response) {
// Higher scoped variables are referred to at various stages in the
// asynchronous state machine below.
var schema = void 0;
var context = void 0;
var rootValue = void 0;
var pretty = void 0;
var graphiql = void 0;
var formatErrorFn = void 0;
var extensionsFn = void 0;
var showGraphiQL = void 0;
var query = void 0;
var documentAST = void 0;
var variables = void 0;
var operationName = void 0;
var validationRules = void 0;
// Promises are used as a mechanism for capturing any thrown errors during
// the asynchronous process below.
// Resolve the Options to get OptionsData.
return new Promise(function (resolve) {
resolve(typeof options === 'function' ? options(request, response) : options);
}).then(function (optionsData) {
// Assert that optionsData is in fact an Object.
if (!optionsData || (typeof optionsData === 'undefined' ? 'undefined' : _typeof(optionsData)) !== 'object') {
throw new Error('GraphQL middleware option function must return an options object ' + 'or a promise which will be resolved
to an options object.');
}
// Assert that schema is required.
if (!optionsData.schema) {
throw new Error('GraphQL middleware options must contain a schema.');
}
// Collect information from the options data object.
schema = optionsData.schema;
context = optionsData.context || request;
rootValue = optionsData.rootValue;
pretty = optionsData.pretty;
graphiql = optionsData.graphiql;
formatErrorFn = optionsData.formatError;
extensionsFn = optionsData.extensions;
validationRules = _graphql.specifiedRules;
if (optionsData.validationRules) {
validationRules = validationRules.concat(optionsData.validationRules);
}
// GraphQL HTTP only supports GET and POST methods.
if (request.method !== 'GET' && request.method !== 'POST') {
response.setHeader('Allow', 'GET, POST');
throw (0, _httpErrors2.default)(405, 'GraphQL only supports GET and POST requests.');
}
// Parse the Request to get GraphQL request parameters.
return getGraphQLParams(request);
}).then(function (params) {
// Get GraphQL params from the request and POST body data.
query = params.query;
variables = params.variables;
operationName = params.operationName;
showGraphiQL = graphiql && canDisplayGraphiQL(request, params);
// If there is no query, but GraphiQL will be displayed, do not produce
// a result, otherwise return a 400: Bad Request.
if (!query) {
if (showGraphiQL) {
return null;
}
throw (0, _httpErrors2.default)(400, 'Must provide query string.');
}
// GraphQL source.
var source = new _graphql.Source(query, 'GraphQL request');
// Parse source to AST, reporting any syntax error.
try {
documentAST = (0, _graphql.parse)(source);
} catch (syntaxError) {
// Return 400: Bad Request if any syntax errors errors exist.
response.statusCode = 400;
return { errors: [syntaxError] };
}
// Validate AST, reporting any errors.
var validationErrors = (0, _graphql.validate)(schema, documentAST, validationRules);
if (validationErrors.length > 0) {
// Return 400: Bad Request if any validation errors exist.
response.statusCode = 400;
return { errors: validationErrors };
}
// Only query operations are allowed on GET requests.
if (request.method === 'GET') {
// Determine if this GET request will perform a non-query.
var operationAST = (0, _graphql.getOperationAST)(documentAST, operationName);
if (operationAST && operationAST.operation !== 'query') {
// If GraphiQL can be shown, do not perform this query, but
// provide ...