Skip to content

Commit

Permalink
Merge branch 'master' into benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ramon committed Jan 7, 2018
2 parents b0a378b + 363d9c3 commit 4c66ed6
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 99 deletions.
132 changes: 77 additions & 55 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,20 @@ func NewScalar(config ScalarConfig) *Scalar {
st.PrivateName = config.Name
st.PrivateDescription = config.Description

err = invariant(
err = invariantf(
config.Serialize != nil,
fmt.Sprintf(`%v must provide "serialize" function. If this custom Scalar is `+
`%v must provide "serialize" function. If this custom Scalar is `+
`also used as an input type, ensure "parseValue" and "parseLiteral" `+
`functions are also provided.`, st),
`functions are also provided.`, st,
)
if err != nil {
st.err = err
return st
}
if config.ParseValue != nil || config.ParseLiteral != nil {
err = invariant(
err = invariantf(
config.ParseValue != nil && config.ParseLiteral != nil,
fmt.Sprintf(`%v must provide both "parseValue" and "parseLiteral" functions.`, st),
`%v must provide both "parseValue" and "parseLiteral" functions.`, st,
)
if err != nil {
st.err = err
Expand Down Expand Up @@ -365,9 +365,11 @@ type Object struct {
PrivateDescription string `json:"description"`
IsTypeOf IsTypeOfFn

typeConfig ObjectConfig
fields FieldDefinitionMap
interfaces []*Interface
typeConfig ObjectConfig
initialisedFields bool
fields FieldDefinitionMap
initialisedInterfaces bool
interfaces []*Interface
// Interim alternative to throwing an error during schema definition at run-time
err error
}
Expand Down Expand Up @@ -429,6 +431,7 @@ func (gt *Object) AddFieldConfig(fieldName string, fieldConfig *Field) {
switch gt.typeConfig.Fields.(type) {
case Fields:
gt.typeConfig.Fields.(Fields)[fieldName] = fieldConfig
gt.initialisedFields = false
}
}
func (gt *Object) Name() string {
Expand All @@ -441,20 +444,30 @@ func (gt *Object) String() string {
return gt.PrivateName
}
func (gt *Object) Fields() FieldDefinitionMap {
if gt.initialisedFields {
return gt.fields
}

var configureFields Fields
switch gt.typeConfig.Fields.(type) {
case Fields:
configureFields = gt.typeConfig.Fields.(Fields)
case FieldsThunk:
configureFields = gt.typeConfig.Fields.(FieldsThunk)()
}

fields, err := defineFieldMap(gt, configureFields)
gt.err = err
gt.fields = fields
gt.initialisedFields = true
return gt.fields
}

func (gt *Object) Interfaces() []*Interface {
if gt.initialisedInterfaces {
return gt.interfaces
}

var configInterfaces []*Interface
switch gt.typeConfig.Interfaces.(type) {
case InterfacesThunk:
Expand All @@ -463,14 +476,18 @@ func (gt *Object) Interfaces() []*Interface {
configInterfaces = gt.typeConfig.Interfaces.([]*Interface)
case nil:
default:
gt.err = fmt.Errorf("Unknown Object.Interfaces type: %v", reflect.TypeOf(gt.typeConfig.Interfaces))
gt.err = fmt.Errorf("Unknown Object.Interfaces type: %T", gt.typeConfig.Interfaces)
gt.initialisedInterfaces = true
return nil
}

interfaces, err := defineInterfaces(gt, configInterfaces)
gt.err = err
gt.interfaces = interfaces
gt.initialisedInterfaces = true
return gt.interfaces
}

func (gt *Object) Error() error {
return gt.err
}
Expand All @@ -482,20 +499,20 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
return ifaces, nil
}
for _, iface := range interfaces {
err := invariant(
err := invariantf(
iface != nil,
fmt.Sprintf(`%v may only implement Interface types, it cannot implement: %v.`, ttype, iface),
`%v may only implement Interface types, it cannot implement: %v.`, ttype, iface,
)
if err != nil {
return ifaces, err
}
if iface.ResolveType != nil {
err = invariant(
err = invariantf(
iface.ResolveType != nil,
fmt.Sprintf(`Interface Type %v does not provide a "resolveType" function `+
`Interface Type %v does not provide a "resolveType" function `+
`and implementing Type %v does not provide a "isTypeOf" `+
`function. There is no way to resolve this implementing type `+
`during execution.`, iface, ttype),
`during execution.`, iface, ttype,
)
if err != nil {
return ifaces, err
Expand All @@ -507,20 +524,12 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
return ifaces, nil
}

func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error) {
var fieldMap Fields
switch fields.(type) {
case Fields:
fieldMap = fields.(Fields)
case FieldsThunk:
fieldMap = fields.(FieldsThunk)()
}

func defineFieldMap(ttype Named, fieldMap Fields) (FieldDefinitionMap, error) {
resultFieldMap := FieldDefinitionMap{}

err := invariant(
err := invariantf(
len(fieldMap) > 0,
fmt.Sprintf(`%v fields must be an object with field names as keys or a function which return such an object.`, ttype),
`%v fields must be an object with field names as keys or a function which return such an object.`, ttype,
)
if err != nil {
return resultFieldMap, err
Expand All @@ -530,9 +539,9 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)
if field == nil {
continue
}
err = invariant(
err = invariantf(
field.Type != nil,
fmt.Sprintf(`%v.%v field type must be Output Type but got: %v.`, ttype, fieldName, field.Type),
`%v.%v field type must be Output Type but got: %v.`, ttype, fieldName, field.Type,
)
if err != nil {
return resultFieldMap, err
Expand All @@ -558,16 +567,16 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)
if err != nil {
return resultFieldMap, err
}
err = invariant(
err = invariantf(
arg != nil,
fmt.Sprintf(`%v.%v args must be an object with argument names as keys.`, ttype, fieldName),
`%v.%v args must be an object with argument names as keys.`, ttype, fieldName,
)
if err != nil {
return resultFieldMap, err
}
err = invariant(
err = invariantf(
arg.Type != nil,
fmt.Sprintf(`%v.%v(%v:) argument type must be Input Type but got: %v.`, ttype, fieldName, argName, arg.Type),
`%v.%v(%v:) argument type must be Input Type but got: %v.`, ttype, fieldName, argName, arg.Type,
)
if err != nil {
return resultFieldMap, err
Expand Down Expand Up @@ -695,9 +704,10 @@ type Interface struct {
PrivateDescription string `json:"description"`
ResolveType ResolveTypeFn

typeConfig InterfaceConfig
fields FieldDefinitionMap
err error
typeConfig InterfaceConfig
initialisedFields bool
fields FieldDefinitionMap
err error
}
type InterfaceConfig struct {
Name string `json:"name"`
Expand Down Expand Up @@ -751,30 +761,42 @@ func (it *Interface) AddFieldConfig(fieldName string, fieldConfig *Field) {
switch it.typeConfig.Fields.(type) {
case Fields:
it.typeConfig.Fields.(Fields)[fieldName] = fieldConfig
it.initialisedFields = false
}
}

func (it *Interface) Name() string {
return it.PrivateName
}

func (it *Interface) Description() string {
return it.PrivateDescription
}

func (it *Interface) Fields() (fields FieldDefinitionMap) {
if it.initialisedFields {
return it.fields
}

var configureFields Fields
switch it.typeConfig.Fields.(type) {
case Fields:
configureFields = it.typeConfig.Fields.(Fields)
case FieldsThunk:
configureFields = it.typeConfig.Fields.(FieldsThunk)()
}

fields, err := defineFieldMap(it, configureFields)
it.err = err
it.fields = fields
it.initialisedFields = true
return it.fields
}

func (it *Interface) String() string {
return it.PrivateName
}

func (it *Interface) Error() error {
return it.err
}
Expand Down Expand Up @@ -834,30 +856,30 @@ func NewUnion(config UnionConfig) *Union {
objectType.PrivateDescription = config.Description
objectType.ResolveType = config.ResolveType

err = invariant(
err = invariantf(
len(config.Types) > 0,
fmt.Sprintf(`Must provide Array of types for Union %v.`, config.Name),
`Must provide Array of types for Union %v.`, config.Name,
)
if err != nil {
objectType.err = err
return objectType
}
for _, ttype := range config.Types {
err := invariant(
err := invariantf(
ttype != nil,
fmt.Sprintf(`%v may only contain Object types, it cannot contain: %v.`, objectType, ttype),
`%v may only contain Object types, it cannot contain: %v.`, objectType, ttype,
)
if err != nil {
objectType.err = err
return objectType
}
if objectType.ResolveType == nil {
err = invariant(
err = invariantf(
ttype.IsTypeOf != nil,
fmt.Sprintf(`Union Type %v does not provide a "resolveType" function `+
`Union Type %v does not provide a "resolveType" function `+
`and possible Type %v does not provide a "isTypeOf" `+
`function. There is no way to resolve this possible type `+
`during execution.`, objectType, ttype),
`during execution.`, objectType, ttype,
)
if err != nil {
objectType.err = err
Expand Down Expand Up @@ -958,19 +980,19 @@ func NewEnum(config EnumConfig) *Enum {
func (gt *Enum) defineEnumValues(valueMap EnumValueConfigMap) ([]*EnumValueDefinition, error) {
values := []*EnumValueDefinition{}

err := invariant(
err := invariantf(
len(valueMap) > 0,
fmt.Sprintf(`%v values must be an object with value names as keys.`, gt),
`%v values must be an object with value names as keys.`, gt,
)
if err != nil {
return values, err
}

for valueName, valueConfig := range valueMap {
err := invariant(
err := invariantf(
valueConfig != nil,
fmt.Sprintf(`%v.%v must refer to an object with a "value" key `+
`representing an internal value but got: %v.`, gt, valueName, valueConfig),
`%v.%v must refer to an object with a "value" key `+
`representing an internal value but got: %v.`, gt, valueName, valueConfig,
)
if err != nil {
return values, err
Expand Down Expand Up @@ -1151,9 +1173,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
}
resultFieldMap := InputObjectFieldMap{}

err := invariant(
err := invariantf(
len(fieldMap) > 0,
fmt.Sprintf(`%v fields must be an object with field names as keys or a function which return such an object.`, gt),
`%v fields must be an object with field names as keys or a function which return such an object.`, gt,
)
if err != nil {
gt.err = err
Expand All @@ -1168,9 +1190,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
if err != nil {
continue
}
err = invariant(
err = invariantf(
fieldConfig.Type != nil,
fmt.Sprintf(`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type),
`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type,
)
if err != nil {
gt.err = err
Expand Down Expand Up @@ -1231,7 +1253,7 @@ type List struct {
func NewList(ofType Type) *List {
gl := &List{}

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

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

func assertValidName(name string) error {
return invariant(
return invariantf(
NameRegExp.MatchString(name),
fmt.Sprintf(`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%v" does not.`, name),
)
`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%v" does not.`, name)

}

0 comments on commit 4c66ed6

Please sign in to comment.