11use std:: {
22 collections:: HashMap ,
3- fmt:: format,
43 sync:: { Arc , Mutex } ,
54 thread:: Result ,
65} ;
@@ -10,11 +9,14 @@ use cynic_introspection::{
109 Directive , DirectiveLocation , FieldWrapping , InputValue , InterfaceType , IntrospectionQuery ,
1110 Type , UnionType , WrappingType ,
1211} ;
13- use stringcase:: { kebab_case} ;
12+ use stringcase:: { kebab_case, pascal_case } ;
1413use tokio:: task:: spawn_blocking;
1514
1615use crate :: {
17- config:: { parse_outside_types:: OutsideTypes , workspace:: WorkspaceConfig } ,
16+ config:: {
17+ parse_outside_types:: { Mod , OutsideTypes } , parse_scalar_types:: ScalarTypes ,
18+ workspace:: WorkspaceConfig ,
19+ } ,
1820 enums:: generate_enum:: generate_enum,
1921 hasura_types:: as_gql_field,
2022 purescript_gen:: {
@@ -26,7 +28,6 @@ use crate::{
2628 purescript_record:: { show_field_name, Field , PurescriptRecord } ,
2729 purescript_type:: PurescriptType ,
2830 purescript_variant:: Variant ,
29- upper_first:: * ,
3031 } ,
3132 write:: write,
3233} ;
@@ -35,6 +36,7 @@ pub async fn build_schema(
3536 role : String ,
3637 postgres_types : Arc < Mutex < HashMap < String , ( String , String , String ) > > > ,
3738 outside_types : Arc < Mutex < OutsideTypes > > ,
39+ scalar_types : Arc < Mutex < ScalarTypes > > ,
3840 workspace_config : WorkspaceConfig ,
3941) -> Result < ( ) > {
4042 // Fetch the introspection schema
@@ -99,7 +101,7 @@ pub async fn build_schema(
99101 let query_type = PurescriptType :: new (
100102 "Query" ,
101103 vec ! [ ] ,
102- Argument :: new_type ( & upper_first ( schema. query_type . as_str ( ) ) ) ,
104+ Argument :: new_type ( & pascal_case ( schema. query_type . as_str ( ) ) ) ,
103105 ) ;
104106 schema_record. add_field ( Field :: new ( "query" ) . with_type ( & query_type. name ) ) ;
105107 types. push ( query_type) ;
@@ -114,7 +116,7 @@ pub async fn build_schema(
114116 let mutation_type = PurescriptType :: new (
115117 "Mutation" ,
116118 vec ! [ ] ,
117- Argument :: new_type ( & upper_first ( & mut_type) ) ,
119+ Argument :: new_type ( & pascal_case ( & mut_type) ) ,
118120 ) ;
119121 schema_record. add_field ( Field :: new ( "mutation" ) . with_type ( & mutation_type. name ) ) ;
120122 types. push ( mutation_type) ;
@@ -125,7 +127,7 @@ pub async fn build_schema(
125127 let mutation_type = PurescriptType :: new (
126128 "Subscription" ,
127129 vec ! [ ] ,
128- Argument :: new_type ( & upper_first ( & mut_type) ) ,
130+ Argument :: new_type ( & pascal_case ( & mut_type) ) ,
129131 ) ;
130132 schema_record. add_field ( Field :: new ( "subscription" ) . with_type ( & mutation_type. name ) ) ;
131133 types. push ( mutation_type) ;
@@ -140,7 +142,7 @@ pub async fn build_schema(
140142 }
141143
142144 // Convert the gql_type_name to a PurescriptTypeName
143- let name = upper_first ( & obj. name ) ;
145+ let name = pascal_case ( & obj. name ) ;
144146
145147 // Creates a new record for the object
146148 let mut record = PurescriptRecord :: new ( "Ignored" ) ;
@@ -201,11 +203,12 @@ pub async fn build_schema(
201203 types. push ( query_type) ;
202204 } ;
203205 match type_ {
204- Type :: Object ( obj) => handle_obj ( obj) ,
206+ Type :: Object ( obj) => handle_obj ( & obj) ,
205207 Type :: Scalar ( scalar) => {
206208 // Add imports for common scalar types if they are used.
207209 // TODO maybe move these to config so they can be updated outside of rust
208210 match scalar. name . as_str ( ) {
211+ "ID" => add_import ( "graphql-client" , "GraphQL.Client.ID" , "ID" , & mut imports) ,
209212 _ if scalar. is_builtin ( ) => { } // ignore built in types like String, Int, etc.
210213 "date" => add_import ( "datetime" , "Data.Date" , "Date" , & mut imports) ,
211214 "timestamp" | "timestamptz" => {
@@ -215,8 +218,19 @@ pub async fn build_schema(
215218 add_import ( "argonaut-core" , "Data.Argonaut.Core" , "Json" , & mut imports)
216219 }
217220 "time" => add_import ( "datetime" , "Data.Time" , "Time" , & mut imports) ,
218- "ID" => add_import ( "graphql-client" , "GraphQL.Client.ID" , "ID" , & mut imports) ,
219- _ => { }
221+ scalar_name => {
222+ match scalar_types. lock ( ) . unwrap ( ) . get ( scalar_name) {
223+ Some ( Mod { package, import, name } ) => {
224+ add_import ( package, import, name, & mut imports) ;
225+ types. push ( PurescriptType :: new ( scalar_name, vec ! [ ] , Argument :: new_type ( name) ) ) ;
226+ }
227+ None => {
228+ add_import ( "argonaut-core" , "Data.Argonaut.Core" , "Json" , & mut imports) ;
229+ types. push ( PurescriptType :: new ( scalar_name, vec ! [ ] , Argument :: new_type ( "Json" ) ) ) ;
230+
231+ }
232+ }
233+ }
220234 }
221235 }
222236 Type :: Enum ( en) => {
@@ -241,7 +255,7 @@ pub async fn build_schema(
241255 }
242256
243257 // Convert the gql_type_name to a PurescriptTypeName
244- let name = upper_first ( & obj. name ) ;
258+ let name = pascal_case ( & obj. name ) ;
245259
246260 // Build a purescript record with all fields
247261 let mut record = PurescriptRecord :: new ( "Query" ) ;
@@ -304,7 +318,7 @@ pub async fn build_schema(
304318 union. with_values (
305319 & possible_types
306320 . iter ( )
307- . map ( |t| ( t. clone ( ) , upper_first ( & t) ) )
321+ . map ( |t| ( t. clone ( ) , pascal_case ( & t) ) )
308322 . collect ( ) ,
309323 ) ;
310324
@@ -467,13 +481,7 @@ fn directive_type_str(directive: &Directive) -> Option<String> {
467481 . map ( input_value_type_str)
468482 . collect :: < Vec < String > > ( )
469483 . join ( "\n , " ) ;
470- let locations = directive_locations_str ( & directive. locations ) ;
471-
472- if locations. is_none ( ) {
473- return None
474- }
475-
476- let locations = locations. unwrap ( ) ;
484+ let locations = directive_locations_str ( & directive. locations ) ?;
477485
478486 Some ( format ! ( "( Directive \" {name}\" \n \" {description}\" \n {{ {args} }} \n {locations} \n )" ) )
479487}
@@ -542,7 +550,6 @@ fn build_directives(lib_path: String, role: String, directives: Vec<Directive>)
542550 "module {role}.Directives where \n {DIRECTIVE_IMPORTS}\n type Directives :: List' Type\n {directive_str}\n \n {directive_fns}"
543551 ) ) ;
544552
545-
546553 write (
547554 & format ! ( "{lib_path}/src/{role}/Directives.purs" ) ,
548555 & directive_mod,
0 commit comments