Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
refactor(RenameTypes): simplify recursive method
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Jan 5, 2020
1 parent 1676ab0 commit 869a0fd
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions src/transforms/RenameTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type RenameOptions = {

export default class RenameTypes implements Transform {
private renamer: (name: string) => string | undefined;
private map: { [key: string]: string };
private reverseMap: { [key: string]: string };
private renameBuiltins: boolean;
private renameScalars: boolean;
Expand All @@ -27,6 +28,7 @@ export default class RenameTypes implements Transform {
options?: RenameOptions,
) {
this.renamer = renamer;
this.map = {};
this.reverseMap = {};
const { renameBuiltins = false, renameScalars = true } = options || {};
this.renameBuiltins = renameBuiltins;
Expand All @@ -42,9 +44,11 @@ export default class RenameTypes implements Transform {
if (type instanceof GraphQLScalarType && !this.renameScalars) {
return undefined;
}
const newName = this.renamer(type.name);
if (newName && newName !== type.name) {
this.reverseMap[newName] = type.name;
const oldName = type.name;
const newName = this.renamer(oldName);
if (newName && newName !== oldName) {
this.map[oldName] = type.name;
this.reverseMap[newName] = oldName;
const newType = cloneType(type);
newType.name = newName;
return newType;
Expand Down Expand Up @@ -79,42 +83,28 @@ export default class RenameTypes implements Transform {
}

public transformResult(result: Result): Result {
if (result.data) {
const data = this.renameTypes(result.data, 'data');
if (data !== result.data) {
return { ...result, data };
}
}

return result;
return {
...result,
data: this.renameTypes(result.data)
};
}

private renameTypes(value: any, name?: string) {
if (name === '__typename') {
return this.renamer(value);
}

if (value && typeof value === 'object') {
const newValue = Array.isArray(value) ? []
// Create a new object with the same prototype.
: Object.create(Object.getPrototypeOf(value));

let returnNewValue = false;

private renameTypes(value: any): any {
if (value == null) {
return value;
} else if (Array.isArray(value)) {
value.forEach((v, index) => {
value[index] = this.renameTypes(v);
});
return value;
} else if (typeof value === 'object') {
Object.keys(value).forEach(key => {
const oldChild = value[key];
const newChild = this.renameTypes(oldChild, key);
newValue[key] = newChild;
if (newChild !== oldChild) {
returnNewValue = true;
}
value[key] = key === '__typename' ?
this.renamer(value[key]) : this.renameTypes(value[key]);
});

if (returnNewValue) {
return newValue;
}
return value;
} else {
return value;
}

return value;
}
}

0 comments on commit 869a0fd

Please sign in to comment.