forked from samsarahq/thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kitchen_sink_test.go
158 lines (133 loc) · 2.87 KB
/
kitchen_sink_test.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
package federation
import (
"context"
"fmt"
"github.com/denkhaus/thunder/batch"
"github.com/denkhaus/thunder/graphql/schemabuilder"
)
type Enum int
type Foo struct {
Name string
}
type Bar struct {
Id int64
}
type FooOrBar struct {
schemabuilder.Union
*Foo
*Bar
}
type Pair struct {
A, B int64
}
func buildTestSchema1() *schemabuilder.Schema {
schema := schemabuilder.NewSchemaWithName("schema1")
query := schema.Query()
query.FieldFunc("s1f", func() *Foo {
return &Foo{
Name: "jimbob",
}
})
query.FieldFunc("s1fff", func() []*Foo {
return []*Foo{
{
Name: "jimbo",
},
{
Name: "bob",
},
}
})
query.FieldFunc("s1echo", func(args struct {
Foo string
Required Pair
Optional *int64
}) string {
return fmt.Sprintf("%s %v %v", args.Foo, args.Required, args.Optional)
})
schema.Enum(Enum(1), map[string]Enum{
"one": 1,
})
mutation := schema.Mutation()
mutation.FieldFunc("s1addFoo", func(args struct{ Name string }) *Foo {
return &Foo{
Name: args.Name,
}
})
foo := schema.Object("Foo", Foo{}, schemabuilder.RootObject)
foo.BatchFieldFunc("s1hmm", func(ctx context.Context, in map[batch.Index]*Foo) (map[batch.Index]string, error) {
out := make(map[batch.Index]string)
for i, foo := range in {
out[i] = foo.Name + "!!!"
}
return out, nil
})
foo.FieldFunc("s1nest", func(f *Foo) *Foo {
return f
})
foo.FieldFunc("s1enum", func(f *Foo) Enum {
return Enum(1)
})
type BarKeys struct {
Id int64
}
schema.FederatedFieldFunc("Bar", func(args struct{ Keys []*BarKeys }) []*Bar {
bars := make([]*Bar, 0, len(args.Keys))
for _, key := range args.Keys {
bars = append(bars, &Bar{Id: key.Id})
}
return bars
})
bar := schema.Object("Bar", Bar{})
bar.FieldFunc("s1baz", func(b *Bar) string {
return fmt.Sprint(b.Id)
})
query.FieldFunc("s1both", func() []FooOrBar {
return []FooOrBar{
{
Foo: &Foo{
Name: "this is the foo",
},
},
{
Bar: &Bar{
Id: 1234,
},
},
}
})
return schema
}
func buildTestSchema2() *schemabuilder.Schema {
schema := schemabuilder.NewSchemaWithName("schema2")
type FooKeys struct {
Name string
}
schema.FederatedFieldFunc("Foo", func(args struct{ Keys []*FooKeys }) []*Foo {
foos := make([]*Foo, 0, len(args.Keys))
for _, key := range args.Keys {
foos = append(foos, &Foo{Name: key.Name})
}
return foos
})
schema.Query().FieldFunc("s2root", func() string {
return "hello"
})
foo := schema.Object("Foo", Foo{})
foo.FieldFunc("s2ok", func(ctx context.Context, in *Foo) (int, error) {
return len(in.Name), nil
})
foo.FieldFunc("s2ok2", func(in *Foo) (int, error) {
return len(in.Name), nil
})
foo.FieldFunc("s2bar", func(in *Foo) *Bar {
return &Bar{
Id: int64(len(in.Name)*2 + 4),
}
})
foo.FieldFunc("s2nest", func(f *Foo) *Foo {
return f
})
schema.Object("Bar", Bar{}, schemabuilder.RootObject)
return schema
}