addResolveFunctionsToSchema.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. Object.defineProperty(exports, "__esModule", { value: true });
  2. var _1 = require(".");
  3. var graphql_1 = require("graphql");
  4. var transforms_1 = require("../transforms/transforms");
  5. var _2 = require(".");
  6. var ConvertEnumValues_1 = require("../transforms/ConvertEnumValues");
  7. function addResolveFunctionsToSchema(options, legacyInputResolvers, legacyInputValidationOptions) {
  8. if (options instanceof graphql_1.GraphQLSchema) {
  9. console.warn('The addResolveFunctionsToSchema function takes named options now; see IAddResolveFunctionsToSchemaOptions');
  10. options = {
  11. schema: options,
  12. resolvers: legacyInputResolvers,
  13. resolverValidationOptions: legacyInputValidationOptions,
  14. };
  15. }
  16. var schema = options.schema, inputResolvers = options.resolvers, _a = options.resolverValidationOptions, resolverValidationOptions = _a === void 0 ? {} : _a, _b = options.inheritResolversFromInterfaces, inheritResolversFromInterfaces = _b === void 0 ? false : _b;
  17. var _c = resolverValidationOptions.allowResolversNotInSchema, allowResolversNotInSchema = _c === void 0 ? false : _c, requireResolversForResolveType = resolverValidationOptions.requireResolversForResolveType;
  18. var resolvers = inheritResolversFromInterfaces
  19. ? _2.extendResolversFromInterfaces(schema, inputResolvers)
  20. : inputResolvers;
  21. // Used to map the external value of an enum to its internal value, when
  22. // that internal value is provided by a resolver.
  23. var enumValueMap = Object.create(null);
  24. Object.keys(resolvers).forEach(function (typeName) {
  25. var resolverValue = resolvers[typeName];
  26. var resolverType = typeof resolverValue;
  27. if (resolverType !== 'object' && resolverType !== 'function') {
  28. throw new _1.SchemaError("\"" + typeName + "\" defined in resolvers, but has invalid value \"" + resolverValue + "\". A resolver's value " +
  29. "must be of type object or function.");
  30. }
  31. var type = schema.getType(typeName);
  32. if (!type && typeName !== '__schema') {
  33. if (allowResolversNotInSchema) {
  34. return;
  35. }
  36. throw new _1.SchemaError("\"" + typeName + "\" defined in resolvers, but not in schema");
  37. }
  38. Object.keys(resolverValue).forEach(function (fieldName) {
  39. if (fieldName.startsWith('__')) {
  40. // this is for isTypeOf and resolveType and all the other stuff.
  41. type[fieldName.substring(2)] = resolverValue[fieldName];
  42. return;
  43. }
  44. if (type instanceof graphql_1.GraphQLScalarType) {
  45. type[fieldName] = resolverValue[fieldName];
  46. return;
  47. }
  48. if (type instanceof graphql_1.GraphQLEnumType) {
  49. if (!type.getValue(fieldName)) {
  50. if (allowResolversNotInSchema) {
  51. return;
  52. }
  53. throw new _1.SchemaError(typeName + "." + fieldName + " was defined in resolvers, but enum is not in schema");
  54. }
  55. // We've encountered an enum resolver that is being used to provide an
  56. // internal enum value.
  57. // Reference: https://www.apollographql.com/docs/graphql-tools/scalars.html#internal-values
  58. //
  59. // We're storing a map of the current enums external facing value to
  60. // its resolver provided internal value. This map is used to transform
  61. // the current schema to a new schema that includes enums with the new
  62. // internal value.
  63. enumValueMap[type.name] = enumValueMap[type.name] || {};
  64. enumValueMap[type.name][fieldName] = resolverValue[fieldName];
  65. return;
  66. }
  67. // object type
  68. var fields = getFieldsForType(type);
  69. if (!fields) {
  70. if (allowResolversNotInSchema) {
  71. return;
  72. }
  73. throw new _1.SchemaError(typeName + " was defined in resolvers, but it's not an object");
  74. }
  75. if (!fields[fieldName]) {
  76. if (allowResolversNotInSchema) {
  77. return;
  78. }
  79. throw new _1.SchemaError(typeName + "." + fieldName + " defined in resolvers, but not in schema");
  80. }
  81. var field = fields[fieldName];
  82. var fieldResolve = resolverValue[fieldName];
  83. if (typeof fieldResolve === 'function') {
  84. // for convenience. Allows shorter syntax in resolver definition file
  85. setFieldProperties(field, { resolve: fieldResolve });
  86. }
  87. else {
  88. if (typeof fieldResolve !== 'object') {
  89. throw new _1.SchemaError("Resolver " + typeName + "." + fieldName + " must be object or function");
  90. }
  91. setFieldProperties(field, fieldResolve);
  92. }
  93. });
  94. });
  95. _2.checkForResolveTypeResolver(schema, requireResolversForResolveType);
  96. // If there are any enum resolver functions (that are used to return
  97. // internal enum values), create a new schema that includes enums with the
  98. // new internal facing values.
  99. var updatedSchema = transforms_1.applySchemaTransforms(schema, [
  100. new ConvertEnumValues_1.default(enumValueMap),
  101. ]);
  102. return updatedSchema;
  103. }
  104. function getFieldsForType(type) {
  105. if (type instanceof graphql_1.GraphQLObjectType ||
  106. type instanceof graphql_1.GraphQLInterfaceType) {
  107. return type.getFields();
  108. }
  109. else {
  110. return undefined;
  111. }
  112. }
  113. function setFieldProperties(field, propertiesObj) {
  114. Object.keys(propertiesObj).forEach(function (propertyName) {
  115. field[propertyName] = propertiesObj[propertyName];
  116. });
  117. }
  118. exports.default = addResolveFunctionsToSchema;
  119. //# sourceMappingURL=addResolveFunctionsToSchema.js.map