-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
70 lines (60 loc) · 1.84 KB
/
doc.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
/*
Package gql is a GraphQL implementation
NOTE: THIS PACKAGE IS STILL UNDER DEVELOPMENT
About gql
The gql package aims to fulfill a few extra features that I missed in other packages,
be as efficient as possible and 100% close to the GraphQL specificaton. The idea behind
the package is to achive a really easy learning curve, but still leave many option to customize
everything to your benefit.
Just a few features that are already built in
- Custom scalars
- request validation (still in progress)
- handler
Getting started
We'll build our own pizzeria, for that we need a `Pizza` type to store the properties of the pizzas.
At the beginning, we only store the name, size and if the pizza is spicy.
var PizzaType = &gql.Object{
Name: "Pizza",
Fields: gql.Fields{
"name": &gql.Field{
Description: "name of the pizza",
Type: gql.String,
},
"size": &gql.Field{
Description: "size of the pizza (in cm)",
Type: gql.Int,
},
"is_spicy": &gql.Field{
Description: "is the pizza spicy or not",
Type: gql.Boolean,
},
},
}
Since we have a type, we need to build a query to be able to access to it. The root query will be
the starting point of any query, fields in this type represents be all the available query fields.
Let's add the root query with a "pizzas" query, which will return a list of the available pizzas.
Since we need a little logic here to return the available pizzas, we'll use a resolver.
var RootQuery = &gql.Object{
Name: "Query",
Fields: gql.Fields{
"pizzas": &gql.Field{
Type: gql.NewList(PizzaType),
Resolver: func(c gql.Context) (interface{}, error) {
return []Pizza{
Pizza{
Name: "songoku",
Size: 32,
IsSpicy: false,
},
Pizza{
Name: "hell-o",
Size: 40,
IsSpicy: true,
},
}, nil
},
},
},
}
*/
package gql