Skip to content

Commit 4c66ed6

Browse files
authored
Merge branch 'master' into benchmarks
2 parents b0a378b + 363d9c3 commit 4c66ed6

File tree

4 files changed

+129
-99
lines changed

4 files changed

+129
-99
lines changed

definition.go

Lines changed: 77 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,20 @@ func NewScalar(config ScalarConfig) *Scalar {
269269
st.PrivateName = config.Name
270270
st.PrivateDescription = config.Description
271271

272-
err = invariant(
272+
err = invariantf(
273273
config.Serialize != nil,
274-
fmt.Sprintf(`%v must provide "serialize" function. If this custom Scalar is `+
274+
`%v must provide "serialize" function. If this custom Scalar is `+
275275
`also used as an input type, ensure "parseValue" and "parseLiteral" `+
276-
`functions are also provided.`, st),
276+
`functions are also provided.`, st,
277277
)
278278
if err != nil {
279279
st.err = err
280280
return st
281281
}
282282
if config.ParseValue != nil || config.ParseLiteral != nil {
283-
err = invariant(
283+
err = invariantf(
284284
config.ParseValue != nil && config.ParseLiteral != nil,
285-
fmt.Sprintf(`%v must provide both "parseValue" and "parseLiteral" functions.`, st),
285+
`%v must provide both "parseValue" and "parseLiteral" functions.`, st,
286286
)
287287
if err != nil {
288288
st.err = err
@@ -365,9 +365,11 @@ type Object struct {
365365
PrivateDescription string `json:"description"`
366366
IsTypeOf IsTypeOfFn
367367

368-
typeConfig ObjectConfig
369-
fields FieldDefinitionMap
370-
interfaces []*Interface
368+
typeConfig ObjectConfig
369+
initialisedFields bool
370+
fields FieldDefinitionMap
371+
initialisedInterfaces bool
372+
interfaces []*Interface
371373
// Interim alternative to throwing an error during schema definition at run-time
372374
err error
373375
}
@@ -429,6 +431,7 @@ func (gt *Object) AddFieldConfig(fieldName string, fieldConfig *Field) {
429431
switch gt.typeConfig.Fields.(type) {
430432
case Fields:
431433
gt.typeConfig.Fields.(Fields)[fieldName] = fieldConfig
434+
gt.initialisedFields = false
432435
}
433436
}
434437
func (gt *Object) Name() string {
@@ -441,20 +444,30 @@ func (gt *Object) String() string {
441444
return gt.PrivateName
442445
}
443446
func (gt *Object) Fields() FieldDefinitionMap {
447+
if gt.initialisedFields {
448+
return gt.fields
449+
}
450+
444451
var configureFields Fields
445452
switch gt.typeConfig.Fields.(type) {
446453
case Fields:
447454
configureFields = gt.typeConfig.Fields.(Fields)
448455
case FieldsThunk:
449456
configureFields = gt.typeConfig.Fields.(FieldsThunk)()
450457
}
458+
451459
fields, err := defineFieldMap(gt, configureFields)
452460
gt.err = err
453461
gt.fields = fields
462+
gt.initialisedFields = true
454463
return gt.fields
455464
}
456465

457466
func (gt *Object) Interfaces() []*Interface {
467+
if gt.initialisedInterfaces {
468+
return gt.interfaces
469+
}
470+
458471
var configInterfaces []*Interface
459472
switch gt.typeConfig.Interfaces.(type) {
460473
case InterfacesThunk:
@@ -463,14 +476,18 @@ func (gt *Object) Interfaces() []*Interface {
463476
configInterfaces = gt.typeConfig.Interfaces.([]*Interface)
464477
case nil:
465478
default:
466-
gt.err = fmt.Errorf("Unknown Object.Interfaces type: %v", reflect.TypeOf(gt.typeConfig.Interfaces))
479+
gt.err = fmt.Errorf("Unknown Object.Interfaces type: %T", gt.typeConfig.Interfaces)
480+
gt.initialisedInterfaces = true
467481
return nil
468482
}
483+
469484
interfaces, err := defineInterfaces(gt, configInterfaces)
470485
gt.err = err
471486
gt.interfaces = interfaces
487+
gt.initialisedInterfaces = true
472488
return gt.interfaces
473489
}
490+
474491
func (gt *Object) Error() error {
475492
return gt.err
476493
}
@@ -482,20 +499,20 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
482499
return ifaces, nil
483500
}
484501
for _, iface := range interfaces {
485-
err := invariant(
502+
err := invariantf(
486503
iface != nil,
487-
fmt.Sprintf(`%v may only implement Interface types, it cannot implement: %v.`, ttype, iface),
504+
`%v may only implement Interface types, it cannot implement: %v.`, ttype, iface,
488505
)
489506
if err != nil {
490507
return ifaces, err
491508
}
492509
if iface.ResolveType != nil {
493-
err = invariant(
510+
err = invariantf(
494511
iface.ResolveType != nil,
495-
fmt.Sprintf(`Interface Type %v does not provide a "resolveType" function `+
512+
`Interface Type %v does not provide a "resolveType" function `+
496513
`and implementing Type %v does not provide a "isTypeOf" `+
497514
`function. There is no way to resolve this implementing type `+
498-
`during execution.`, iface, ttype),
515+
`during execution.`, iface, ttype,
499516
)
500517
if err != nil {
501518
return ifaces, err
@@ -507,20 +524,12 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
507524
return ifaces, nil
508525
}
509526

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-
}
518-
527+
func defineFieldMap(ttype Named, fieldMap Fields) (FieldDefinitionMap, error) {
519528
resultFieldMap := FieldDefinitionMap{}
520529

521-
err := invariant(
530+
err := invariantf(
522531
len(fieldMap) > 0,
523-
fmt.Sprintf(`%v fields must be an object with field names as keys or a function which return such an object.`, ttype),
532+
`%v fields must be an object with field names as keys or a function which return such an object.`, ttype,
524533
)
525534
if err != nil {
526535
return resultFieldMap, err
@@ -530,9 +539,9 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)
530539
if field == nil {
531540
continue
532541
}
533-
err = invariant(
542+
err = invariantf(
534543
field.Type != nil,
535-
fmt.Sprintf(`%v.%v field type must be Output Type but got: %v.`, ttype, fieldName, field.Type),
544+
`%v.%v field type must be Output Type but got: %v.`, ttype, fieldName, field.Type,
536545
)
537546
if err != nil {
538547
return resultFieldMap, err
@@ -558,16 +567,16 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)
558567
if err != nil {
559568
return resultFieldMap, err
560569
}
561-
err = invariant(
570+
err = invariantf(
562571
arg != nil,
563-
fmt.Sprintf(`%v.%v args must be an object with argument names as keys.`, ttype, fieldName),
572+
`%v.%v args must be an object with argument names as keys.`, ttype, fieldName,
564573
)
565574
if err != nil {
566575
return resultFieldMap, err
567576
}
568-
err = invariant(
577+
err = invariantf(
569578
arg.Type != nil,
570-
fmt.Sprintf(`%v.%v(%v:) argument type must be Input Type but got: %v.`, ttype, fieldName, argName, arg.Type),
579+
`%v.%v(%v:) argument type must be Input Type but got: %v.`, ttype, fieldName, argName, arg.Type,
571580
)
572581
if err != nil {
573582
return resultFieldMap, err
@@ -695,9 +704,10 @@ type Interface struct {
695704
PrivateDescription string `json:"description"`
696705
ResolveType ResolveTypeFn
697706

698-
typeConfig InterfaceConfig
699-
fields FieldDefinitionMap
700-
err error
707+
typeConfig InterfaceConfig
708+
initialisedFields bool
709+
fields FieldDefinitionMap
710+
err error
701711
}
702712
type InterfaceConfig struct {
703713
Name string `json:"name"`
@@ -751,30 +761,42 @@ func (it *Interface) AddFieldConfig(fieldName string, fieldConfig *Field) {
751761
switch it.typeConfig.Fields.(type) {
752762
case Fields:
753763
it.typeConfig.Fields.(Fields)[fieldName] = fieldConfig
764+
it.initialisedFields = false
754765
}
755766
}
767+
756768
func (it *Interface) Name() string {
757769
return it.PrivateName
758770
}
771+
759772
func (it *Interface) Description() string {
760773
return it.PrivateDescription
761774
}
775+
762776
func (it *Interface) Fields() (fields FieldDefinitionMap) {
777+
if it.initialisedFields {
778+
return it.fields
779+
}
780+
763781
var configureFields Fields
764782
switch it.typeConfig.Fields.(type) {
765783
case Fields:
766784
configureFields = it.typeConfig.Fields.(Fields)
767785
case FieldsThunk:
768786
configureFields = it.typeConfig.Fields.(FieldsThunk)()
769787
}
788+
770789
fields, err := defineFieldMap(it, configureFields)
771790
it.err = err
772791
it.fields = fields
792+
it.initialisedFields = true
773793
return it.fields
774794
}
795+
775796
func (it *Interface) String() string {
776797
return it.PrivateName
777798
}
799+
778800
func (it *Interface) Error() error {
779801
return it.err
780802
}
@@ -834,30 +856,30 @@ func NewUnion(config UnionConfig) *Union {
834856
objectType.PrivateDescription = config.Description
835857
objectType.ResolveType = config.ResolveType
836858

837-
err = invariant(
859+
err = invariantf(
838860
len(config.Types) > 0,
839-
fmt.Sprintf(`Must provide Array of types for Union %v.`, config.Name),
861+
`Must provide Array of types for Union %v.`, config.Name,
840862
)
841863
if err != nil {
842864
objectType.err = err
843865
return objectType
844866
}
845867
for _, ttype := range config.Types {
846-
err := invariant(
868+
err := invariantf(
847869
ttype != nil,
848-
fmt.Sprintf(`%v may only contain Object types, it cannot contain: %v.`, objectType, ttype),
870+
`%v may only contain Object types, it cannot contain: %v.`, objectType, ttype,
849871
)
850872
if err != nil {
851873
objectType.err = err
852874
return objectType
853875
}
854876
if objectType.ResolveType == nil {
855-
err = invariant(
877+
err = invariantf(
856878
ttype.IsTypeOf != nil,
857-
fmt.Sprintf(`Union Type %v does not provide a "resolveType" function `+
879+
`Union Type %v does not provide a "resolveType" function `+
858880
`and possible Type %v does not provide a "isTypeOf" `+
859881
`function. There is no way to resolve this possible type `+
860-
`during execution.`, objectType, ttype),
882+
`during execution.`, objectType, ttype,
861883
)
862884
if err != nil {
863885
objectType.err = err
@@ -958,19 +980,19 @@ func NewEnum(config EnumConfig) *Enum {
958980
func (gt *Enum) defineEnumValues(valueMap EnumValueConfigMap) ([]*EnumValueDefinition, error) {
959981
values := []*EnumValueDefinition{}
960982

961-
err := invariant(
983+
err := invariantf(
962984
len(valueMap) > 0,
963-
fmt.Sprintf(`%v values must be an object with value names as keys.`, gt),
985+
`%v values must be an object with value names as keys.`, gt,
964986
)
965987
if err != nil {
966988
return values, err
967989
}
968990

969991
for valueName, valueConfig := range valueMap {
970-
err := invariant(
992+
err := invariantf(
971993
valueConfig != nil,
972-
fmt.Sprintf(`%v.%v must refer to an object with a "value" key `+
973-
`representing an internal value but got: %v.`, gt, valueName, valueConfig),
994+
`%v.%v must refer to an object with a "value" key `+
995+
`representing an internal value but got: %v.`, gt, valueName, valueConfig,
974996
)
975997
if err != nil {
976998
return values, err
@@ -1151,9 +1173,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
11511173
}
11521174
resultFieldMap := InputObjectFieldMap{}
11531175

1154-
err := invariant(
1176+
err := invariantf(
11551177
len(fieldMap) > 0,
1156-
fmt.Sprintf(`%v fields must be an object with field names as keys or a function which return such an object.`, gt),
1178+
`%v fields must be an object with field names as keys or a function which return such an object.`, gt,
11571179
)
11581180
if err != nil {
11591181
gt.err = err
@@ -1168,9 +1190,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
11681190
if err != nil {
11691191
continue
11701192
}
1171-
err = invariant(
1193+
err = invariantf(
11721194
fieldConfig.Type != nil,
1173-
fmt.Sprintf(`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type),
1195+
`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type,
11741196
)
11751197
if err != nil {
11761198
gt.err = err
@@ -1231,7 +1253,7 @@ type List struct {
12311253
func NewList(ofType Type) *List {
12321254
gl := &List{}
12331255

1234-
err := invariant(ofType != nil, fmt.Sprintf(`Can only create List of a Type but got: %v.`, ofType))
1256+
err := invariantf(ofType != nil, `Can only create List of a Type but got: %v.`, ofType)
12351257
if err != nil {
12361258
gl.err = err
12371259
return gl
@@ -1284,7 +1306,7 @@ func NewNonNull(ofType Type) *NonNull {
12841306
gl := &NonNull{}
12851307

12861308
_, isOfTypeNonNull := ofType.(*NonNull)
1287-
err := invariant(ofType != nil && !isOfTypeNonNull, fmt.Sprintf(`Can only create NonNull of a Nullable Type but got: %v.`, ofType))
1309+
err := invariantf(ofType != nil && !isOfTypeNonNull, `Can only create NonNull of a Nullable Type but got: %v.`, ofType)
12881310
if err != nil {
12891311
gl.err = err
12901312
return gl
@@ -1311,8 +1333,8 @@ func (gl *NonNull) Error() error {
13111333
var NameRegExp, _ = regexp.Compile("^[_a-zA-Z][_a-zA-Z0-9]*$")
13121334

13131335
func assertValidName(name string) error {
1314-
return invariant(
1336+
return invariantf(
13151337
NameRegExp.MatchString(name),
1316-
fmt.Sprintf(`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%v" does not.`, name),
1317-
)
1338+
`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%v" does not.`, name)
1339+
13181340
}

0 commit comments

Comments
 (0)