diff --git a/example/src/graphql/get_character.gleam b/example/src/graphql/get_character.gleam index 3e9f034..9fa0cc9 100644 --- a/example/src/graphql/get_character.gleam +++ b/example/src/graphql/get_character.gleam @@ -1,8 +1,8 @@ import gleam/dynamic/decode import gleam/http/request.{type Request} import gleam/json -import squall import gleam/option.{type Option} +import squall pub type Character { Character( @@ -22,14 +22,12 @@ pub fn character_decoder() -> decode.Decoder(Character) { } pub fn character_to_json(input: Character) -> json.Json { - json.object( - [ - #("id", json.nullable(input.id, json.string)), - #("name", json.nullable(input.name, json.string)), - #("status", json.nullable(input.status, json.string)), - #("species", json.nullable(input.species, json.string)), - ], - ) + json.object([ + #("id", json.nullable(input.id, json.string)), + #("name", json.nullable(input.name, json.string)), + #("status", json.nullable(input.status, json.string)), + #("species", json.nullable(input.species, json.string)), + ]) } pub type GetCharacterResponse { @@ -37,19 +35,23 @@ pub type GetCharacterResponse { } pub fn get_character_response_decoder() -> decode.Decoder(GetCharacterResponse) { - use character <- decode.field("character", decode.optional(character_decoder())) + use character <- decode.field( + "character", + decode.optional(character_decoder()), + ) decode.success(GetCharacterResponse(character: character)) } pub fn get_character_response_to_json(input: GetCharacterResponse) -> json.Json { - json.object( - [ - #("character", json.nullable(input.character, character_to_json)), - ], - ) + json.object([ + #("character", json.nullable(input.character, character_to_json)), + ]) } -pub fn get_character(client: squall.Client, id: String) -> Result(Request(String), String) { +pub fn get_character( + client: squall.Client, + id: String, +) -> Result(Request(String), String) { squall.prepare_request( client, "query GetCharacter($id: ID!) {\n character(id: $id) {\n id\n name\n status\n species\n }\n}\n", @@ -57,6 +59,8 @@ pub fn get_character(client: squall.Client, id: String) -> Result(Request(String ) } -pub fn parse_get_character_response(body: String) -> Result(GetCharacterResponse, String) { +pub fn parse_get_character_response( + body: String, +) -> Result(GetCharacterResponse, String) { squall.parse_response(body, get_character_response_decoder()) } diff --git a/example/src/graphql/get_character_with_fragment.gleam b/example/src/graphql/get_character_with_fragment.gleam index 20da85a..0407731 100644 --- a/example/src/graphql/get_character_with_fragment.gleam +++ b/example/src/graphql/get_character_with_fragment.gleam @@ -1,8 +1,8 @@ import gleam/dynamic/decode import gleam/http/request.{type Request} import gleam/json -import squall import gleam/option.{type Option} +import squall pub type Character { Character( @@ -39,15 +39,13 @@ pub fn location_decoder() -> decode.Decoder(Location) { } pub fn character_to_json(input: Character) -> json.Json { - json.object( - [ - #("id", json.nullable(input.id, json.string)), - #("name", json.nullable(input.name, json.string)), - #("status", json.nullable(input.status, json.string)), - #("species", json.nullable(input.species, json.string)), - #("origin", json.nullable(input.origin, location_to_json)), - ], - ) + json.object([ + #("id", json.nullable(input.id, json.string)), + #("name", json.nullable(input.name, json.string)), + #("status", json.nullable(input.status, json.string)), + #("species", json.nullable(input.species, json.string)), + #("origin", json.nullable(input.origin, location_to_json)), + ]) } pub fn location_to_json(input: Location) -> json.Json { @@ -58,20 +56,28 @@ pub type GetCharacterWithFragmentResponse { GetCharacterWithFragmentResponse(character: Option(Character)) } -pub fn get_character_with_fragment_response_decoder() -> decode.Decoder(GetCharacterWithFragmentResponse) { - use character <- decode.field("character", decode.optional(character_decoder())) +pub fn get_character_with_fragment_response_decoder() -> decode.Decoder( + GetCharacterWithFragmentResponse, +) { + use character <- decode.field( + "character", + decode.optional(character_decoder()), + ) decode.success(GetCharacterWithFragmentResponse(character: character)) } -pub fn get_character_with_fragment_response_to_json(input: GetCharacterWithFragmentResponse) -> json.Json { - json.object( - [ - #("character", json.nullable(input.character, character_to_json)), - ], - ) +pub fn get_character_with_fragment_response_to_json( + input: GetCharacterWithFragmentResponse, +) -> json.Json { + json.object([ + #("character", json.nullable(input.character, character_to_json)), + ]) } -pub fn get_character_with_fragment(client: squall.Client, id: String) -> Result(Request(String), String) { +pub fn get_character_with_fragment( + client: squall.Client, + id: String, +) -> Result(Request(String), String) { squall.prepare_request( client, "fragment CharacterInfo on Character {\n id\n name\n status\n species\n}\n\nquery GetCharacterWithFragment($id: ID!) {\n character(id: $id) {\n ...CharacterInfo\n origin {\n name\n }\n }\n}\n", @@ -79,6 +85,8 @@ pub fn get_character_with_fragment(client: squall.Client, id: String) -> Result( ) } -pub fn parse_get_character_with_fragment_response(body: String) -> Result(GetCharacterWithFragmentResponse, String) { +pub fn parse_get_character_with_fragment_response( + body: String, +) -> Result(GetCharacterWithFragmentResponse, String) { squall.parse_response(body, get_character_with_fragment_response_decoder()) } diff --git a/src/squall.gleam b/src/squall.gleam index bea418d..86b33a4 100644 --- a/src/squall.gleam +++ b/src/squall.gleam @@ -206,7 +206,8 @@ fn generate(endpoint: String) { // Extract main operation and fragments case graphql_ast.get_main_operation(document) { Ok(operation) -> { - let fragments = graphql_ast.get_fragment_definitions(document) + let fragments = + graphql_ast.get_fragment_definitions(document) case codegen.generate_operation_with_fragments( diff --git a/src/squall/internal/codegen.gleam b/src/squall/internal/codegen.gleam index 48a5a19..6583f6e 100644 --- a/src/squall/internal/codegen.gleam +++ b/src/squall/internal/codegen.gleam @@ -249,7 +249,14 @@ pub fn generate_operation( schema_data: schema.Schema, _graphql_endpoint: String, ) -> Result(String, Error) { - generate_operation_with_fragments(operation_name, source, operation, [], schema_data, "") + generate_operation_with_fragments( + operation_name, + source, + operation, + [], + schema_data, + "", + ) } pub fn generate_operation_with_fragments( @@ -1225,15 +1232,17 @@ fn generate_function( schema_types: dict.Dict(String, schema.Type), ) -> Document { // Generate the prepare request function - let prepare_function = generate_prepare_function( - operation_name, - variables, - query_string, - schema_types, - ) + let prepare_function = + generate_prepare_function( + operation_name, + variables, + query_string, + schema_types, + ) // Generate the parse response function - let parse_function = generate_parse_function(operation_name, response_type_name) + let parse_function = + generate_parse_function(operation_name, response_type_name) // Combine both functions with a line break doc.concat([prepare_function, doc.lines(2), parse_function]) diff --git a/test/codegen_test.gleam b/test/codegen_test.gleam index 1b54e87..b02a69f 100644 --- a/test/codegen_test.gleam +++ b/test/codegen_test.gleam @@ -56,7 +56,13 @@ pub fn generate_simple_query_test() { ) let result = - codegen.generate_operation("get_user", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_user", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -123,7 +129,13 @@ pub fn generate_query_with_variables_test() { ) let result = - codegen.generate_operation("get_user", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_user", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -202,7 +214,13 @@ pub fn generate_query_with_nested_types_test() { ) let result = - codegen.generate_operation("get_character", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_character", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -274,7 +292,13 @@ pub fn generate_mutation_test() { ) let result = - codegen.generate_operation("create_user", mutation_source, operation, mock_schema, "") + codegen.generate_operation( + "create_user", + mutation_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -339,7 +363,13 @@ pub fn generate_with_reserved_keywords_test() { ) let result = - codegen.generate_operation("get_item", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_item", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -411,7 +441,13 @@ pub fn generate_inline_scalar_arguments_test() { ) let result = - codegen.generate_operation("get_character", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_character", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -498,7 +534,13 @@ pub fn generate_inline_object_arguments_test() { ) let result = - codegen.generate_operation("get_characters", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_characters", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -572,7 +614,13 @@ pub fn generate_inline_array_arguments_test() { ) let result = - codegen.generate_operation("get_episodes", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_episodes", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -703,7 +751,13 @@ pub fn generate_multiple_root_fields_test() { ) let result = - codegen.generate_operation("multi_query", query_source, operation, mock_schema, "") + codegen.generate_operation( + "multi_query", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -798,7 +852,13 @@ pub fn generate_mutation_with_input_object_test() { ) let result = - codegen.generate_operation("update_profile", mutation_source, operation, mock_schema, "") + codegen.generate_operation( + "update_profile", + mutation_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -915,7 +975,13 @@ pub fn generate_mutation_with_nested_input_object_test() { ) let result = - codegen.generate_operation("update_profile", mutation_source, operation, mock_schema, "") + codegen.generate_operation( + "update_profile", + mutation_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -989,7 +1055,13 @@ pub fn generate_query_with_all_non_nullable_fields_test() { ) let result = - codegen.generate_operation("get_product", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_product", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -1060,7 +1132,13 @@ pub fn generate_query_with_json_scalar_test() { ) let result = - codegen.generate_operation("get_profile", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_profile", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -1144,7 +1222,13 @@ pub fn generate_mutation_with_json_input_field_test() { ) let result = - codegen.generate_operation("update_settings", mutation_source, operation, mock_schema, "") + codegen.generate_operation( + "update_settings", + mutation_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -1245,7 +1329,13 @@ pub fn generate_mutation_with_optional_input_fields_test() { ) let result = - codegen.generate_operation("create_profile", mutation_source, operation, mock_schema, "") + codegen.generate_operation( + "create_profile", + mutation_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -1321,7 +1411,13 @@ pub fn generate_query_with_optional_response_fields_test() { ) let result = - codegen.generate_operation("get_profile", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_profile", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -1390,7 +1486,13 @@ pub fn generate_response_serializer_simple_test() { ) let result = - codegen.generate_operation("get_user", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_user", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -1454,7 +1556,13 @@ pub fn generate_response_serializer_with_optional_fields_test() { ) let result = - codegen.generate_operation("get_user", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_user", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -1537,7 +1645,13 @@ pub fn generate_response_serializer_with_nested_types_test() { ) let result = - codegen.generate_operation("get_user", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_user", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -1604,7 +1718,13 @@ pub fn generate_response_serializer_with_lists_test() { ) let result = - codegen.generate_operation("get_users", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_users", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> { @@ -1695,7 +1815,13 @@ pub fn generate_response_serializer_with_all_scalars_test() { ) let result = - codegen.generate_operation("get_product", query_source, operation, mock_schema, "") + codegen.generate_operation( + "get_product", + query_source, + operation, + mock_schema, + "", + ) case result { Ok(code) -> {