graphql.mjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import isPromise from './jsutils/isPromise';
  2. import { parse } from './language/parser';
  3. import { validate } from './validation/validate';
  4. import { validateSchema } from './type/validate';
  5. import { execute } from './execution/execute';
  6. /**
  7. * This is the primary entry point function for fulfilling GraphQL operations
  8. * by parsing, validating, and executing a GraphQL document along side a
  9. * GraphQL schema.
  10. *
  11. * More sophisticated GraphQL servers, such as those which persist queries,
  12. * may wish to separate the validation and execution phases to a static time
  13. * tooling step, and a server runtime step.
  14. *
  15. * Accepts either an object with named arguments, or individual arguments:
  16. *
  17. * schema:
  18. * The GraphQL type system to use when validating and executing a query.
  19. * source:
  20. * A GraphQL language formatted string representing the requested operation.
  21. * rootValue:
  22. * The value provided as the first argument to resolver functions on the top
  23. * level type (e.g. the query object type).
  24. * contextValue:
  25. * The context value is provided as an argument to resolver functions after
  26. * field arguments. It is used to pass shared information useful at any point
  27. * during executing this query, for example the currently logged in user and
  28. * connections to databases or other services.
  29. * variableValues:
  30. * A mapping of variable name to runtime value to use for all variables
  31. * defined in the requestString.
  32. * operationName:
  33. * The name of the operation to use if requestString contains multiple
  34. * possible operations. Can be omitted if requestString contains only
  35. * one operation.
  36. * fieldResolver:
  37. * A resolver function to use when one is not provided by the schema.
  38. * If not provided, the default field resolver is used (which looks for a
  39. * value or method on the source value with the field's name).
  40. * typeResolver:
  41. * A type resolver function to use when none is provided by the schema.
  42. * If not provided, the default type resolver is used (which looks for a
  43. * `__typename` field or alternatively calls the `isTypeOf` method).
  44. */
  45. export function graphql(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) {
  46. var _arguments = arguments;
  47. /* eslint-enable no-redeclare */
  48. // Always return a Promise for a consistent API.
  49. return new Promise(function (resolve) {
  50. return resolve( // Extract arguments from object args if provided.
  51. _arguments.length === 1 ? graphqlImpl(argsOrSchema) : graphqlImpl({
  52. schema: argsOrSchema,
  53. source: source,
  54. rootValue: rootValue,
  55. contextValue: contextValue,
  56. variableValues: variableValues,
  57. operationName: operationName,
  58. fieldResolver: fieldResolver,
  59. typeResolver: typeResolver
  60. }));
  61. });
  62. }
  63. /**
  64. * The graphqlSync function also fulfills GraphQL operations by parsing,
  65. * validating, and executing a GraphQL document along side a GraphQL schema.
  66. * However, it guarantees to complete synchronously (or throw an error) assuming
  67. * that all field resolvers are also synchronous.
  68. */
  69. export function graphqlSync(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) {
  70. /* eslint-enable no-redeclare */
  71. // Extract arguments from object args if provided.
  72. var result = arguments.length === 1 ? graphqlImpl(argsOrSchema) : graphqlImpl({
  73. schema: argsOrSchema,
  74. source: source,
  75. rootValue: rootValue,
  76. contextValue: contextValue,
  77. variableValues: variableValues,
  78. operationName: operationName,
  79. fieldResolver: fieldResolver,
  80. typeResolver: typeResolver
  81. }); // Assert that the execution was synchronous.
  82. if (isPromise(result)) {
  83. throw new Error('GraphQL execution failed to complete synchronously.');
  84. }
  85. return result;
  86. }
  87. function graphqlImpl(args) {
  88. var schema = args.schema,
  89. source = args.source,
  90. rootValue = args.rootValue,
  91. contextValue = args.contextValue,
  92. variableValues = args.variableValues,
  93. operationName = args.operationName,
  94. fieldResolver = args.fieldResolver,
  95. typeResolver = args.typeResolver; // Validate Schema
  96. var schemaValidationErrors = validateSchema(schema);
  97. if (schemaValidationErrors.length > 0) {
  98. return {
  99. errors: schemaValidationErrors
  100. };
  101. } // Parse
  102. var document;
  103. try {
  104. document = parse(source);
  105. } catch (syntaxError) {
  106. return {
  107. errors: [syntaxError]
  108. };
  109. } // Validate
  110. var validationErrors = validate(schema, document);
  111. if (validationErrors.length > 0) {
  112. return {
  113. errors: validationErrors
  114. };
  115. } // Execute
  116. return execute({
  117. schema: schema,
  118. document: document,
  119. rootValue: rootValue,
  120. contextValue: contextValue,
  121. variableValues: variableValues,
  122. operationName: operationName,
  123. fieldResolver: fieldResolver,
  124. typeResolver: typeResolver
  125. });
  126. }