forEachDefaultValue.js 997 B

12345678910111213141516171819202122232425
  1. import { getNamedType, isObjectType, isInputObjectType } from 'graphql';
  2. export function forEachDefaultValue(schema, fn) {
  3. const typeMap = schema.getTypeMap();
  4. for (const typeName in typeMap) {
  5. const type = typeMap[typeName];
  6. if (!getNamedType(type).name.startsWith('__')) {
  7. if (isObjectType(type)) {
  8. const fields = type.getFields();
  9. for (const fieldName in fields) {
  10. const field = fields[fieldName];
  11. for (const arg of field.args) {
  12. arg.defaultValue = fn(arg.type, arg.defaultValue);
  13. }
  14. }
  15. }
  16. else if (isInputObjectType(type)) {
  17. const fields = type.getFields();
  18. for (const fieldName in fields) {
  19. const field = fields[fieldName];
  20. field.defaultValue = fn(field.type, field.defaultValue);
  21. }
  22. }
  23. }
  24. }
  25. }