graphql.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.graphql = graphql;
  6. exports.graphqlSync = graphqlSync;
  7. var _devAssert = require('./jsutils/devAssert.js');
  8. var _isPromise = require('./jsutils/isPromise.js');
  9. var _parser = require('./language/parser.js');
  10. var _validate = require('./type/validate.js');
  11. var _validate2 = require('./validation/validate.js');
  12. var _execute = require('./execution/execute.js');
  13. function graphql(args) {
  14. // Always return a Promise for a consistent API.
  15. return new Promise((resolve) => resolve(graphqlImpl(args)));
  16. }
  17. /**
  18. * The graphqlSync function also fulfills GraphQL operations by parsing,
  19. * validating, and executing a GraphQL document along side a GraphQL schema.
  20. * However, it guarantees to complete synchronously (or throw an error) assuming
  21. * that all field resolvers are also synchronous.
  22. */
  23. function graphqlSync(args) {
  24. const result = graphqlImpl(args); // Assert that the execution was synchronous.
  25. if ((0, _isPromise.isPromise)(result)) {
  26. throw new Error('GraphQL execution failed to complete synchronously.');
  27. }
  28. return result;
  29. }
  30. function graphqlImpl(args) {
  31. // Temporary for v15 to v16 migration. Remove in v17
  32. arguments.length < 2 ||
  33. (0, _devAssert.devAssert)(
  34. false,
  35. 'graphql@16 dropped long-deprecated support for positional arguments, please pass an object instead.',
  36. );
  37. const {
  38. schema,
  39. source,
  40. rootValue,
  41. contextValue,
  42. variableValues,
  43. operationName,
  44. fieldResolver,
  45. typeResolver,
  46. } = args; // Validate Schema
  47. const schemaValidationErrors = (0, _validate.validateSchema)(schema);
  48. if (schemaValidationErrors.length > 0) {
  49. return {
  50. errors: schemaValidationErrors,
  51. };
  52. } // Parse
  53. let document;
  54. try {
  55. document = (0, _parser.parse)(source);
  56. } catch (syntaxError) {
  57. return {
  58. errors: [syntaxError],
  59. };
  60. } // Validate
  61. const validationErrors = (0, _validate2.validate)(schema, document);
  62. if (validationErrors.length > 0) {
  63. return {
  64. errors: validationErrors,
  65. };
  66. } // Execute
  67. return (0, _execute.execute)({
  68. schema,
  69. document,
  70. rootValue,
  71. contextValue,
  72. variableValues,
  73. operationName,
  74. fieldResolver,
  75. typeResolver,
  76. });
  77. }