1 package example
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 "github.com/udacity/graphb"
8 )
9
10 func TestMethodChaining(t *testing.T) {
11
22 q := graphb.MakeQuery(graphb.TypeQuery).
23 SetName("").
24 SetFields(
25 graphb.MakeField("a").
26 SetArguments(
27 graphb.ArgumentString("string", "123"),
28 graphb.ArgumentCustomTypeSlice(
29 "mapArray",
30 graphb.ArgumentCustomTypeSliceElem(
31 graphb.ArgumentString("foo", "bar"),
32 graphb.ArgumentInt("fizzbuzz", 15),
33 ),
34 graphb.ArgumentCustomTypeSliceElem(
35 graphb.ArgumentString("foo", "baz"),
36 graphb.ArgumentInt("fizzbuzz", 45),
37 ),
38 ),
39 ).
40 SetFields(
41 graphb.MakeField("x").
42 SetArguments(
43 graphb.ArgumentString("string", "123"),
44 graphb.ArgumentIntSlice("int_slice", 1, 2, 3),
45 ),
46 graphb.MakeField("y"),
47 ).
48 SetAlias("some_alias"),
49 ).
50 AddFields(graphb.MakeField("b"))
51 s, err := q.JSON()
52 assert.Nil(t, err)
53 assert.Equal(t, `{"query":"query{some_alias:a(string:\"123\",mapArray:[{foo:\"bar\",fizzbuzz:15},{foo:\"baz\",fizzbuzz:45}]){x(string:\"123\",int_slice:[1,2,3]),y},b}"}`, s)
54 }
55
56 func TestFunctionalOptions(t *testing.T) {
57
66 q := graphb.NewQuery(
67 graphb.TypeQuery,
68 graphb.OfName("Good_Name_Is_Important"),
69 graphb.OfField(
70 "books",
71 graphb.OfArguments(
72 graphb.ArgumentString("author", "William Shakespeare"),
73 graphb.ArgumentStringSlice("title", "Hamlet", "Henry IV"),
74 ),
75 graphb.OfFields("author", "title", "price"),
76 ),
77 )
78 jsonString, err := q.JSON()
79 assert.Nil(t, err)
80 assert.Equal(
81 t,
82 `{"query":"query Good_Name_Is_Important{books(author:\"William Shakespeare\",title:[\"Hamlet\",\"Henry IV\"]){author,title,price}}"}`,
83 jsonString,
84 )
85 }
86
87 func TestStructLiteral(t *testing.T) {
88
91 q := graphb.Query{
92 Type: graphb.TypeMutation,
93 Name: "It_is_A_Name",
94 Fields: []*graphb.Field{
95 {
96 Name: "f_1",
97 Alias: "f_1_alias",
98 Arguments: []graphb.Argument{graphb.ArgumentIntSlice("arg", 3, 2, -1)},
99 },
100 graphb.NewField("f_2"),
101 },
102 }
103
104 jsonString, err := q.JSON()
105 assert.Nil(t, err)
106 assert.Equal(
107 t,
108 `{"query":"mutation It_is_A_Name{f_1_alias:f_1(arg:[3,2,-1]),f_2}"}`,
109 jsonString,
110 )
111 }
112
View as plain text