馃寠 A GraphQL implementation in Gleam
1/// Tests for mutation execution
2import birdie
3import gleam/list
4import gleam/option.{None, Some}
5import gleam/string
6import gleeunit
7import gleeunit/should
8import swell/executor
9import swell/schema
10import swell/value
11
12pub fn main() {
13 gleeunit.main()
14}
15
16fn format_response(response: executor.Response) -> String {
17 string.inspect(response)
18}
19
20fn test_schema_with_mutations() -> schema.Schema {
21 let user_type =
22 schema.object_type("User", "A user", [
23 schema.field("id", schema.id_type(), "User ID", fn(ctx) {
24 case ctx.data {
25 Some(value.Object(fields)) -> {
26 case fields |> list.key_find("id") {
27 Ok(id) -> Ok(id)
28 Error(_) -> Ok(value.String("123"))
29 }
30 }
31 _ -> Ok(value.String("123"))
32 }
33 }),
34 schema.field("name", schema.string_type(), "User name", fn(ctx) {
35 case ctx.data {
36 Some(value.Object(fields)) -> {
37 case fields |> list.key_find("name") {
38 Ok(name) -> Ok(name)
39 Error(_) -> Ok(value.String("Unknown"))
40 }
41 }
42 _ -> Ok(value.String("Unknown"))
43 }
44 }),
45 ])
46
47 let query_type =
48 schema.object_type("Query", "Root query", [
49 schema.field("dummy", schema.string_type(), "Dummy field", fn(_) {
50 Ok(value.String("dummy"))
51 }),
52 ])
53
54 let mutation_type =
55 schema.object_type("Mutation", "Root mutation", [
56 schema.field_with_args(
57 "createUser",
58 user_type,
59 "Create a user",
60 [schema.argument("name", schema.string_type(), "User name", None)],
61 fn(ctx) {
62 case schema.get_argument(ctx, "name") {
63 Some(value.String(name)) ->
64 Ok(
65 value.Object([
66 #("id", value.String("123")),
67 #("name", value.String(name)),
68 ]),
69 )
70 _ ->
71 Ok(
72 value.Object([
73 #("id", value.String("123")),
74 #("name", value.String("Default Name")),
75 ]),
76 )
77 }
78 },
79 ),
80 schema.field_with_args(
81 "deleteUser",
82 schema.boolean_type(),
83 "Delete a user",
84 [
85 schema.argument(
86 "id",
87 schema.non_null(schema.id_type()),
88 "User ID",
89 None,
90 ),
91 ],
92 fn(_) { Ok(value.Boolean(True)) },
93 ),
94 ])
95
96 schema.schema(query_type, Some(mutation_type))
97}
98
99pub fn execute_simple_mutation_test() {
100 let schema = test_schema_with_mutations()
101 let query = "mutation { createUser(name: \"Alice\") { id name } }"
102
103 let result = executor.execute(query, schema, schema.context(None))
104
105 let response = case result {
106 Ok(r) -> r
107 Error(_) -> panic as "Execution failed"
108 }
109
110 birdie.snap(
111 title: "Execute simple mutation",
112 content: format_response(response),
113 )
114}
115
116pub fn execute_named_mutation_test() {
117 let schema = test_schema_with_mutations()
118 let query = "mutation CreateUser { createUser(name: \"Bob\") { id name } }"
119
120 let result = executor.execute(query, schema, schema.context(None))
121
122 should.be_ok(result)
123}
124
125pub fn execute_multiple_mutations_test() {
126 let schema = test_schema_with_mutations()
127 let query =
128 "
129 mutation {
130 createUser(name: \"Alice\") { id name }
131 deleteUser(id: \"123\")
132 }
133 "
134
135 let result = executor.execute(query, schema, schema.context(None))
136
137 let response = case result {
138 Ok(r) -> r
139 Error(_) -> panic as "Execution failed"
140 }
141
142 birdie.snap(
143 title: "Execute multiple mutations",
144 content: format_response(response),
145 )
146}
147
148pub fn execute_mutation_without_argument_test() {
149 let schema = test_schema_with_mutations()
150 let query = "mutation { createUser { id name } }"
151
152 let result = executor.execute(query, schema, schema.context(None))
153
154 should.be_ok(result)
155}
156
157pub fn execute_mutation_with_context_test() {
158 let schema = test_schema_with_mutations()
159 let query = "mutation { createUser(name: \"Context User\") { id name } }"
160
161 let ctx_data =
162 value.Object([
163 #("userId", value.String("456")),
164 #("token", value.String("abc123")),
165 ])
166 let ctx = schema.context(Some(ctx_data))
167
168 let result = executor.execute(query, schema, ctx)
169
170 should.be_ok(result)
171}