Skip to content

Commit bc95f52

Browse files
authored
fix: Handle adding of fields to thunk-fields (#4)
1 parent 2fe60b2 commit bc95f52

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

definition.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,11 +1206,27 @@ func (gt *InputObject) AddFieldConfig(fieldName string, fieldConfig *InputObject
12061206
if fieldName == "" || fieldConfig == nil {
12071207
return
12081208
}
1209-
fieldMap, ok := gt.typeConfig.Fields.(InputObjectConfigFieldMap)
1210-
if gt.err = invariant(ok, "Cannot add field to a thunk"); gt.err != nil {
1211-
return
1209+
1210+
switch fields := gt.typeConfig.Fields.(type) {
1211+
case InputObjectConfigFieldMap:
1212+
// If the fields were defined synchronously (not a thunk),
1213+
// we just add to the map
1214+
fields[fieldName] = fieldConfig
1215+
case InputObjectConfigFieldMapThunk:
1216+
// If the fields were defined using a thunk, we wrap the
1217+
// original thunk behind a new thunk which appends the new
1218+
// field
1219+
var newThunk InputObjectConfigFieldMapThunk = func() (InputObjectConfigFieldMap, error) {
1220+
oldFieldMap, err := fields()
1221+
if err != nil {
1222+
return nil, err
1223+
}
1224+
oldFieldMap[fieldName] = fieldConfig
1225+
return oldFieldMap, nil
1226+
}
1227+
gt.typeConfig.Fields = newThunk
12121228
}
1213-
fieldMap[fieldName] = fieldConfig
1229+
12141230
gt.fields = gt.defineFieldMap()
12151231
}
12161232

0 commit comments

Comments
 (0)