graphql.js.flow 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // @flow strict
  2. import type { PromiseOrValue } from './jsutils/PromiseOrValue';
  3. import isPromise from './jsutils/isPromise';
  4. import type { Source } from './language/source';
  5. import { parse } from './language/parser';
  6. import { validate } from './validation/validate';
  7. import type {
  8. GraphQLFieldResolver,
  9. GraphQLTypeResolver,
  10. } from './type/definition';
  11. import type { GraphQLSchema } from './type/schema';
  12. import { validateSchema } from './type/validate';
  13. import type { ExecutionResult } from './execution/execute';
  14. import { execute } from './execution/execute';
  15. /**
  16. * This is the primary entry point function for fulfilling GraphQL operations
  17. * by parsing, validating, and executing a GraphQL document along side a
  18. * GraphQL schema.
  19. *
  20. * More sophisticated GraphQL servers, such as those which persist queries,
  21. * may wish to separate the validation and execution phases to a static time
  22. * tooling step, and a server runtime step.
  23. *
  24. * Accepts either an object with named arguments, or individual arguments:
  25. *
  26. * schema:
  27. * The GraphQL type system to use when validating and executing a query.
  28. * source:
  29. * A GraphQL language formatted string representing the requested operation.
  30. * rootValue:
  31. * The value provided as the first argument to resolver functions on the top
  32. * level type (e.g. the query object type).
  33. * contextValue:
  34. * The context value is provided as an argument to resolver functions after
  35. * field arguments. It is used to pass shared information useful at any point
  36. * during executing this query, for example the currently logged in user and
  37. * connections to databases or other services.
  38. * variableValues:
  39. * A mapping of variable name to runtime value to use for all variables
  40. * defined in the requestString.
  41. * operationName:
  42. * The name of the operation to use if requestString contains multiple
  43. * possible operations. Can be omitted if requestString contains only
  44. * one operation.
  45. * fieldResolver:
  46. * A resolver function to use when one is not provided by the schema.
  47. * If not provided, the default field resolver is used (which looks for a
  48. * value or method on the source value with the field's name).
  49. * typeResolver:
  50. * A type resolver function to use when none is provided by the schema.
  51. * If not provided, the default type resolver is used (which looks for a
  52. * `__typename` field or alternatively calls the `isTypeOf` method).
  53. */
  54. export type GraphQLArgs = {|
  55. schema: GraphQLSchema,
  56. source: string | Source,
  57. rootValue?: mixed,
  58. contextValue?: mixed,
  59. variableValues?: ?{ +[variable: string]: mixed, ... },
  60. operationName?: ?string,
  61. fieldResolver?: ?GraphQLFieldResolver<any, any>,
  62. typeResolver?: ?GraphQLTypeResolver<any, any>,
  63. |};
  64. declare function graphql(GraphQLArgs, ..._: []): Promise<ExecutionResult>;
  65. /* eslint-disable no-redeclare */
  66. declare function graphql(
  67. schema: GraphQLSchema,
  68. source: Source | string,
  69. rootValue?: mixed,
  70. contextValue?: mixed,
  71. variableValues?: ?{ +[variable: string]: mixed, ... },
  72. operationName?: ?string,
  73. fieldResolver?: ?GraphQLFieldResolver<any, any>,
  74. typeResolver?: ?GraphQLTypeResolver<any, any>,
  75. ): Promise<ExecutionResult>;
  76. export function graphql(
  77. argsOrSchema,
  78. source,
  79. rootValue,
  80. contextValue,
  81. variableValues,
  82. operationName,
  83. fieldResolver,
  84. typeResolver,
  85. ) {
  86. /* eslint-enable no-redeclare */
  87. // Always return a Promise for a consistent API.
  88. return new Promise((resolve) =>
  89. resolve(
  90. // Extract arguments from object args if provided.
  91. arguments.length === 1
  92. ? graphqlImpl(argsOrSchema)
  93. : graphqlImpl({
  94. schema: argsOrSchema,
  95. source,
  96. rootValue,
  97. contextValue,
  98. variableValues,
  99. operationName,
  100. fieldResolver,
  101. typeResolver,
  102. }),
  103. ),
  104. );
  105. }
  106. /**
  107. * The graphqlSync function also fulfills GraphQL operations by parsing,
  108. * validating, and executing a GraphQL document along side a GraphQL schema.
  109. * However, it guarantees to complete synchronously (or throw an error) assuming
  110. * that all field resolvers are also synchronous.
  111. */
  112. declare function graphqlSync(GraphQLArgs, ..._: []): ExecutionResult;
  113. /* eslint-disable no-redeclare */
  114. declare function graphqlSync(
  115. schema: GraphQLSchema,
  116. source: Source | string,
  117. rootValue?: mixed,
  118. contextValue?: mixed,
  119. variableValues?: ?{ +[variable: string]: mixed, ... },
  120. operationName?: ?string,
  121. fieldResolver?: ?GraphQLFieldResolver<any, any>,
  122. typeResolver?: ?GraphQLTypeResolver<any, any>,
  123. ): ExecutionResult;
  124. export function graphqlSync(
  125. argsOrSchema,
  126. source,
  127. rootValue,
  128. contextValue,
  129. variableValues,
  130. operationName,
  131. fieldResolver,
  132. typeResolver,
  133. ) {
  134. /* eslint-enable no-redeclare */
  135. // Extract arguments from object args if provided.
  136. const result =
  137. arguments.length === 1
  138. ? graphqlImpl(argsOrSchema)
  139. : graphqlImpl({
  140. schema: argsOrSchema,
  141. source,
  142. rootValue,
  143. contextValue,
  144. variableValues,
  145. operationName,
  146. fieldResolver,
  147. typeResolver,
  148. });
  149. // Assert that the execution was synchronous.
  150. if (isPromise(result)) {
  151. throw new Error('GraphQL execution failed to complete synchronously.');
  152. }
  153. return result;
  154. }
  155. function graphqlImpl(args: GraphQLArgs): PromiseOrValue<ExecutionResult> {
  156. const {
  157. schema,
  158. source,
  159. rootValue,
  160. contextValue,
  161. variableValues,
  162. operationName,
  163. fieldResolver,
  164. typeResolver,
  165. } = args;
  166. // Validate Schema
  167. const schemaValidationErrors = validateSchema(schema);
  168. if (schemaValidationErrors.length > 0) {
  169. return { errors: schemaValidationErrors };
  170. }
  171. // Parse
  172. let document;
  173. try {
  174. document = parse(source);
  175. } catch (syntaxError) {
  176. return { errors: [syntaxError] };
  177. }
  178. // Validate
  179. const validationErrors = validate(schema, document);
  180. if (validationErrors.length > 0) {
  181. return { errors: validationErrors };
  182. }
  183. // Execute
  184. return execute({
  185. schema,
  186. document,
  187. rootValue,
  188. contextValue,
  189. variableValues,
  190. operationName,
  191. fieldResolver,
  192. typeResolver,
  193. });
  194. }