-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (107 loc) · 2.37 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
package main
import (
"errors"
"log"
"net/http"
"github.com/rigglo/gql"
"github.com/rigglo/gql/pkg/handler"
)
func main() {
h := handler.New(handler.Config{
Executor: gql.DefaultExecutor(BlockBusters),
Playground: true,
})
http.Handle("/graphql", h)
if err := http.ListenAndServe(":9999", nil); err != nil {
log.Println(err)
}
}
type Movie struct {
ID string `json:"id"`
Title string `json:"title"`
}
var (
BlockBusters = &gql.Schema{
Query: Query,
Mutation: nil,
AdditionalTypes: []gql.Type{
FooInput,
},
}
FooInput = &gql.InputObject{
Name: "FooInput",
Fields: gql.InputFields{
"asd": &gql.InputField{
Type: gql.NewNonNull(gql.String),
DefaultValue: "bar",
},
},
}
Query = &gql.Object{
Name: "Query",
Description: "Just the blockbusters root query",
Fields: gql.Fields{
"top_movies": &gql.Field{
Type: gql.NewNonNull(gql.NewList(MovieType)),
Description: "",
Resolver: func(ctx gql.Context) (interface{}, error) {
return []Movie{
{
ID: "22424234",
Title: "Interstellar",
},
{
ID: "34363453",
Title: "Titanic",
},
}, nil
},
},
"foo": &gql.Field{
Type: gql.String,
Description: "",
Arguments: gql.Arguments{
"asd": &gql.Argument{
Type: gql.NewNonNull(FooInput),
},
"bar": &gql.Argument{
Type: gql.String,
},
},
Resolver: func(ctx gql.Context) (interface{}, error) {
log.Printf("%v", ctx.Args())
return ctx.Args()["asd"].(map[string]interface{})["asd"], nil
},
},
},
}
MovieType = &gql.Object{
Name: "Movie",
Description: "This is a movie from the BlockBusters",
Fields: gql.Fields{
"id": &gql.Field{
Type: gql.String,
Description: "id of the movie",
Arguments: gql.Arguments{
"foo": &gql.Argument{
Type: gql.String,
},
},
},
"title": &gql.Field{
Type: gql.String,
Description: "title of the movie",
},
"name": &gql.Field{
Type: gql.NewNonNull(gql.String),
Description: "name of the movie",
Directives: gql.TypeSystemDirectives{
gql.Deprecate("It's just not implemented and movies have titles, not names"),
},
Resolver: func(ctx gql.Context) (interface{}, error) {
return "asd", errors.New("something bad happened")
},
},
},
}
)