-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
232 lines (211 loc) · 5.4 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/corganfuzz/go-gql-sqlite/pkg/model"
"github.com/graphql-go/graphql"
)
// // Tutorial comment
// type Tutorial struct {
// ID int
// Title string
// Author Author
// Comments []Comment
// }
// // Author comment
// type Author struct {
// Name string
// Tutorials []int
// }
// // Comment here
// type Comment struct {
// Body string
// }
func executeQuery(query string, schema graphql.Schema) *graphql.Result {
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})
if len(result.Errors) > 0 {
fmt.Println("wrong result, unexpected errors: %v", result.Errors)
}
return result
}
var agregateSchema = graphql.Fields{
"tutorial": model.SingleTutorialSchema(),
"list": model.ListTutorialSchema(),
}
var agregateMutations = graphql.NewObject(graphql.ObjectConfig{
Name: "Mutation",
Fields: graphql.Fields{
"create": model.CreateTutorialMutation(),
},
})
// var authorType = graphql.NewObject(
// graphql.ObjectConfig{
// Name: "Author",
// Fields: graphql.Fields{
// "Name": &graphql.Field{
// Type: graphql.String,
// },
// "Tutorials": &graphql.Field{
// Type: graphql.NewList(graphql.Int),
// },
// },
// },
// )
// var commentType = graphql.NewObject(
// graphql.ObjectConfig{
// Name: "Comment",
// Fields: graphql.Fields{
// "body": &graphql.Field{
// Type: graphql.String,
// },
// },
// },
// )
// var tutorialType = graphql.NewObject(
// graphql.ObjectConfig{
// Name: "Tutorial",
// Fields: graphql.Fields{
// "id": &graphql.Field{
// Type: graphql.Int,
// },
// "title": &graphql.Field{
// Type: graphql.String,
// },
// "author": &graphql.Field{
// Type: authorType,
// },
// "comments": &graphql.Field{
// Type: graphql.NewList(commentType),
// },
// },
// },
// )
// var mutationType = graphql.NewObject(graphql.ObjectConfig{
// Name: "Mutation",
// Fields: graphql.Fields{
// "create": &graphql.Field{
// Type: tutorialType,
// Description: "Create a new Tutorial",
// Args: graphql.FieldConfigArgument{
// "id": &graphql.ArgumentConfig{
// Type: graphql.NewNonNull(graphql.Int),
// },
// "title": &graphql.ArgumentConfig{
// Type: graphql.NewNonNull(graphql.String),
// },
// },
// Resolve: func(params graphql.ResolveParams) (interface{}, error) {
// db, err := sql.Open("sqlite3", "./tutorials.db")
// if err != nil {
// log.Fatal(err)
// }
// defer db.Close()
// stmt, err := db.Prepare("INSERT INTO tutorials VALUES (?, ?)")
// if err != nil {
// log.Fatal(err)
// }
// defer stmt.Close()
// _, err = stmt.Exec(params.Args["id"].(int), params.Args["title"].(string))
// if err != nil {
// fmt.Println(err)
// }
// var tutorial Tutorial
// err = db.QueryRow("SELECT * FROM tutorials where ID = ?", params.Args["id"].(int)).Scan(&tutorial.ID, &tutorial.Title)
// if err != nil {
// fmt.Println(err)
// }
// return tutorial, nil
// },
// },
// },
// })
func main() {
// db, err := sql.Open("sqlite3", "./tutorials.db")
// if err != nil {
// log.Fatal(err)
// }
// defer db.Close()
// // Schema
// fields := graphql.Fields{
// "tutorial": &graphql.Field{
// Type: tutorialType,
// Description: "Get Tutorial By ID",
// Args: graphql.FieldConfigArgument{
// "id": &graphql.ArgumentConfig{
// Type: graphql.Int,
// },
// },
// Resolve: func(p graphql.ResolveParams) (interface{}, error) {
// id, ok := p.Args["id"].(int)
// if ok {
// var tutorial Tutorial
// err := db.QueryRow("SELECT * FROM tutorials where ID = ?", id).Scan(&tutorial.ID, &tutorial.Title)
// if err != nil {
// fmt.Println(err)
// }
// return tutorial, nil
// }
// return nil, nil
// },
// },
// "list": &graphql.Field{
// Type: graphql.NewList(tutorialType),
// Description: "Get Tutorial List",
// Resolve: func(params graphql.ResolveParams) (interface{}, error) {
// db, err := sql.Open("sqlite3", "./tutorials.db")
// if err != nil {
// log.Fatal(err)
// }
// defer db.Close()
// // perform a db.Query insert
// var tutorials []Tutorial
// results, err := db.Query("SELECT * FROM tutorials")
// if err != nil {
// fmt.Println(err)
// }
// for results.Next() {
// var tutorial Tutorial
// err = results.Scan(&tutorial.ID, &tutorial.Title)
// if err != nil {
// fmt.Println(err)
// }
// log.Println(tutorial)
// tutorials = append(tutorials, tutorial)
// }
// return tutorials, nil
// },
// },
// }
rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: agregateSchema}
schema, err := graphql.NewSchema(
graphql.SchemaConfig{
Query: graphql.NewObject(rootQuery),
Mutation: agregateMutations,
},
)
if err != nil {
log.Fatalf("failed to create new schema, error: %v", err)
}
// Query
query := `
mutation {
create(id: 3, title: "Acid") {
id
title
}
}
`
// params := graphql.Params{Schema: schema, RequestString: query}
// r := graphql.Do(params)
// if len(r.Errors) > 0 {
// log.Fatalf("failed to execute graphql operation, errors: %+v", r.Errors)
// }
// rJSON, _ := json.Marshal(r)
result := executeQuery(query, schema)
rJSON, _ := json.Marshal(result)
fmt.Printf("%s \n", rJSON)
}