graphql
Version:
A Query Language and Runtime which can target any service.
63 lines (50 loc) • 2.07 kB
JavaScript
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
;
var _Object$defineProperty = require('babel-runtime/core-js/object/define-property')['default'];
var _Promise = require('babel-runtime/core-js/promise')['default'];
_Object$defineProperty(exports, '__esModule', {
value: true
});
exports.graphql = graphql;
/**
* The result of a GraphQL parse, validation and execution.
*
* `data` is the result of a successful execution of the query.
* `errors` is included when any errors occurred as a non-empty array.
*/
var _languageSource = require('./language/source');
var _languageParser = require('./language/parser');
var _validator = require('./validator');
var _executorExecutor = require('./executor/executor');
var _error = require('./error');
/**
* This is the primary entry point function for fulfilling GraphQL operations
* by parsing, validating, and executing a GraphQL document along side a
* GraphQL schema.
*
* More sophisticated GraphQL servers, such as those which persist queries,
* may wish to separate the validation and execution phases to a static time
* tooling step, and a server runtime step.
*/
function graphql(schema, requestString, rootObject, variableValues, operationName) {
return new _Promise(function (resolve) {
var source = new _languageSource.Source(requestString || '', 'GraphQL request');
var ast = (0, _languageParser.parse)(source);
var validationResult = (0, _validator.validateDocument)(schema, ast);
if (!validationResult.isValid) {
resolve({ errors: validationResult.errors });
} else {
resolve((0, _executorExecutor.execute)(schema, rootObject, ast, operationName, variableValues));
}
})['catch'](function (error) {
return { errors: [(0, _error.formatError)(error)] };
});
}