Skip to content

Commit

Permalink
Merge pull request #332 from robdaemon/add_inputobject_field
Browse files Browse the repository at this point in the history
Adds an AddFieldConfig method to InputObjects
  • Loading branch information
chris-ramon authored Jul 19, 2018
2 parents 83f0349 + a05d5bb commit 086b3bf
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions definition.go
Original file line number Diff line number Diff line change
@@ -1149,6 +1149,18 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
return resultFieldMap
}

func (gt *InputObject) AddFieldConfig(fieldName string, fieldConfig *InputObjectFieldConfig) {
if fieldName == "" || fieldConfig == nil {
return
}
fieldMap, ok := gt.typeConfig.Fields.(InputObjectConfigFieldMap)
if gt.err = invariant(ok, "Cannot add field to a thunk"); gt.err != nil {
return
}
fieldMap[fieldName] = fieldConfig
gt.fields = gt.defineFieldMap()
}

func (gt *InputObject) Fields() InputObjectFieldMap {
if !gt.init {
gt.fields = gt.defineFieldMap()
25 changes: 25 additions & 0 deletions definition_test.go
Original file line number Diff line number Diff line change
@@ -641,3 +641,28 @@ func TestTypeSystem_DefinitionExampe_AllowsCyclicFieldTypes(t *testing.T) {
}

}

func TestTypeSystem_DefinitionExample_CanAddInputObjectField(t *testing.T) {
io := graphql.NewInputObject(graphql.InputObjectConfig{
Name: "inputObject",
Fields: graphql.InputObjectConfigFieldMap{
"value": &graphql.InputObjectFieldConfig{
Type: graphql.String,
},
},
})
io.AddFieldConfig("newValue", &graphql.InputObjectFieldConfig{
Type: graphql.Int,
})
fieldMap := io.Fields()

if len(fieldMap) < 2 {
t.Fatalf("Unexpected result, inputObject should have two fields, has %d", len(fieldMap))
}
if _, ok := fieldMap["value"]; !ok {
t.Fatal("Unexpected result, inputObject should have a field named 'value'")
}
if _, ok := fieldMap["newValue"]; !ok {
t.Fatal("Unexpected result, inputObject should have a field named 'newValue'")
}
}

0 comments on commit 086b3bf

Please sign in to comment.