graphql.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.graphql = graphql;
  6. exports.graphqlSync = graphqlSync;
  7. var _isPromise = _interopRequireDefault(require("./jsutils/isPromise.js"));
  8. var _parser = require("./language/parser.js");
  9. var _validate = require("./validation/validate.js");
  10. var _validate2 = require("./type/validate.js");
  11. var _execute = require("./execution/execute.js");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function graphql(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) {
  14. var _arguments = arguments;
  15. /* eslint-enable no-redeclare */
  16. // Always return a Promise for a consistent API.
  17. return new Promise(function (resolve) {
  18. return resolve( // Extract arguments from object args if provided.
  19. _arguments.length === 1 ? graphqlImpl(argsOrSchema) : graphqlImpl({
  20. schema: argsOrSchema,
  21. source: source,
  22. rootValue: rootValue,
  23. contextValue: contextValue,
  24. variableValues: variableValues,
  25. operationName: operationName,
  26. fieldResolver: fieldResolver,
  27. typeResolver: typeResolver
  28. }));
  29. });
  30. }
  31. /**
  32. * The graphqlSync function also fulfills GraphQL operations by parsing,
  33. * validating, and executing a GraphQL document along side a GraphQL schema.
  34. * However, it guarantees to complete synchronously (or throw an error) assuming
  35. * that all field resolvers are also synchronous.
  36. */
  37. function graphqlSync(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) {
  38. /* eslint-enable no-redeclare */
  39. // Extract arguments from object args if provided.
  40. var result = arguments.length === 1 ? graphqlImpl(argsOrSchema) : graphqlImpl({
  41. schema: argsOrSchema,
  42. source: source,
  43. rootValue: rootValue,
  44. contextValue: contextValue,
  45. variableValues: variableValues,
  46. operationName: operationName,
  47. fieldResolver: fieldResolver,
  48. typeResolver: typeResolver
  49. }); // Assert that the execution was synchronous.
  50. if ((0, _isPromise.default)(result)) {
  51. throw new Error('GraphQL execution failed to complete synchronously.');
  52. }
  53. return result;
  54. }
  55. function graphqlImpl(args) {
  56. var schema = args.schema,
  57. source = args.source,
  58. rootValue = args.rootValue,
  59. contextValue = args.contextValue,
  60. variableValues = args.variableValues,
  61. operationName = args.operationName,
  62. fieldResolver = args.fieldResolver,
  63. typeResolver = args.typeResolver; // Validate Schema
  64. var schemaValidationErrors = (0, _validate2.validateSchema)(schema);
  65. if (schemaValidationErrors.length > 0) {
  66. return {
  67. errors: schemaValidationErrors
  68. };
  69. } // Parse
  70. var document;
  71. try {
  72. document = (0, _parser.parse)(source);
  73. } catch (syntaxError) {
  74. return {
  75. errors: [syntaxError]
  76. };
  77. } // Validate
  78. var validationErrors = (0, _validate.validate)(schema, document);
  79. if (validationErrors.length > 0) {
  80. return {
  81. errors: validationErrors
  82. };
  83. } // Execute
  84. return (0, _execute.execute)({
  85. schema: schema,
  86. document: document,
  87. rootValue: rootValue,
  88. contextValue: contextValue,
  89. variableValues: variableValues,
  90. operationName: operationName,
  91. fieldResolver: fieldResolver,
  92. typeResolver: typeResolver
  93. });
  94. }