extractExtensionsFromSchema.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { mapSchema } from './mapSchema.js';
  2. import { MapperKind } from './Interfaces.js';
  3. export function extractExtensionsFromSchema(schema) {
  4. const result = {
  5. schemaExtensions: schema.extensions || {},
  6. types: {},
  7. };
  8. mapSchema(schema, {
  9. [MapperKind.OBJECT_TYPE]: type => {
  10. result.types[type.name] = { fields: {}, type: 'object', extensions: type.extensions || {} };
  11. return type;
  12. },
  13. [MapperKind.INTERFACE_TYPE]: type => {
  14. result.types[type.name] = { fields: {}, type: 'interface', extensions: type.extensions || {} };
  15. return type;
  16. },
  17. [MapperKind.FIELD]: (field, fieldName, typeName) => {
  18. result.types[typeName].fields[fieldName] = {
  19. arguments: {},
  20. extensions: field.extensions || {},
  21. };
  22. const args = field.args;
  23. if (args != null) {
  24. for (const argName in args) {
  25. result.types[typeName].fields[fieldName].arguments[argName] =
  26. args[argName].extensions || {};
  27. }
  28. }
  29. return field;
  30. },
  31. [MapperKind.ENUM_TYPE]: type => {
  32. result.types[type.name] = { values: {}, type: 'enum', extensions: type.extensions || {} };
  33. return type;
  34. },
  35. [MapperKind.ENUM_VALUE]: (value, typeName, _schema, valueName) => {
  36. result.types[typeName].values[valueName] = value.extensions || {};
  37. return value;
  38. },
  39. [MapperKind.SCALAR_TYPE]: type => {
  40. result.types[type.name] = { type: 'scalar', extensions: type.extensions || {} };
  41. return type;
  42. },
  43. [MapperKind.UNION_TYPE]: type => {
  44. result.types[type.name] = { type: 'union', extensions: type.extensions || {} };
  45. return type;
  46. },
  47. [MapperKind.INPUT_OBJECT_TYPE]: type => {
  48. result.types[type.name] = { fields: {}, type: 'input', extensions: type.extensions || {} };
  49. return type;
  50. },
  51. [MapperKind.INPUT_OBJECT_FIELD]: (field, fieldName, typeName) => {
  52. result.types[typeName].fields[fieldName] = {
  53. extensions: field.extensions || {},
  54. };
  55. return field;
  56. },
  57. });
  58. return result;
  59. }