makeExecutableSchema.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. function __export(m) {
  2. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  3. }
  4. Object.defineProperty(exports, "__esModule", { value: true });
  5. var graphql_1 = require("graphql");
  6. var schemaVisitor_1 = require("./schemaVisitor");
  7. var mergeDeep_1 = require("./mergeDeep");
  8. var generate_1 = require("./generate");
  9. function makeExecutableSchema(_a) {
  10. var typeDefs = _a.typeDefs, _b = _a.resolvers, resolvers = _b === void 0 ? {} : _b, connectors = _a.connectors, logger = _a.logger, _c = _a.allowUndefinedInResolve, allowUndefinedInResolve = _c === void 0 ? true : _c, _d = _a.resolverValidationOptions, resolverValidationOptions = _d === void 0 ? {} : _d, _e = _a.directiveResolvers, directiveResolvers = _e === void 0 ? null : _e, _f = _a.schemaDirectives, schemaDirectives = _f === void 0 ? null : _f, _g = _a.parseOptions, parseOptions = _g === void 0 ? {} : _g, _h = _a.inheritResolversFromInterfaces, inheritResolversFromInterfaces = _h === void 0 ? false : _h;
  11. // Validate and clean up arguments
  12. if (typeof resolverValidationOptions !== 'object') {
  13. throw new generate_1.SchemaError('Expected `resolverValidationOptions` to be an object');
  14. }
  15. if (!typeDefs) {
  16. throw new generate_1.SchemaError('Must provide typeDefs');
  17. }
  18. if (!resolvers) {
  19. throw new generate_1.SchemaError('Must provide resolvers');
  20. }
  21. // We allow passing in an array of resolver maps, in which case we merge them
  22. var resolverMap = Array.isArray(resolvers)
  23. ? resolvers.filter(function (resolverObj) { return typeof resolverObj === 'object'; }).reduce(mergeDeep_1.default, {})
  24. : resolvers;
  25. // Arguments are now validated and cleaned up
  26. var schema = generate_1.buildSchemaFromTypeDefinitions(typeDefs, parseOptions);
  27. schema = generate_1.addResolveFunctionsToSchema({
  28. schema: schema,
  29. resolvers: resolverMap,
  30. resolverValidationOptions: resolverValidationOptions,
  31. inheritResolversFromInterfaces: inheritResolversFromInterfaces
  32. });
  33. generate_1.assertResolveFunctionsPresent(schema, resolverValidationOptions);
  34. if (!allowUndefinedInResolve) {
  35. addCatchUndefinedToSchema(schema);
  36. }
  37. if (logger) {
  38. addErrorLoggingToSchema(schema, logger);
  39. }
  40. if (typeof resolvers['__schema'] === 'function') {
  41. // TODO a bit of a hack now, better rewrite generateSchema to attach it there.
  42. // not doing that now, because I'd have to rewrite a lot of tests.
  43. generate_1.addSchemaLevelResolveFunction(schema, resolvers['__schema']);
  44. }
  45. if (connectors) {
  46. // connectors are optional, at least for now. That means you can just import them in the resolve
  47. // function if you want.
  48. generate_1.attachConnectorsToContext(schema, connectors);
  49. }
  50. if (directiveResolvers) {
  51. generate_1.attachDirectiveResolvers(schema, directiveResolvers);
  52. }
  53. if (schemaDirectives) {
  54. schemaVisitor_1.SchemaDirectiveVisitor.visitSchemaDirectives(schema, schemaDirectives);
  55. }
  56. return schema;
  57. }
  58. exports.makeExecutableSchema = makeExecutableSchema;
  59. function decorateToCatchUndefined(fn, hint) {
  60. if (typeof fn === 'undefined') {
  61. fn = graphql_1.defaultFieldResolver;
  62. }
  63. return function (root, args, ctx, info) {
  64. var result = fn(root, args, ctx, info);
  65. if (typeof result === 'undefined') {
  66. throw new Error("Resolve function for \"" + hint + "\" returned undefined");
  67. }
  68. return result;
  69. };
  70. }
  71. function addCatchUndefinedToSchema(schema) {
  72. generate_1.forEachField(schema, function (field, typeName, fieldName) {
  73. var errorHint = typeName + "." + fieldName;
  74. field.resolve = decorateToCatchUndefined(field.resolve, errorHint);
  75. });
  76. }
  77. exports.addCatchUndefinedToSchema = addCatchUndefinedToSchema;
  78. function addErrorLoggingToSchema(schema, logger) {
  79. if (!logger) {
  80. throw new Error('Must provide a logger');
  81. }
  82. if (typeof logger.log !== 'function') {
  83. throw new Error('Logger.log must be a function');
  84. }
  85. generate_1.forEachField(schema, function (field, typeName, fieldName) {
  86. var errorHint = typeName + "." + fieldName;
  87. field.resolve = generate_1.decorateWithLogger(field.resolve, logger, errorHint);
  88. });
  89. }
  90. exports.addErrorLoggingToSchema = addErrorLoggingToSchema;
  91. __export(require("./generate"));
  92. //# sourceMappingURL=makeExecutableSchema.js.map