-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
204 lines (175 loc) · 3.42 KB
/
schema.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
package feasibility
import (
graphql "github.com/neelance/graphql-go"
)
var Schema = `
schema {
query: Query
mutation: Mutation
}
# The query type, represents all of the entry points into our object graph
type Query {
costs: [Cost]!
cost(id: ID!): Cost
categories: [Category]!
}
# The mutation type, represents all updates we can make to our data
type Mutation {
createCost(name: String!, price: Int!): Cost
}
# A project cost
type Cost {
id: ID!
name: String!
price: Int!
}
# A project cost category
type Category {
id: ID!
name: String!
total: Int
costs: [Cost]
categories: [Category]
}
`
type cost struct {
ID graphql.ID
Name string
Price int32
}
var costs = []*cost{
{
ID: "1000",
Name: "Foundation",
Price: 2000000,
},
{
ID: "1001",
Name: "Framing",
Price: 4000000,
},
{
ID: "1002",
Name: "Flooring",
Price: 200000,
},
{
ID: "1003",
Name: "Drywall",
Price: 5000,
},
}
var costData = make(map[graphql.ID]*cost)
func init() {
for _, c := range costs {
costData[c.ID] = c
}
}
type category struct {
ID graphql.ID
Name string
Categories []graphql.ID
Costs []graphql.ID
}
var categories = []*category{
{
ID: "1001",
Name: "Structure",
Costs: []graphql.ID{"1000", "1002"},
Categories: []graphql.ID{"1002"},
},
{
ID: "1002",
Name: "Walls",
Costs: []graphql.ID{"1001", "1003"},
},
}
var categoryData = make(map[graphql.ID]*category)
func init() {
for _, c := range categories {
categoryData[c.ID] = c
}
}
type Resolver struct{}
func (r *Resolver) Cost(args struct{ ID graphql.ID }) *costResolver {
if c := costData[args.ID]; c != nil {
return &costResolver{c}
}
return nil
}
func (r *Resolver) Costs() []*costResolver {
var cs []*costResolver
for _, c := range costs {
cs = append(cs, &costResolver{c})
}
return cs
}
func (r *Resolver) Category(args struct{ ID graphql.ID }) *categoryResolver {
if c := categoryData[args.ID]; c != nil {
return &categoryResolver{c}
}
return nil
}
func (r *Resolver) Categories() []*categoryResolver {
var cs []*categoryResolver
for _, c := range categories {
cs = append(cs, &categoryResolver{c})
}
return cs
}
func (r *Resolver) CreateCost(args *struct {
Name string
Price int32
}) *costResolver {
cost := &cost{
Name: args.Name,
Price: args.Price,
}
return &costResolver{cost}
}
type costResolver struct {
c *cost
}
func (r *costResolver) ID() graphql.ID {
return r.c.ID
}
func (r *costResolver) Name() string {
return r.c.Name
}
func (r *costResolver) Price() int32 {
return r.c.Price
}
type categoryResolver struct {
c *category
}
func (r *categoryResolver) ID() graphql.ID {
return r.c.ID
}
func (r *categoryResolver) Name() string {
return r.c.Name
}
func (r *categoryResolver) Total() *int32 {
t := int32(0)
for _, id := range r.c.Costs {
t += costData[id].Price
}
for _, id := range r.c.Categories {
c := &categoryResolver{categoryData[id]}
t += *c.Total()
}
return &t
}
func (r *categoryResolver) Costs() *[]*costResolver {
l := make([]*costResolver, len(r.c.Costs))
for i, id := range r.c.Costs {
l[i] = &costResolver{costData[id]}
}
return &l
}
func (r *categoryResolver) Categories() *[]*categoryResolver {
l := make([]*categoryResolver, len(r.c.Categories))
for i, id := range r.c.Categories {
l[i] = &categoryResolver{categoryData[id]}
}
return &l
}