馃寠 A GraphQL implementation in Gleam
at v2.1.1 5.4 kB view raw
1/// Snapshot tests for mutation SDL generation 2import birdie 3import gleam/option.{None} 4import gleeunit 5import swell/schema 6import swell/sdl 7import swell/value 8 9pub fn main() { 10 gleeunit.main() 11} 12 13pub fn simple_mutation_type_test() { 14 let user_type = 15 schema.object_type("User", "A user", [ 16 schema.field("id", schema.non_null(schema.id_type()), "User ID", fn(_) { 17 Ok(value.String("1")) 18 }), 19 schema.field("name", schema.string_type(), "User name", fn(_) { 20 Ok(value.String("Alice")) 21 }), 22 ]) 23 24 let mutation_type = 25 schema.object_type("Mutation", "Root mutation type", [ 26 schema.field_with_args( 27 "createUser", 28 user_type, 29 "Create a new user", 30 [ 31 schema.argument( 32 "name", 33 schema.non_null(schema.string_type()), 34 "User name", 35 None, 36 ), 37 ], 38 fn(_) { Ok(value.Null) }, 39 ), 40 ]) 41 42 let serialized = sdl.print_type(mutation_type) 43 44 birdie.snap(title: "Simple mutation type", content: serialized) 45} 46 47pub fn mutation_with_input_object_test() { 48 let create_user_input = 49 schema.input_object_type("CreateUserInput", "Input for creating a user", [ 50 schema.input_field( 51 "name", 52 schema.non_null(schema.string_type()), 53 "User name", 54 None, 55 ), 56 schema.input_field( 57 "email", 58 schema.non_null(schema.string_type()), 59 "Email address", 60 None, 61 ), 62 schema.input_field("age", schema.int_type(), "Age", None), 63 ]) 64 65 let user_type = 66 schema.object_type("User", "A user", [ 67 schema.field("id", schema.id_type(), "User ID", fn(_) { Ok(value.Null) }), 68 schema.field("name", schema.string_type(), "User name", fn(_) { 69 Ok(value.Null) 70 }), 71 ]) 72 73 let mutation_type = 74 schema.object_type("Mutation", "Mutations", [ 75 schema.field_with_args( 76 "createUser", 77 user_type, 78 "Create a new user", 79 [ 80 schema.argument( 81 "input", 82 schema.non_null(create_user_input), 83 "User data", 84 None, 85 ), 86 ], 87 fn(_) { Ok(value.Null) }, 88 ), 89 ]) 90 91 let serialized = 92 sdl.print_types([create_user_input, user_type, mutation_type]) 93 94 birdie.snap(title: "Mutation with input object argument", content: serialized) 95} 96 97pub fn multiple_mutations_test() { 98 let user_type = 99 schema.object_type("User", "A user", [ 100 schema.field("id", schema.id_type(), "User ID", fn(_) { Ok(value.Null) }), 101 ]) 102 103 let delete_response = 104 schema.object_type("DeleteResponse", "Delete response", [ 105 schema.field("success", schema.boolean_type(), "Success flag", fn(_) { 106 Ok(value.Null) 107 }), 108 ]) 109 110 let mutation_type = 111 schema.object_type("Mutation", "Mutations", [ 112 schema.field_with_args( 113 "createUser", 114 user_type, 115 "Create a user", 116 [schema.argument("name", schema.string_type(), "Name", None)], 117 fn(_) { Ok(value.Null) }, 118 ), 119 schema.field_with_args( 120 "updateUser", 121 user_type, 122 "Update a user", 123 [ 124 schema.argument( 125 "id", 126 schema.non_null(schema.id_type()), 127 "User ID", 128 None, 129 ), 130 schema.argument("name", schema.string_type(), "New name", None), 131 ], 132 fn(_) { Ok(value.Null) }, 133 ), 134 schema.field_with_args( 135 "deleteUser", 136 delete_response, 137 "Delete a user", 138 [ 139 schema.argument( 140 "id", 141 schema.non_null(schema.id_type()), 142 "User ID", 143 None, 144 ), 145 ], 146 fn(_) { Ok(value.Null) }, 147 ), 148 ]) 149 150 let serialized = sdl.print_type(mutation_type) 151 152 birdie.snap( 153 title: "Multiple mutations (CRUD operations)", 154 content: serialized, 155 ) 156} 157 158pub fn mutation_returning_list_test() { 159 let user_type = 160 schema.object_type("User", "A user", [ 161 schema.field("id", schema.id_type(), "User ID", fn(_) { Ok(value.Null) }), 162 ]) 163 164 let mutation_type = 165 schema.object_type("Mutation", "Mutations", [ 166 schema.field_with_args( 167 "createUsers", 168 schema.list_type(user_type), 169 "Create multiple users", 170 [ 171 schema.argument( 172 "names", 173 schema.list_type(schema.non_null(schema.string_type())), 174 "User names", 175 None, 176 ), 177 ], 178 fn(_) { Ok(value.Null) }, 179 ), 180 ]) 181 182 let serialized = sdl.print_type(mutation_type) 183 184 birdie.snap(title: "Mutation returning list", content: serialized) 185} 186 187pub fn mutation_with_non_null_return_test() { 188 let user_type = 189 schema.object_type("User", "A user", [ 190 schema.field("id", schema.id_type(), "User ID", fn(_) { Ok(value.Null) }), 191 ]) 192 193 let mutation_type = 194 schema.object_type("Mutation", "Mutations", [ 195 schema.field_with_args( 196 "createUser", 197 schema.non_null(user_type), 198 "Create a user (guaranteed to return)", 199 [ 200 schema.argument( 201 "name", 202 schema.non_null(schema.string_type()), 203 "User name", 204 None, 205 ), 206 ], 207 fn(_) { Ok(value.Null) }, 208 ), 209 ]) 210 211 let serialized = sdl.print_type(mutation_type) 212 213 birdie.snap(title: "Mutation with non-null return type", content: serialized) 214}