Skip to content

Commit

Permalink
Merge pull request #590 from TheScenery/master
Browse files Browse the repository at this point in the history
use "invariantf" for performance
  • Loading branch information
chris-ramon authored Jan 2, 2021
2 parents ba29b76 + 0f4df50 commit 62a7bb0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
6 changes: 2 additions & 4 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,10 +800,8 @@ func completeAbstractValue(eCtx *executionContext, returnType Abstract, fieldAST
runtimeType = defaultResolveTypeFn(resolveTypeParams, returnType)
}

err := invariant(runtimeType != nil,
fmt.Sprintf(`Abstract type %v must resolve to an Object type at runtime `+
`for field %v.%v with value "%v", received "%v".`,
returnType, info.ParentType, info.FieldName, result, runtimeType),
err := invariantf(runtimeType != nil, `Abstract type %v must resolve to an Object type at runtime `+
`for field %v.%v with value "%v", received "%v".`, returnType, info.ParentType, info.FieldName, result, runtimeType,
)
if err != nil {
panic(err)
Expand Down
41 changes: 18 additions & 23 deletions schema.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package graphql

import (
"fmt"
)

type SchemaConfig struct {
Query *Object
Mutation *Object
Expand Down Expand Up @@ -403,12 +399,12 @@ func assertObjectImplementsInterface(schema *Schema, object *Object, iface *Inte

// Assert interface field type is satisfied by object field type, by being
// a valid subtype. (covariant)
err = invariant(
err = invariantf(
isTypeSubTypeOf(schema, objectField.Type, ifaceField.Type),
fmt.Sprintf(`%v.%v expects type "%v" but `+
`%v.%v expects type "%v" but `+
`%v.%v provides type "%v".`,
iface, fieldName, ifaceField.Type,
object, fieldName, objectField.Type),
iface, fieldName, ifaceField.Type,
object, fieldName, objectField.Type,
)
if err != nil {
return err
Expand All @@ -425,27 +421,26 @@ func assertObjectImplementsInterface(schema *Schema, object *Object, iface *Inte
}
}
// Assert interface field arg exists on object field.
err = invariant(
err = invariantf(
objectArg != nil,
fmt.Sprintf(`%v.%v expects argument "%v" but `+
`%v.%v expects argument "%v" but `+
`%v.%v does not provide it.`,
iface, fieldName, argName,
object, fieldName),
iface, fieldName, argName,
object, fieldName,
)
if err != nil {
return err
}

// Assert interface field arg type matches object field arg type.
// (invariant)
err = invariant(
err = invariantf(
isEqualType(ifaceArg.Type, objectArg.Type),
fmt.Sprintf(
`%v.%v(%v:) expects type "%v" `+
`but %v.%v(%v:) provides `+
`type "%v".`,
iface, fieldName, argName, ifaceArg.Type,
object, fieldName, argName, objectArg.Type),
`%v.%v(%v:) expects type "%v" `+
`but %v.%v(%v:) provides `+
`type "%v".`,
iface, fieldName, argName, ifaceArg.Type,
object, fieldName, argName, objectArg.Type,
)
if err != nil {
return err
Expand All @@ -464,12 +459,12 @@ func assertObjectImplementsInterface(schema *Schema, object *Object, iface *Inte

if ifaceArg == nil {
_, ok := objectArg.Type.(*NonNull)
err = invariant(
err = invariantf(
!ok,
fmt.Sprintf(`%v.%v(%v:) is of required type `+
`%v.%v(%v:) is of required type `+
`"%v" but is not also provided by the interface %v.%v.`,
object, fieldName, argName,
objectArg.Type, iface, fieldName),
object, fieldName, argName,
objectArg.Type, iface, fieldName,
)
if err != nil {
return err
Expand Down

0 comments on commit 62a7bb0

Please sign in to comment.