Type-safe GraphQL client generator for Gleam
at v1.1.2 3.9 kB view raw
1import gleam/list 2import gleeunit/should 3import squall/internal/schema 4 5// Test: Parse a simple scalar type from introspection result 6pub fn parse_scalar_type_test() { 7 let json_response = 8 "{ 9 \"data\": { 10 \"__schema\": { 11 \"types\": [ 12 { 13 \"name\": \"String\", 14 \"kind\": \"SCALAR\", 15 \"description\": \"Built-in String type\" 16 } 17 ] 18 } 19 } 20 }" 21 22 let result = schema.parse_introspection_response(json_response) 23 24 should.be_ok(result) 25 let assert Ok(schema_data) = result 26 27 // Should have at least one type 28 should.equal(schema.get_type_count(schema_data), 1) 29 30 // Should find the String type 31 let string_type = schema.find_type(schema_data, "String") 32 should.be_ok(string_type) 33} 34 35// Test: Parse an object type with fields 36pub fn parse_object_type_test() { 37 let json_response = 38 "{ 39 \"data\": { 40 \"__schema\": { 41 \"types\": [ 42 { 43 \"name\": \"Character\", 44 \"kind\": \"OBJECT\", 45 \"description\": \"A character from Rick and Morty\", 46 \"fields\": [ 47 { 48 \"name\": \"id\", 49 \"type\": { 50 \"kind\": \"NON_NULL\", 51 \"ofType\": { 52 \"kind\": \"SCALAR\", 53 \"name\": \"ID\" 54 } 55 } 56 }, 57 { 58 \"name\": \"name\", 59 \"type\": { 60 \"kind\": \"SCALAR\", 61 \"name\": \"String\" 62 } 63 } 64 ] 65 } 66 ] 67 } 68 } 69 }" 70 71 let result = schema.parse_introspection_response(json_response) 72 73 should.be_ok(result) 74 let assert Ok(schema_data) = result 75 76 // Should find the Character type 77 let character_type = schema.find_type(schema_data, "Character") 78 should.be_ok(character_type) 79 80 // Character should have fields 81 let assert Ok(char_type) = character_type 82 let fields = schema.get_type_fields(char_type) 83 should.equal(list.length(fields), 2) 84} 85 86// Test: Parse query type 87pub fn parse_query_type_test() { 88 let json_response = 89 "{ 90 \"data\": { 91 \"__schema\": { 92 \"queryType\": { 93 \"name\": \"Query\" 94 }, 95 \"types\": [ 96 { 97 \"name\": \"Query\", 98 \"kind\": \"OBJECT\", 99 \"fields\": [ 100 { 101 \"name\": \"character\", 102 \"type\": { 103 \"kind\": \"OBJECT\", 104 \"name\": \"Character\" 105 }, 106 \"args\": [ 107 { 108 \"name\": \"id\", 109 \"type\": { 110 \"kind\": \"NON_NULL\", 111 \"ofType\": { 112 \"kind\": \"SCALAR\", 113 \"name\": \"ID\" 114 } 115 } 116 } 117 ] 118 } 119 ] 120 } 121 ] 122 } 123 } 124 }" 125 126 let result = schema.parse_introspection_response(json_response) 127 128 should.be_ok(result) 129 let assert Ok(schema_data) = result 130 131 // Should have query type 132 let query_type_name = schema.get_query_type_name(schema_data) 133 should.equal(query_type_name, Ok("Query")) 134} 135 136// Test: Handle invalid JSON 137pub fn invalid_json_test() { 138 let invalid_json = "{ invalid json }" 139 140 let result = schema.parse_introspection_response(invalid_json) 141 142 should.be_error(result) 143} 144 145// Test: Handle missing __schema field 146pub fn missing_schema_field_test() { 147 let json_response = "{\"data\": {}}" 148 149 let result = schema.parse_introspection_response(json_response) 150 151 should.be_error(result) 152}