@@ -8,192 +8,18 @@ import (
88 "time"
99
1010 "github.com/graphql-go/graphql"
11+ "github.com/graphql-go/graphql/examples/todo/schema"
1112)
1213
13- type Todo struct {
14- ID string `json:"id"`
15- Text string `json:"text"`
16- Done bool `json:"done"`
17- }
18-
19- var TodoList []Todo
20- var letterRunes = []rune ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" )
21-
22- func RandStringRunes (n int ) string {
23- b := make ([]rune , n )
24- for i := range b {
25- b [i ] = letterRunes [rand .Intn (len (letterRunes ))]
26- }
27- return string (b )
28- }
29-
3014func init () {
31- todo1 := Todo {ID : "a" , Text : "A todo not to forget" , Done : false }
32- todo2 := Todo {ID : "b" , Text : "This is the most important" , Done : false }
33- todo3 := Todo {ID : "c" , Text : "Please do this or else" , Done : false }
34- TodoList = append (TodoList , todo1 , todo2 , todo3 )
15+ todo1 := schema. Todo {ID : "a" , Text : "A todo not to forget" , Done : false }
16+ todo2 := schema. Todo {ID : "b" , Text : "This is the most important" , Done : false }
17+ todo3 := schema. Todo {ID : "c" , Text : "Please do this or else" , Done : false }
18+ schema . TodoList = append (schema . TodoList , todo1 , todo2 , todo3 )
3519
3620 rand .Seed (time .Now ().UnixNano ())
3721}
3822
39- // define custom GraphQL ObjectType `todoType` for our Golang struct `Todo`
40- // Note that
41- // - the fields in our todoType maps with the json tags for the fields in our struct
42- // - the field type matches the field type in our struct
43- var todoType = graphql .NewObject (graphql.ObjectConfig {
44- Name : "Todo" ,
45- Fields : graphql.Fields {
46- "id" : & graphql.Field {
47- Type : graphql .String ,
48- },
49- "text" : & graphql.Field {
50- Type : graphql .String ,
51- },
52- "done" : & graphql.Field {
53- Type : graphql .Boolean ,
54- },
55- },
56- })
57-
58- // root mutation
59- var rootMutation = graphql .NewObject (graphql.ObjectConfig {
60- Name : "RootMutation" ,
61- Fields : graphql.Fields {
62- /*
63- curl -g 'http://localhost:8080/graphql?query=mutation+_{createTodo(text:"My+new+todo"){id,text,done}}'
64- */
65- "createTodo" : & graphql.Field {
66- Type : todoType , // the return type for this field
67- Description : "Create new todo" ,
68- Args : graphql.FieldConfigArgument {
69- "text" : & graphql.ArgumentConfig {
70- Type : graphql .NewNonNull (graphql .String ),
71- },
72- },
73- Resolve : func (params graphql.ResolveParams ) (interface {}, error ) {
74-
75- // marshall and cast the argument value
76- text , _ := params .Args ["text" ].(string )
77-
78- // figure out new id
79- newID := RandStringRunes (8 )
80-
81- // perform mutation operation here
82- // for e.g. create a Todo and save to DB.
83- newTodo := Todo {
84- ID : newID ,
85- Text : text ,
86- Done : false ,
87- }
88-
89- TodoList = append (TodoList , newTodo )
90-
91- // return the new Todo object that we supposedly save to DB
92- // Note here that
93- // - we are returning a `Todo` struct instance here
94- // - we previously specified the return Type to be `todoType`
95- // - `Todo` struct maps to `todoType`, as defined in `todoType` ObjectConfig`
96- return newTodo , nil
97- },
98- },
99- /*
100- curl -g 'http://localhost:8080/graphql?query=mutation+_{updateTodo(id:"a",done:true){id,text,done}}'
101- */
102- "updateTodo" : & graphql.Field {
103- Type : todoType , // the return type for this field
104- Description : "Update existing todo, mark it done or not done" ,
105- Args : graphql.FieldConfigArgument {
106- "done" : & graphql.ArgumentConfig {
107- Type : graphql .Boolean ,
108- },
109- "id" : & graphql.ArgumentConfig {
110- Type : graphql .NewNonNull (graphql .String ),
111- },
112- },
113- Resolve : func (params graphql.ResolveParams ) (interface {}, error ) {
114- // marshall and cast the argument value
115- done , _ := params .Args ["done" ].(bool )
116- id , _ := params .Args ["id" ].(string )
117- affectedTodo := Todo {}
118-
119- // Search list for todo with id and change the done variable
120- for i := 0 ; i < len (TodoList ); i ++ {
121- if TodoList [i ].ID == id {
122- TodoList [i ].Done = done
123- // Assign updated todo so we can return it
124- affectedTodo = TodoList [i ]
125- break
126- }
127- }
128- // Return affected todo
129- return affectedTodo , nil
130- },
131- },
132- },
133- })
134-
135- // root query
136- // we just define a trivial example here, since root query is required.
137- // Test with curl
138- // curl -g 'http://localhost:8080/graphql?query={lastTodo{id,text,done}}'
139- var rootQuery = graphql .NewObject (graphql.ObjectConfig {
140- Name : "RootQuery" ,
141- Fields : graphql.Fields {
142-
143- /*
144- curl -g 'http://localhost:8080/graphql?query={todo(id:"b"){id,text,done}}'
145- */
146- "todo" : & graphql.Field {
147- Type : todoType ,
148- Description : "Get single todo" ,
149- Args : graphql.FieldConfigArgument {
150- "id" : & graphql.ArgumentConfig {
151- Type : graphql .String ,
152- },
153- },
154- Resolve : func (params graphql.ResolveParams ) (interface {}, error ) {
155-
156- idQuery , isOK := params .Args ["id" ].(string )
157- if isOK {
158- // Search for el with id
159- for _ , todo := range TodoList {
160- if todo .ID == idQuery {
161- return todo , nil
162- }
163- }
164- }
165-
166- return Todo {}, nil
167- },
168- },
169-
170- "lastTodo" : & graphql.Field {
171- Type : todoType ,
172- Description : "Last todo added" ,
173- Resolve : func (params graphql.ResolveParams ) (interface {}, error ) {
174- return TodoList [len (TodoList )- 1 ], nil
175- },
176- },
177-
178- /*
179- curl -g 'http://localhost:8080/graphql?query={todoList{id,text,done}}'
180- */
181- "todoList" : & graphql.Field {
182- Type : graphql .NewList (todoType ),
183- Description : "List of todos" ,
184- Resolve : func (p graphql.ResolveParams ) (interface {}, error ) {
185- return TodoList , nil
186- },
187- },
188- },
189- })
190-
191- // define schema, with our rootQuery and rootMutation
192- var schema , _ = graphql .NewSchema (graphql.SchemaConfig {
193- Query : rootQuery ,
194- Mutation : rootMutation ,
195- })
196-
19723func executeQuery (query string , schema graphql.Schema ) * graphql.Result {
19824 result := graphql .Do (graphql.Params {
19925 Schema : schema ,
@@ -207,7 +33,7 @@ func executeQuery(query string, schema graphql.Schema) *graphql.Result {
20733
20834func main () {
20935 http .HandleFunc ("/graphql" , func (w http.ResponseWriter , r * http.Request ) {
210- result := executeQuery (r .URL .Query ().Get ("query" ), schema )
36+ result := executeQuery (r .URL .Query ().Get ("query" ), schema . TodoSchema )
21137 json .NewEncoder (w ).Encode (result )
21238 })
21339 // Serve static files
0 commit comments