@@ -9,6 +9,28 @@ import {
9
9
} from '../type/definition' ;
10
10
import type { GraphQLFieldMap } from '../type/definition' ;
11
11
import { GraphQLSchema } from '../type/schema' ;
12
+ import invariant from '../jsutils/invariant' ;
13
+
14
+ export const DescribedObjectType = {
15
+ FIELD : 'FIELD' ,
16
+ TYPE : 'TYPE' ,
17
+ ARGUMENT : 'ARGUMENT' ,
18
+ ENUM_VALUE : 'ENUM_VALUE' ,
19
+ } ;
20
+
21
+ export const DescriptionChangeType = {
22
+ OBJECT_ADDED : 'OBJECT_ADDED' ,
23
+ DESCRIPTION_ADDED : 'DESCRIPTION_ADDED' ,
24
+ DESCRIPTION_CHANGED : 'DESCRIPTION_CHANGED' ,
25
+ } ;
26
+
27
+ export type DescriptionChange = {
28
+ object : $Keys < typeof DescribedObjectType > ,
29
+ change : $Keys < typeof DescriptionChangeType > ,
30
+ description : string ,
31
+ oldThing : any ,
32
+ newThing : any ,
33
+ } ;
12
34
13
35
/**
14
36
* Given two schemas, returns an Array containing descriptions of any
@@ -17,37 +39,32 @@ import { GraphQLSchema } from '../type/schema';
17
39
export function findDescriptionChanges (
18
40
oldSchema : GraphQLSchema ,
19
41
newSchema : GraphQLSchema ,
20
- ) : Array < string > {
42
+ ) : Array < DescriptionChange > {
21
43
const oldTypeMap = oldSchema . getTypeMap ( ) ;
22
44
const newTypeMap = newSchema . getTypeMap ( ) ;
23
45
24
- const descriptionChanges : Array < string > = [];
46
+ const descriptionChanges : Array < ? DescriptionChange > = [ ] ;
25
47
26
48
Object . keys ( newTypeMap ) . forEach ( typeName => {
27
49
const oldType = oldTypeMap [ typeName ] ;
28
50
const newType = newTypeMap [ typeName ] ;
29
51
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
- }
52
+ descriptionChanges . push (
53
+ generateDescriptionChange ( newType , oldType , DescribedObjectType . TYPE ) ,
54
+ ) ;
45
55
46
56
if (
47
57
newType instanceof GraphQLObjectType ||
48
58
newType instanceof GraphQLInterfaceType ||
49
59
newType instanceof GraphQLInputObjectType
50
60
) {
61
+ invariant (
62
+ ! oldType ||
63
+ oldType instanceof GraphQLObjectType ||
64
+ oldType instanceof GraphQLInterfaceType ||
65
+ oldType instanceof GraphQLInputObjectType ,
66
+ 'Expected oldType to also have fields' ,
67
+ ) ;
51
68
const oldTypeFields : ?GraphQLFieldMap < * , * > = oldType
52
69
? oldType . getFields ( )
53
70
: null ;
@@ -57,21 +74,13 @@ export function findDescriptionChanges(
57
74
const oldField = oldTypeFields ? oldTypeFields [ fieldName ] : null ;
58
75
const newField = newTypeFields [ fieldName ] ;
59
76
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
- }
77
+ descriptionChanges . push (
78
+ generateDescriptionChange (
79
+ newField ,
80
+ oldField ,
81
+ DescribedObjectType . FIELD ,
82
+ ) ,
83
+ ) ;
75
84
76
85
if ( ! newField . args ) {
77
86
return ;
@@ -82,61 +91,73 @@ export function findDescriptionChanges(
82
91
? oldField . args . find ( arg => arg . name === newArg . name )
83
92
: null ;
84
93
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
- }
94
+ descriptionChanges . push (
95
+ generateDescriptionChange (
96
+ newArg ,
97
+ oldArg ,
98
+ DescribedObjectType . ARGUMENT ,
99
+ ) ,
100
+ ) ;
106
101
} ) ;
107
102
} ) ;
108
103
} else if ( newType instanceof GraphQLEnumType ) {
104
+ invariant (
105
+ ! oldType || oldType instanceof GraphQLEnumType ,
106
+ 'Expected oldType to also have values' ,
107
+ ) ;
109
108
const oldValues = oldType ? oldType . getValues ( ) : null ;
110
109
const newValues = newType . getValues ( ) ;
111
110
newValues . forEach ( newValue => {
112
111
const oldValue = oldValues
113
112
? oldValues . find ( value => value . name === newValue . name )
114
113
: null ;
115
114
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
- }
115
+ descriptionChanges . push (
116
+ generateDescriptionChange (
117
+ newValue ,
118
+ oldValue ,
119
+ DescribedObjectType . ENUM_VALUE ,
120
+ ) ,
121
+ ) ;
137
122
} ) ;
138
123
}
139
124
} ) ;
140
125
141
- return descriptionChanges ;
126
+ return descriptionChanges . filter ( Boolean ) ;
127
+ }
128
+
129
+ function generateDescriptionChange (
130
+ newThing ,
131
+ oldThing ,
132
+ objectType : $Keys < typeof DescribedObjectType > ,
133
+ ) : ?DescriptionChange {
134
+ if ( ! newThing . description ) {
135
+ return ;
136
+ }
137
+
138
+ if ( ! oldThing ) {
139
+ return {
140
+ object : objectType ,
141
+ change : DescriptionChangeType . OBJECT_ADDED ,
142
+ oldThing,
143
+ newThing,
144
+ description : `New ${ objectType } ${ newThing . name } added with description.` ,
145
+ } ;
146
+ } else if ( ! oldThing . description ) {
147
+ return {
148
+ object : objectType ,
149
+ change : DescriptionChangeType . DESCRIPTION_ADDED ,
150
+ oldThing,
151
+ newThing,
152
+ description : `Description added on ${ objectType } ${ newThing . name } .` ,
153
+ } ;
154
+ } else if ( oldThing . description !== newThing . description ) {
155
+ return {
156
+ object : objectType ,
157
+ change : DescriptionChangeType . DESCRIPTION_CHANGED ,
158
+ oldThing,
159
+ newThing,
160
+ description : `Description changed on ${ objectType } ${ newThing . name } .` ,
161
+ } ;
162
+ }
142
163
}
0 commit comments