Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
G
goqlorm
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
6
Snippets
Groups
Projects
Show more breadcrumbs
Simao Gomes Viana
goqlorm
Commits
304cbd87
Commit
304cbd87
authored
5 years ago
by
Simao Gomes Viana
Browse files
Options
Downloads
Patches
Plain Diff
Improve naming, unexport some fields, add documentation
parent
fd81936c
No related branches found
Branches containing commit
Tags
v0.0.3
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
generator/field_generator.go
+5
-5
5 additions, 5 deletions
generator/field_generator.go
generator/helpers.go
+18
-6
18 additions, 6 deletions
generator/helpers.go
with
23 additions
and
11 deletions
generator/field_generator.go
+
5
−
5
View file @
304cbd87
...
...
@@ -21,7 +21,7 @@ func FieldConfigArgs(gqlType *graphql.Object) *graphql.FieldConfigArgument {
}
func
QueryOne
(
model
interface
{},
db
*
pg
.
DB
)
*
graphql
.
Field
{
objType
:=
GetObjType
(
model
)
objType
:=
Get
GraphQL
ObjType
(
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
:=
Get
GraphQL
ObjType
(
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
:=
Get
GraphQL
ObjType
(
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
:=
Get
GraphQL
ObjType
(
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
:=
Get
GraphQL
ObjType
(
model
)
return
&
graphql
.
Field
{
Type
:
objType
,
Description
:
fmt
.
Sprintf
(
"Delete one %s"
,
strings
.
ToLower
(
objType
.
Name
())),
...
...
This diff is collapsed.
Click to expand it.
generator/helpers.go
+
18
−
6
View file @
304cbd87
...
...
@@ -8,20 +8,28 @@ import (
"github.com/xdevs23/straf"
)
var
o
bjTypeCache
=
map
[
string
]
*
graphql
.
Object
{}
var
gqlO
bjTypeCache
=
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
:=
o
bjTypeCache
[
tyName
];
cacheType
!=
nil
{
if
cacheType
:=
gqlO
bjTypeCache
[
tyName
];
cacheType
!=
nil
{
return
cacheType
}
objType
,
_
:=
straf
.
GetGraphQLObject
(
*
CreateNewForType
(
model
))
objType
=
P
reprocessType
(
objType
)
o
bjTypeCache
[
tyName
]
=
objType
objType
=
p
reprocessType
(
objType
)
gqlO
bjTypeCache
[
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
++
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment