馃寠 A GraphQL implementation in Gleam
at main 7.3 kB view raw
1/// Snapshot tests for SDL generation 2/// 3/// Verifies that GraphQL types are correctly serialized to SDL format 4import birdie 5import gleam/option.{None, Some} 6import gleeunit 7import swell/schema 8import swell/sdl 9import swell/value 10 11pub fn main() { 12 gleeunit.main() 13} 14 15// ===== Input Object Types ===== 16 17pub fn simple_input_object_test() { 18 let input_type = 19 schema.input_object_type( 20 "UserInput", 21 "Input for creating or updating a user", 22 [ 23 schema.input_field("name", schema.string_type(), "User's name", None), 24 schema.input_field( 25 "email", 26 schema.non_null(schema.string_type()), 27 "User's email address", 28 None, 29 ), 30 schema.input_field("age", schema.int_type(), "User's age", None), 31 ], 32 ) 33 34 let serialized = sdl.print_type(input_type) 35 36 birdie.snap( 37 title: "Simple input object with descriptions", 38 content: serialized, 39 ) 40} 41 42pub fn input_object_with_default_values_test() { 43 let input_type = 44 schema.input_object_type("FilterInput", "Filter options for queries", [ 45 schema.input_field( 46 "limit", 47 schema.int_type(), 48 "Maximum number of results", 49 Some(value.Int(10)), 50 ), 51 schema.input_field( 52 "offset", 53 schema.int_type(), 54 "Number of results to skip", 55 Some(value.Int(0)), 56 ), 57 ]) 58 59 let serialized = sdl.print_type(input_type) 60 61 birdie.snap(title: "Input object with default values", content: serialized) 62} 63 64pub fn nested_input_types_test() { 65 let address_input = 66 schema.input_object_type("AddressInput", "Street address information", [ 67 schema.input_field("street", schema.string_type(), "Street name", None), 68 schema.input_field("city", schema.string_type(), "City name", None), 69 ]) 70 71 let user_input = 72 schema.input_object_type("UserInput", "User information", [ 73 schema.input_field("name", schema.string_type(), "Full name", None), 74 schema.input_field("address", address_input, "Home address", None), 75 ]) 76 77 let serialized = sdl.print_types([address_input, user_input]) 78 79 birdie.snap(title: "Nested input types", content: serialized) 80} 81 82// ===== Object Types ===== 83 84pub fn simple_object_type_test() { 85 let user_type = 86 schema.object_type("User", "A user in the system", [ 87 schema.field("id", schema.non_null(schema.id_type()), "User ID", fn(_ctx) { 88 Ok(value.String("1")) 89 }), 90 schema.field("name", schema.string_type(), "User's name", fn(_ctx) { 91 Ok(value.String("Alice")) 92 }), 93 schema.field("email", schema.string_type(), "Email address", fn(_ctx) { 94 Ok(value.String("alice@example.com")) 95 }), 96 ]) 97 98 let serialized = sdl.print_type(user_type) 99 100 birdie.snap(title: "Simple object type", content: serialized) 101} 102 103pub fn object_with_list_fields_test() { 104 let post_type = 105 schema.object_type("Post", "A blog post", [ 106 schema.field("id", schema.id_type(), "Post ID", fn(_ctx) { 107 Ok(value.String("1")) 108 }), 109 schema.field("title", schema.string_type(), "Post title", fn(_ctx) { 110 Ok(value.String("Hello")) 111 }), 112 schema.field( 113 "tags", 114 schema.list_type(schema.non_null(schema.string_type())), 115 "Post tags", 116 fn(_ctx) { Ok(value.List([])) }, 117 ), 118 ]) 119 120 let serialized = sdl.print_type(post_type) 121 122 birdie.snap(title: "Object type with list fields", content: serialized) 123} 124 125// ===== Enum Types ===== 126 127pub fn simple_enum_test() { 128 let status_enum = 129 schema.enum_type("Status", "Order status", [ 130 schema.enum_value("PENDING", "Order is pending"), 131 schema.enum_value("PROCESSING", "Order is being processed"), 132 schema.enum_value("SHIPPED", "Order has been shipped"), 133 schema.enum_value("DELIVERED", "Order has been delivered"), 134 ]) 135 136 let serialized = sdl.print_type(status_enum) 137 138 birdie.snap(title: "Simple enum type", content: serialized) 139} 140 141pub fn enum_without_descriptions_test() { 142 let color_enum = 143 schema.enum_type("Color", "", [ 144 schema.enum_value("RED", ""), 145 schema.enum_value("GREEN", ""), 146 schema.enum_value("BLUE", ""), 147 ]) 148 149 let serialized = sdl.print_type(color_enum) 150 151 birdie.snap(title: "Enum without descriptions", content: serialized) 152} 153 154// ===== Scalar Types ===== 155 156pub fn built_in_scalars_test() { 157 let scalars = [ 158 schema.string_type(), 159 schema.int_type(), 160 schema.float_type(), 161 schema.boolean_type(), 162 schema.id_type(), 163 ] 164 165 let serialized = sdl.print_types(scalars) 166 167 birdie.snap(title: "Built-in scalar types", content: serialized) 168} 169 170// ===== Complex Types ===== 171 172pub fn type_with_non_null_and_list_test() { 173 let input_type = 174 schema.input_object_type("ComplexInput", "Complex type modifiers", [ 175 schema.input_field( 176 "required", 177 schema.non_null(schema.string_type()), 178 "Required string", 179 None, 180 ), 181 schema.input_field( 182 "optionalList", 183 schema.list_type(schema.string_type()), 184 "Optional list of strings", 185 None, 186 ), 187 schema.input_field( 188 "requiredList", 189 schema.non_null(schema.list_type(schema.string_type())), 190 "Required list of optional strings", 191 None, 192 ), 193 schema.input_field( 194 "listOfRequired", 195 schema.list_type(schema.non_null(schema.string_type())), 196 "Optional list of required strings", 197 None, 198 ), 199 schema.input_field( 200 "requiredListOfRequired", 201 schema.non_null(schema.list_type(schema.non_null(schema.string_type()))), 202 "Required list of required strings", 203 None, 204 ), 205 ]) 206 207 let serialized = sdl.print_type(input_type) 208 209 birdie.snap( 210 title: "Type with NonNull and List modifiers", 211 content: serialized, 212 ) 213} 214 215// ===== Multiple Related Types ===== 216 217pub fn related_types_test() { 218 let sort_direction = 219 schema.enum_type("SortDirection", "Sort direction for queries", [ 220 schema.enum_value("ASC", "Ascending order"), 221 schema.enum_value("DESC", "Descending order"), 222 ]) 223 224 let sort_field_enum = 225 schema.enum_type("UserSortField", "Fields to sort users by", [ 226 schema.enum_value("NAME", "Sort by name"), 227 schema.enum_value("EMAIL", "Sort by email"), 228 schema.enum_value("CREATED_AT", "Sort by creation date"), 229 ]) 230 231 let sort_input = 232 schema.input_object_type("SortInput", "Sort configuration", [ 233 schema.input_field( 234 "field", 235 schema.non_null(sort_field_enum), 236 "Field to sort by", 237 None, 238 ), 239 schema.input_field( 240 "direction", 241 sort_direction, 242 "Sort direction", 243 Some(value.String("ASC")), 244 ), 245 ]) 246 247 let serialized = 248 sdl.print_types([sort_direction, sort_field_enum, sort_input]) 249 250 birdie.snap(title: "Multiple related types", content: serialized) 251} 252 253// ===== Empty Types (Edge Cases) ===== 254 255pub fn empty_input_object_test() { 256 let empty_input = schema.input_object_type("EmptyInput", "An empty input", []) 257 258 let serialized = sdl.print_type(empty_input) 259 260 birdie.snap(title: "Empty input object", content: serialized) 261} 262 263pub fn empty_enum_test() { 264 let empty_enum = schema.enum_type("EmptyEnum", "An empty enum", []) 265 266 let serialized = sdl.print_type(empty_enum) 267 268 birdie.snap(title: "Empty enum", content: serialized) 269}