22
33module Gql
44 module GqlGeneratorBase
5- extend ActiveSupport :: Concern
5+ protected
66
7- included do
8- protected
7+ # Generate a namedspaced class name with the mutation prefix
8+ def prefixed_class_name ( prefix )
9+ ( class_path + [ "#{ prefix } _#{ file_name } " ] ) . map! ( &:camelize ) . join ( "::" )
10+ end
911
10- # Generate a namedspaced class name with the mutation prefix
11- def prefixed_class_name ( prefix )
12- ( class_path + [ "#{ prefix } _#{ file_name } " ] ) . map! ( &:camelize ) . join ( "::" )
13- end
12+ def type_map
13+ {
14+ integer : 'Int' ,
15+ string : 'String' ,
16+ boolean : 'Boolean' ,
17+ decimal : 'Float' ,
18+ datetime : 'GraphQL::Types::ISO8601DateTime' ,
19+ date : 'GraphQL::Types::ISO8601Date' ,
20+ hstore : 'GraphQL::Types::JSON' ,
21+ text : 'String' ,
22+ json : 'GraphQL::Types::JSON' ,
23+ jsonb : 'GraphQL::Types::JSON'
24+ }
25+ end
1426
15- def type_map
16- {
17- integer : 'Int' ,
18- string : 'String' ,
19- boolean : 'Boolean' ,
20- decimal : 'Float' ,
21- datetime : 'GraphQL::Types::ISO8601DateTime' ,
22- date : 'GraphQL::Types::ISO8601Date' ,
23- hstore : 'GraphQL::Types::JSON' ,
24- text : 'String' ,
25- json : 'GraphQL::Types::JSON'
26- }
27- end
28-
29- def map_model_types ( model_name )
30- klass = model_name . constantize
31- associations = klass . reflect_on_all_associations ( :belongs_to )
32- bt_columns = associations . map ( &:foreign_key )
33-
34- klass . columns
35- . reject { |col | bt_columns . include? ( col . name ) }
36- . reject { |col | type_map [ col . type ] . nil? }
37- . map { |col | { name : col . name , gql_type : type_map [ col . type ] } }
27+ def map_model_types ( model_name )
28+ klass = model_name . constantize
29+ associations = klass . reflect_on_all_associations ( :belongs_to )
30+ bt_columns = associations . map ( &:foreign_key )
31+
32+ klass . columns
33+ . reject { |col | bt_columns . include? ( col . name ) }
34+ . reject { |col | type_map [ col . type ] . nil? }
35+ . map do |col |
36+ {
37+ name : col . name ,
38+ null : col . null ,
39+ gql_type : klass . primary_key == col . name ? 'GraphQL::Types::ID' : type_map [ col . type ]
40+ }
41+ end
42+ end
43+
44+ def root_directory ( namespace )
45+ "app/graphql/#{ namespace . underscore } "
46+ end
47+
48+ def wrap_in_namespace ( namespace )
49+ namespace = namespace . split ( '::' )
50+ namespace . shift if namespace [ 0 ] . empty?
51+
52+ code = namespace . each_with_index . map { |name , i | " " * i + "module #{ name } " } . join ( "\n " )
53+ code << "\n " << yield ( namespace . size ) << "\n "
54+ code << ( namespace . size - 1 ) . downto ( 0 ) . map { |i | " " * i + "end" } . join ( "\n " )
55+ code
56+ end
57+
58+ def class_with_fields ( namespace , name , superclass , fields )
59+ wrap_in_namespace ( namespace ) do |indent |
60+ klass = [ ]
61+ klass << sprintf ( "%sclass %s < %s" , " " * indent , name , superclass )
62+
63+ fields . each do |field |
64+ klass << sprintf ( "%sfield :%s, %s, null: %s" , " " * ( indent + 1 ) , field [ :name ] , field [ :gql_type ] , field [ :null ] )
65+ end
66+
67+ klass << sprintf ( "%send" , " " * indent )
68+ klass . join ( "\n " )
3869 end
3970 end
4071 end
41- end
72+ end
0 commit comments