@@ -398,6 +398,7 @@ type ObjectConfig struct {
398
398
IsTypeOf IsTypeOfFn `json:"isTypeOf"`
399
399
Description string `json:"description"`
400
400
}
401
+
401
402
type FieldsThunk func () Fields
402
403
403
404
func NewObject (config ObjectConfig ) * Object {
@@ -506,19 +507,26 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
506
507
return ifaces , nil
507
508
}
508
509
509
- func defineFieldMap (ttype Named , fields Fields ) (FieldDefinitionMap , error ) {
510
+ func defineFieldMap (ttype Named , fields interface {}) (FieldDefinitionMap , error ) {
511
+ var fieldMap Fields
512
+ switch fields .(type ) {
513
+ case Fields :
514
+ fieldMap = fields .(Fields )
515
+ case FieldsThunk :
516
+ fieldMap = fields .(FieldsThunk )()
517
+ }
510
518
511
519
resultFieldMap := FieldDefinitionMap {}
512
520
513
521
err := invariant (
514
- len (fields ) > 0 ,
522
+ len (fieldMap ) > 0 ,
515
523
fmt .Sprintf (`%v fields must be an object with field names as keys or a function which return such an object.` , ttype ),
516
524
)
517
525
if err != nil {
518
526
return resultFieldMap , err
519
527
}
520
528
521
- for fieldName , field := range fields {
529
+ for fieldName , field := range fieldMap {
522
530
if field == nil {
523
531
continue
524
532
}
@@ -1080,8 +1088,8 @@ type InputObject struct {
1080
1088
1081
1089
typeConfig InputObjectConfig
1082
1090
fields InputObjectFieldMap
1083
-
1084
- err error
1091
+ init bool
1092
+ err error
1085
1093
}
1086
1094
type InputObjectFieldConfig struct {
1087
1095
Type Input `json:"type"`
@@ -1129,7 +1137,7 @@ func NewInputObject(config InputObjectConfig) *InputObject {
1129
1137
gt .PrivateName = config .Name
1130
1138
gt .PrivateDescription = config .Description
1131
1139
gt .typeConfig = config
1132
- gt .fields = gt .defineFieldMap ()
1140
+ // gt.fields = gt.defineFieldMap()
1133
1141
return gt
1134
1142
}
1135
1143
@@ -1175,9 +1183,14 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
1175
1183
field .DefaultValue = fieldConfig .DefaultValue
1176
1184
resultFieldMap [fieldName ] = field
1177
1185
}
1186
+ gt .init = true
1178
1187
return resultFieldMap
1179
1188
}
1189
+
1180
1190
func (gt * InputObject ) Fields () InputObjectFieldMap {
1191
+ if ! gt .init {
1192
+ gt .fields = gt .defineFieldMap ()
1193
+ }
1181
1194
return gt .fields
1182
1195
}
1183
1196
func (gt * InputObject ) Name () string {
0 commit comments