-
Notifications
You must be signed in to change notification settings - Fork 16
/
gql_struct.go
114 lines (108 loc) · 2.23 KB
/
gql_struct.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
package main
import (
"errors"
"log"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/kinds"
)
// _validate for ID support
func _validate(value string) error {
if len(value) < 3 {
return errors.New("The minimum length required is 3")
}
return nil
}
// ID support
var ID = graphql.NewScalar(graphql.ScalarConfig{
Name: "ID",
Description: "The `id` scalar type represents a ID Object.",
Serialize: func(value interface{}) interface{} {
return value
},
ParseValue: func(value interface{}) interface{} {
var err error
switch value.(type) {
case string:
err = _validate(value.(string))
default:
err = errors.New("Must be of type string.")
}
if err != nil {
log.Fatal(err)
}
return value
},
ParseLiteral: func(valueAst ast.Value) interface{} {
if valueAst.GetKind() == kinds.StringValue {
err := _validate(valueAst.GetValue().(string))
if err != nil {
log.Fatal(err)
}
return valueAst
} else {
log.Fatal("Must be of type string.")
return nil
}
},
})
// RecipeType
var RecipeType = graphql.NewObject(graphql.ObjectConfig{
Name: "Recipe",
Fields: graphql.Fields{
"_id": &graphql.Field{
Type: ID,
},
"name": &graphql.Field{
Type: graphql.String,
},
"imageUrl": &graphql.Field{
Type: graphql.String,
},
"category": &graphql.Field{
Type: graphql.String,
},
"description": &graphql.Field{
Type: graphql.String,
},
"instructions": &graphql.Field{
Type: graphql.String,
},
"createdDate": &graphql.Field{
Type: graphql.String,
},
"likes": &graphql.Field{
Type: graphql.Int,
},
"username": &graphql.Field{
Type: graphql.String,
},
},
})
// UserType
var UserType = graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"_id": &graphql.Field{
Type: ID,
},
"username": &graphql.Field{
Type: graphql.String,
},
"password": &graphql.Field{
Type: graphql.String,
},
"email": &graphql.Field{
Type: graphql.String,
},
"joinDate": &graphql.Field{
Type: graphql.String,
},
"favorites": &graphql.Field{
Type: graphql.NewList(RecipeType),
},
"token": &graphql.Field{ // graphql only
Type: graphql.String,
},
},
})