Skip to content

Enable cyclic types in ObjectType and InputObjectType #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,15 @@ type Object struct {

type IsTypeOfFn func(value interface{}, info ResolveInfo) bool

type ObjectFieldMapThunk func() FieldConfigMap
type InterfacesThunk func() []*Interface

type ObjectConfig struct {
Name string `json:"description"`
Interfaces interface{} `json:"interfaces"`
Fields FieldConfigMap `json:"fields"`
IsTypeOf IsTypeOfFn `json:"isTypeOf"`
Description string `json:"description"`
Name string `json:"description"`
Interfaces interface{} `json:"interfaces"`
Fields interface{} `json:"fields"`
IsTypeOf IsTypeOfFn `json:"isTypeOf"`
Description string `json:"description"`
}

func NewObject(config ObjectConfig) *Object {
Expand Down Expand Up @@ -347,8 +348,10 @@ func (gt *Object) AddFieldConfig(fieldName string, fieldConfig *FieldConfig) {
if fieldName == "" || fieldConfig == nil {
return
}
gt.typeConfig.Fields[fieldName] = fieldConfig

switch gt.typeConfig.Fields.(type) {
case FieldConfigMap:
gt.typeConfig.Fields.(FieldConfigMap)[fieldName] = fieldConfig
}
}
func (gt *Object) GetName() string {
return gt.Name
Expand Down Expand Up @@ -418,19 +421,26 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
return ifaces, nil
}

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

resultFieldMap := FieldDefinitionMap{}

err := invariant(
len(fields) > 0,
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),
)
if err != nil {
return resultFieldMap, err
}

for fieldName, field := range fields {
for fieldName, field := range fieldMap {
if field == nil {
continue
}
Expand Down Expand Up @@ -1026,8 +1036,8 @@ type InputObject struct {

typeConfig InputObjectConfig
fields InputObjectFieldMap

err error
init bool
err error
}
type InputObjectFieldConfig struct {
Type Input `json:"type"`
Expand Down Expand Up @@ -1076,7 +1086,7 @@ func NewInputObject(config InputObjectConfig) *InputObject {
gt.Name = config.Name
gt.Description = config.Description
gt.typeConfig = config
gt.fields = gt.defineFieldMap()
//gt.fields = gt.defineFieldMap()
return gt
}

Expand Down Expand Up @@ -1122,9 +1132,13 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
field.DefaultValue = fieldConfig.DefaultValue
resultFieldMap[fieldName] = field
}
gt.init = true
return resultFieldMap
}
func (gt *InputObject) GetFields() InputObjectFieldMap {
if !gt.init {
gt.fields = gt.defineFieldMap()
}
return gt.fields
}
func (gt *InputObject) GetName() string {
Expand Down
22 changes: 22 additions & 0 deletions definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,25 @@ func TestTypeSystem_DefinitionExample_DoesNotMutatePassedFieldDefinitions(t *tes
}

}

func TestTypeSystem_DefinitionExampe_AllowsCyclicFieldTypes(t *testing.T) {
personType := graphql.NewObject(graphql.ObjectConfig{
Name: "Person",
Fields: (graphql.ObjectFieldMapThunk)(func() graphql.FieldConfigMap {
return graphql.FieldConfigMap{
"name": &graphql.FieldConfig{
Type: graphql.String,
},
"bestFriend": &graphql.FieldConfig{
Type: personType,
},
}
}),
})

fieldMap := personType.GetFields()
if !reflect.DeepEqual(fieldMap["name"].Type, graphql.String) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(fieldMap["bestFriend"].Type, personType))
}

}