Skip to content
Snippets Groups Projects
Commit 304cbd87 authored by Simao Gomes Viana's avatar Simao Gomes Viana :rainbow:
Browse files

Improve naming, unexport some fields, add documentation

parent fd81936c
No related branches found
Tags v0.0.3
No related merge requests found
......@@ -21,7 +21,7 @@ func FieldConfigArgs(gqlType *graphql.Object) *graphql.FieldConfigArgument {
}
func QueryOne(model interface{}, db *pg.DB) *graphql.Field {
objType := GetObjType(model)
objType := GetGraphQLObjType(model)
return &graphql.Field{
Type: objType,
Description: fmt.Sprintf("Get one %s", strings.ToLower(objType.Name())),
......@@ -31,7 +31,7 @@ func QueryOne(model interface{}, db *pg.DB) *graphql.Field {
}
func QueryList(model interface{}, db *pg.DB) *graphql.Field {
objType := GetObjType(model)
objType := GetGraphQLObjType(model)
return &graphql.Field{
Type: graphql.NewList(objType),
Description: fmt.Sprintf("Get list of %ss", strings.ToLower(objType.Name())),
......@@ -51,7 +51,7 @@ func GenerateQueryFields(db *pg.DB, models ...interface{}) *graphql.Fields {
}
func CreateOne(model interface{}, db *pg.DB) *graphql.Field {
objType := GetObjType(model)
objType := GetGraphQLObjType(model)
return &graphql.Field{
Type: objType,
Description: fmt.Sprintf("Create one %s", strings.ToLower(objType.Name())),
......@@ -61,7 +61,7 @@ func CreateOne(model interface{}, db *pg.DB) *graphql.Field {
}
func UpdateOne(model interface{}, db *pg.DB) *graphql.Field {
objType := GetObjType(model)
objType := GetGraphQLObjType(model)
return &graphql.Field{
Type: objType,
Description: fmt.Sprintf("Update one %s", strings.ToLower(objType.Name())),
......@@ -71,7 +71,7 @@ func UpdateOne(model interface{}, db *pg.DB) *graphql.Field {
}
func DeleteOne(model interface{}, db *pg.DB) *graphql.Field {
objType := GetObjType(model)
objType := GetGraphQLObjType(model)
return &graphql.Field{
Type: objType,
Description: fmt.Sprintf("Delete one %s", strings.ToLower(objType.Name())),
......
......@@ -8,20 +8,28 @@ import (
"github.com/xdevs23/straf"
)
var objTypeCache = map[string]*graphql.Object{}
var gqlObjTypeCache = map[string]*graphql.Object{}
func GetObjType(model interface{}) *graphql.Object {
// Generates a GraphQL object based on a model.
// Uses a cache to speed up subsequent calls and
// make sure there are no duplicates. The cache is
// very important because otherwise GraphQL will
// spit out weird errors as it expects every type
// to only exist once and gets easily confused.
func GetGraphQLObjType(model interface{}) *graphql.Object {
tyName := GetModelName(model)
if cacheType := objTypeCache[tyName]; cacheType != nil {
if cacheType := gqlObjTypeCache[tyName]; cacheType != nil {
return cacheType
}
objType, _ := straf.GetGraphQLObject(*CreateNewForType(model))
objType = PreprocessType(objType)
objTypeCache[tyName] = objType
objType = preprocessType(objType)
gqlObjTypeCache[tyName] = objType
return objType
}
func PreprocessType(gqlType *graphql.Object) *graphql.Object {
// Does some pre-processing on the specified GraphQL object.
// This does things like turning all field names lower case.
func preprocessType(gqlType *graphql.Object) *graphql.Object {
for name, field := range gqlType.Fields() {
lowerCaseName := strings.ToLower(name)
delete(gqlType.Fields(), name)
......@@ -31,6 +39,8 @@ func PreprocessType(gqlType *graphql.Object) *graphql.Object {
return gqlType
}
// Makes sure that the specified model fulfills all requirements
// and returns the reflection type of the specified model
func prepareAutoResolve(model interface{}) *reflect.Type {
modelType := reflect.TypeOf(model).Elem()
if modelType.Kind() != reflect.Struct {
......@@ -39,6 +49,8 @@ func prepareAutoResolve(model interface{}) *reflect.Type {
return &modelType
}
// Reads out the GraphQL arguments and writes them into
// a new object. Consider this as a kind of deserialization.
func ConstructObjectFromPArgs(modelType reflect.Type, p *graphql.ResolveParams) *interface{} {
obj := NewFromType(modelType)
for i := 0; i < modelType.NumField(); i++ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment