Skip to content

Instantly share code, notes, and snippets.

@tonis2
Created January 29, 2019 10:46
Show Gist options
  • Save tonis2/32bde0938b2c9cdbe44ab0317a7ec7fe to your computer and use it in GitHub Desktop.
Save tonis2/32bde0938b2c9cdbe44ab0317a7ec7fe to your computer and use it in GitHub Desktop.
Golang struct to graphql schema
func makeSchemaValues(Types interface{}) (graphql.Fields, graphql.FieldConfigArgument) {
fields := make(graphql.Fields)
args := make(graphql.FieldConfigArgument)
val := reflect.ValueOf(Types)
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
for i := 0; i < val.NumField(); i++ {
name := val.Type().Field(i).Name
fields[name] = &graphql.Field{
Type: graphql.String,
}
args[name] = &graphql.ArgumentConfig{
Type: graphql.String,
}
}
return fields, args
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment