|
| 1 | +/* eslint-disable no-restricted-syntax */ |
| 2 | +// @flow |
| 3 | + |
| 4 | +import { |
| 5 | + GraphQLInterfaceType, |
| 6 | + GraphQLObjectType, |
| 7 | + GraphQLEnumType, |
| 8 | + GraphQLInputObjectType, |
| 9 | +} from '../type/definition'; |
| 10 | +import type { GraphQLFieldMap } from '../type/definition'; |
| 11 | +import { GraphQLSchema } from '../type/schema'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Given two schemas, returns an Array containing descriptions of any |
| 15 | + * descriptions that are new or changed and need review. |
| 16 | + */ |
| 17 | +export function findDescriptionChanges( |
| 18 | + oldSchema: GraphQLSchema, |
| 19 | + newSchema: GraphQLSchema, |
| 20 | +): Array<string> { |
| 21 | + const oldTypeMap = oldSchema.getTypeMap(); |
| 22 | + const newTypeMap = newSchema.getTypeMap(); |
| 23 | + |
| 24 | + const descriptionChanges: Array<string> = []; |
| 25 | + |
| 26 | + Object.keys(newTypeMap).forEach(typeName => { |
| 27 | + const oldType = oldTypeMap[typeName]; |
| 28 | + const newType = newTypeMap[typeName]; |
| 29 | + |
| 30 | + if (newType.description) { |
| 31 | + if (!oldType) { |
| 32 | + descriptionChanges.push( |
| 33 | + `Description added on new type ${newType.name}.`, |
| 34 | + ); |
| 35 | + } else if (!oldType.description) { |
| 36 | + descriptionChanges.push(`Description added on type ${newType.name}.`); |
| 37 | + } else if (oldType.description !== newType.description) { |
| 38 | + descriptionChanges.push(`Description changed on type ${newType.name}.`); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if (oldType && !(newType instanceof oldType.constructor)) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + if ( |
| 47 | + newType instanceof GraphQLObjectType || |
| 48 | + newType instanceof GraphQLInterfaceType || |
| 49 | + newType instanceof GraphQLInputObjectType |
| 50 | + ) { |
| 51 | + const oldTypeFields: ?GraphQLFieldMap<*, *> = oldType |
| 52 | + ? oldType.getFields() |
| 53 | + : null; |
| 54 | + const newTypeFields: GraphQLFieldMap<*, *> = newType.getFields(); |
| 55 | + |
| 56 | + Object.keys(newTypeFields).forEach(fieldName => { |
| 57 | + const oldField = oldTypeFields ? oldTypeFields[fieldName] : null; |
| 58 | + const newField = newTypeFields[fieldName]; |
| 59 | + |
| 60 | + if (newField.description) { |
| 61 | + if (!oldField) { |
| 62 | + descriptionChanges.push( |
| 63 | + `Description added on new field ${newType.name}.${newField.name}`, |
| 64 | + ); |
| 65 | + } else if (!oldField.description) { |
| 66 | + descriptionChanges.push( |
| 67 | + `Description added on field ${newType.name}.${newField.name}.`, |
| 68 | + ); |
| 69 | + } else if (oldField.description !== newField.description) { |
| 70 | + descriptionChanges.push( |
| 71 | + `Description changed on field ${newType.name}.${newField.name}.`, |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (!newField.args) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + newField.args.forEach(newArg => { |
| 81 | + const oldArg = oldField |
| 82 | + ? oldField.args.find(arg => arg.name === newArg.name) |
| 83 | + : null; |
| 84 | + |
| 85 | + if (newArg.description) { |
| 86 | + if (!oldArg) { |
| 87 | + descriptionChanges.push( |
| 88 | + `Description added on new arg ${newType.name}.${ |
| 89 | + newField.name |
| 90 | + }.${newArg.name}.`, |
| 91 | + ); |
| 92 | + } else if (!oldArg.description) { |
| 93 | + descriptionChanges.push( |
| 94 | + `Description added on arg ${newType.name}.${newField.name}.${ |
| 95 | + newArg.name |
| 96 | + }.`, |
| 97 | + ); |
| 98 | + } else if (oldArg.description !== newArg.description) { |
| 99 | + descriptionChanges.push( |
| 100 | + `Description changed on arg ${newType.name}.${newField.name}.${ |
| 101 | + newArg.name |
| 102 | + }.`, |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + }); |
| 107 | + }); |
| 108 | + } else if (newType instanceof GraphQLEnumType) { |
| 109 | + const oldValues = oldType ? oldType.getValues() : null; |
| 110 | + const newValues = newType.getValues(); |
| 111 | + newValues.forEach(newValue => { |
| 112 | + const oldValue = oldValues |
| 113 | + ? oldValues.find(value => value.name === newValue.name) |
| 114 | + : null; |
| 115 | + |
| 116 | + if (newValue.description) { |
| 117 | + if (!oldValue) { |
| 118 | + descriptionChanges.push( |
| 119 | + `Description added on enum value ${newType.name}.${ |
| 120 | + newValue.name |
| 121 | + }.`, |
| 122 | + ); |
| 123 | + } else if (!oldValue.description) { |
| 124 | + descriptionChanges.push( |
| 125 | + `Description added on enum value ${newType.name}.${ |
| 126 | + newValue.name |
| 127 | + }.`, |
| 128 | + ); |
| 129 | + } else if (oldValue.description !== newValue.description) { |
| 130 | + descriptionChanges.push( |
| 131 | + `Description changed on enum value ${newType.name}.${ |
| 132 | + newValue.name |
| 133 | + }.`, |
| 134 | + ); |
| 135 | + } |
| 136 | + } |
| 137 | + }); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + return descriptionChanges; |
| 142 | +} |
0 commit comments