-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
133 lines (113 loc) · 2.97 KB
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package models
import "github.com/graphql-go/graphql"
/*
* This file contains the model of tag
*/
//Tag is tag to for indexing purpose
type Tag struct {
//ID of the Tag
ID uint `gorm:"primary_key"`
//Name of the tag
Name string
}
//TagConfig is the object config for tag
var TagConfig = graphql.ObjectConfig{
Name: "Tag",
Fields: graphql.Fields{
"ID": &graphql.Field{
Type: graphql.Int,
},
"Name": &graphql.Field{
Type: graphql.String,
},
},
}
//TagSchema is the schema of the Tag model
var TagSchema = graphql.NewObject(TagConfig)
//ReadTagResolve is the resolve function for both read tag/tags
var ReadTagResolve = func(params graphql.ResolveParams) (interface{}, error) {
// marshall and cast the argument value
id, ok := params.Args["ID"]
tag := Tag{}
if !ok {
//id is not given
var tags []Tag
DB.Find(&tags)
return tags, nil
}
//finding the Tag from the db
DB.First(&tag, uint(id.(int)))
// return the new Tag object that we supposedly save to DB
return tag, nil
}
//ReadTag will read a Tag
var ReadTag = &graphql.Field{
Type: TagSchema, // the return type for this field
Description: "Get a single Tag",
Args: graphql.FieldConfigArgument{
"ID": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Resolve: ReadTagResolve,
}
//ReadTags will read a Tag
var ReadTags = &graphql.Field{
Type: graphql.NewList(TagSchema), // the return type for this field
Description: "Get a single Tag",
Args: graphql.FieldConfigArgument{
"ID": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Resolve: ReadTagResolve,
}
//CreateTag for creating a Tag
var CreateTag = &graphql.Field{
Type: TagSchema, // the return type for this field
Description: "Create new Tag",
Args: graphql.FieldConfigArgument{
"Name": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
// marshall and cast the argument value
name, _ := params.Args["Name"].(string)
// perform mutation operation here
// for e.g. create a Tag and save to DB.
newTag := Tag{
Name: name,
}
DB.Create(&newTag)
// return the new Tag object that we supposedly save to DB
return newTag, nil
},
}
//UpdateTag for creating a Tag
var UpdateTag = &graphql.Field{
Type: TagSchema, // the return type for this field
Description: "Update existing Tag",
Args: graphql.FieldConfigArgument{
"ID": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.Int),
},
"Name": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
// marshall and cast the argument value
id, _ := params.Args["ID"].(int)
name, _ := params.Args["Name"].(string)
// perform mutation operation here
// for e.g. create a Tag and save to DB.
tag := Tag{
ID: uint(id),
Name: name,
}
DB.Update(&tag)
// return the new Tag object that we supposedly save to DB
return tag, nil
},
}