1
1
# frozen_string_literal: true
2
+
2
3
require 'erb'
3
4
require 'sass'
4
5
@@ -14,14 +15,13 @@ def initialize(parsed_schema, options)
14
15
15
16
@renderer = @options [ :renderer ] . new ( @parsed_schema , @options )
16
17
17
- %i( operations objects mutations interfaces enums unions input_objects scalars directives ) . each do |sym |
18
- if !File . exist? ( @options [ :templates ] [ sym ] )
19
- raise IOError , "`#{ sym } ` template #{ @options [ :templates ] [ sym ] } was not found"
20
- end
18
+ %i[ operations objects mutations interfaces enums unions input_objects scalars directives ] . each do |sym |
19
+ raise IOError , "`#{ sym } ` template #{ @options [ :templates ] [ sym ] } was not found" unless File . exist? ( @options [ :templates ] [ sym ] )
20
+
21
21
instance_variable_set ( "@graphql_#{ sym } _template" , ERB . new ( File . read ( @options [ :templates ] [ sym ] ) ) )
22
22
end
23
23
24
- %i( index object query mutation interface enum union input_object scalar directive ) . each do |sym |
24
+ %i[ index object query mutation interface enum union input_object scalar directive ] . each do |sym |
25
25
if @options [ :landing_pages ] [ sym ] . nil?
26
26
instance_variable_set ( "@#{ sym } _landing_page" , nil )
27
27
elsif !File . exist? ( @options [ :landing_pages ] [ sym ] )
@@ -33,7 +33,7 @@ def initialize(parsed_schema, options)
33
33
34
34
if File . extname ( ( @options [ :landing_pages ] [ sym ] ) ) == '.erb'
35
35
opts = @options . merge ( @options [ :landing_pages ] [ :variables ] ) . merge ( helper_methods )
36
- if has_yaml ?( landing_page_contents )
36
+ if yaml ?( landing_page_contents )
37
37
metadata , landing_page = split_into_metadata_and_contents ( landing_page_contents , parse : false )
38
38
erb_template = ERB . new ( landing_page )
39
39
else
@@ -60,45 +60,25 @@ def generate
60
60
create_graphql_scalar_pages
61
61
create_graphql_directive_pages
62
62
63
- unless @graphql_index_landing_page . nil?
64
- write_file ( 'static' , 'index' , @graphql_index_landing_page , trim : false )
65
- end
63
+ write_file ( 'static' , 'index' , @graphql_index_landing_page , trim : false ) unless @graphql_index_landing_page . nil?
66
64
67
- unless @graphql_object_landing_page . nil?
68
- write_file ( 'static' , 'object' , @graphql_object_landing_page , trim : false )
69
- end
65
+ write_file ( 'static' , 'object' , @graphql_object_landing_page , trim : false ) unless @graphql_object_landing_page . nil?
70
66
71
- if !@graphql_query_landing_page . nil? && !has_query
72
- write_file ( 'operation' , 'query' , @graphql_query_landing_page , trim : false )
73
- end
67
+ write_file ( 'operation' , 'query' , @graphql_query_landing_page , trim : false ) if !@graphql_query_landing_page . nil? && !has_query
74
68
75
- unless @graphql_mutation_landing_page . nil?
76
- write_file ( 'operation' , 'mutation' , @graphql_mutation_landing_page , trim : false )
77
- end
69
+ write_file ( 'operation' , 'mutation' , @graphql_mutation_landing_page , trim : false ) unless @graphql_mutation_landing_page . nil?
78
70
79
- unless @graphql_interface_landing_page . nil?
80
- write_file ( 'static' , 'interface' , @graphql_interface_landing_page , trim : false )
81
- end
71
+ write_file ( 'static' , 'interface' , @graphql_interface_landing_page , trim : false ) unless @graphql_interface_landing_page . nil?
82
72
83
- unless @graphql_enum_landing_page . nil?
84
- write_file ( 'static' , 'enum' , @graphql_enum_landing_page , trim : false )
85
- end
73
+ write_file ( 'static' , 'enum' , @graphql_enum_landing_page , trim : false ) unless @graphql_enum_landing_page . nil?
86
74
87
- unless @graphql_union_landing_page . nil?
88
- write_file ( 'static' , 'union' , @graphql_union_landing_page , trim : false )
89
- end
75
+ write_file ( 'static' , 'union' , @graphql_union_landing_page , trim : false ) unless @graphql_union_landing_page . nil?
90
76
91
- unless @graphql_input_object_landing_page . nil?
92
- write_file ( 'static' , 'input_object' , @graphql_input_object_landing_page , trim : false )
93
- end
77
+ write_file ( 'static' , 'input_object' , @graphql_input_object_landing_page , trim : false ) unless @graphql_input_object_landing_page . nil?
94
78
95
- unless @graphql_scalar_landing_page . nil?
96
- write_file ( 'static' , 'scalar' , @graphql_scalar_landing_page , trim : false )
97
- end
79
+ write_file ( 'static' , 'scalar' , @graphql_scalar_landing_page , trim : false ) unless @graphql_scalar_landing_page . nil?
98
80
99
- unless @graphql_directive_landing_page . nil?
100
- write_file ( 'static' , 'directive' , @graphql_directive_landing_page , trim : false )
101
- end
81
+ write_file ( 'static' , 'directive' , @graphql_directive_landing_page , trim : false ) unless @graphql_directive_landing_page . nil?
102
82
103
83
if @options [ :use_default_styles ]
104
84
assets_dir = File . join ( File . dirname ( __FILE__ ) , 'layouts' , 'assets' )
@@ -117,23 +97,23 @@ def generate
117
97
def create_graphql_query_pages
118
98
graphql_operation_types . each do |query_type |
119
99
metadata = ''
120
- if query_type [ :name ] == graphql_root_types [ 'query' ]
121
- unless @options [ :landing_pages ] [ :query ] . nil?
122
- query_landing_page = @options [ :landing_pages ] [ :query ]
123
- query_landing_page = File . read ( query_landing_page )
124
- if has_yaml? ( query_landing_page )
125
- pieces = yaml_split ( query_landing_page )
126
- pieces [ 2 ] = pieces [ 2 ] . chomp
127
- metadata = pieces [ 1 , 3 ] . join ( "\n " )
128
- query_landing_page = pieces [ 4 ]
129
- end
130
- query_type [ :description ] = query_landing_page
100
+ next unless query_type [ :name ] == graphql_root_types [ 'query' ]
101
+
102
+ unless @options [ :landing_pages ] [ :query ] . nil?
103
+ query_landing_page = @options [ :landing_pages ] [ :query ]
104
+ query_landing_page = File . read ( query_landing_page )
105
+ if yaml? ( query_landing_page )
106
+ pieces = yaml_split ( query_landing_page )
107
+ pieces [ 2 ] = pieces [ 2 ] . chomp
108
+ metadata = pieces [ 1 , 3 ] . join ( "\n " )
109
+ query_landing_page = pieces [ 4 ]
131
110
end
132
- opts = default_generator_options ( type : query_type )
133
- contents = @graphql_operations_template . result ( OpenStruct . new ( opts ) . instance_eval { binding } )
134
- write_file ( 'operation' , 'query' , metadata + contents )
135
- return true
111
+ query_type [ :description ] = query_landing_page
136
112
end
113
+ opts = default_generator_options ( type : query_type )
114
+ contents = @graphql_operations_template . result ( OpenStruct . new ( opts ) . instance_eval { binding } )
115
+ write_file ( 'operation' , 'query' , metadata + contents )
116
+ return true
137
117
end
138
118
false
139
119
end
@@ -229,7 +209,7 @@ def write_file(type, name, contents, trim: true)
229
209
FileUtils . mkdir_p ( path )
230
210
end
231
211
232
- if has_yaml ?( contents )
212
+ if yaml ?( contents )
233
213
# Split data
234
214
meta , contents = split_into_metadata_and_contents ( contents )
235
215
@options = @options . merge ( meta )
0 commit comments