Skip to content

Commit b9d26ea

Browse files
committed
Prune dead code, add doc comments, adjust naming and minor refactor.
1 parent b387aa8 commit b9d26ea

File tree

4 files changed

+184
-102
lines changed

4 files changed

+184
-102
lines changed

codegen/lib/graphql_swift_gen.rb

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,45 @@ def deserialize_value_code(field_name, expr, type, untyped: true)
218218
raise NotImplementedError, "Unexpected #{type.kind} argument type"
219219
end
220220
end
221-
221+
222+
def generate_input_init(type)
223+
text = "public init("
224+
input_fields = type.required_input_fields + type.optional_input_fields
225+
input_fields.each do |field|
226+
text << escape_reserved_word(field.camelize_name)
227+
text << ": "
228+
text << swift_input_type(field.type)
229+
text << (field.type.non_null? ? "" : " = nil")
230+
text << (field == input_fields.last ? "" : ", ")
231+
end
232+
text << ")"
233+
text << " {\n"
234+
type.input_fields.each do |field|
235+
name = escape_reserved_word(field.camelize_name)
236+
text << "self." + name + " = " + name
237+
text << "\n"
238+
end
239+
text << "}"
240+
end
241+
242+
def remove_linebreaks(text)
243+
text.gsub("\n", " ")
244+
end
245+
246+
def input_field_description(type)
247+
unless type.input_fields.count == 0
248+
text = "/// - parameters:" + ""
249+
type.input_fields.each do |field|
250+
description = (field.description.nil? ? "No description" : remove_linebreaks(field.description))
251+
text << "\n/// - " + field.name + ": " + description
252+
end
253+
text << "\n///"
254+
text
255+
end
256+
end
257+
222258
def swift_arg_defs(field)
223-
defs = ["aliasSuffix: String? = nil"]
259+
defs = ["alias: String? = nil"]
224260
field.args.each do |arg|
225261
arg_def = "#{escape_reserved_word(arg.name)}: #{swift_input_type(arg.type)}"
226262
arg_def << " = nil" unless arg.type.non_null?
@@ -242,16 +278,72 @@ def generate_append_objects_code(expr, type, non_null: false)
242278
end
243279
return "#{expr}.forEach {\n#{generate_append_objects_code('$0', type.of_type)}\n}" if type.list?
244280

245-
abstract_response = type.object? ? expr : "#{expr} as! GraphQL.AbstractResponse"
281+
abstract_response = type.object? ? expr : "(#{expr} as! GraphQL.AbstractResponse)"
246282
"response.append(#{abstract_response})\n" \
247-
"response.append(contentsOf: #{expr}.childResponseObjectMap())"
283+
"response.append(contentsOf: #{abstract_response}.childResponseObjectMap())"
248284
end
249285

250286
def swift_attributes(deprecatable)
251287
return unless deprecatable.deprecated?
252288
if deprecatable.deprecation_reason
253289
message_argument = ", message:#{deprecatable.deprecation_reason.inspect}"
254290
end
255-
"@available(*, deprecated#{message_argument})"
291+
"@available(*, deprecated#{message_argument})\n"
292+
end
293+
294+
def swift_doc(element, include_args=true)
295+
doc = ''
296+
297+
unless element.description.nil?
298+
description = element.description
299+
description = wrap_text(description, '/// ')
300+
doc << "\n\n" + description
301+
end
302+
303+
if include_args && element.respond_to?(:args)
304+
if element.args.count > 0
305+
doc << "\n///\n"
306+
doc << "/// - parameters:"
307+
element.args.each do |arg|
308+
doc << "\n"
309+
doc << '/// - ' + arg.name + ': ' + (arg.description.nil? ? "No description" : format_arg_list(arg.description, 7))
310+
end
311+
doc << "\n///"
312+
end
313+
end
314+
doc
315+
end
316+
317+
def wrap_text(text, prefix, width=80)
318+
container = ''
319+
line = "" + prefix
320+
321+
parts = text.split(" ")
322+
parts.each do |part|
323+
if line.length + part.length < width
324+
line << part
325+
line << ' '
326+
else
327+
container << line
328+
container << "\n"
329+
line = "" + prefix
330+
line << part
331+
line << ' '
332+
end
333+
end
334+
335+
if line.length > 0
336+
container << line
337+
end
338+
container
339+
end
340+
341+
def format_arg_list(text, spacing)
342+
parts = text.split("\n")
343+
commented = parts.drop(1).map do |part|
344+
"/// " + (" " * spacing) + part
345+
end
346+
commented.unshift(parts.first)
347+
commented.join("\n")
256348
end
257349
end

codegen/lib/graphql_swift_gen/templates/ApiSchema.swift.erb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
// Generated from <%= script_name %>
1+
//
2+
// <%= schema_name %>.swift
3+
// Buy
4+
//
5+
// Created by Shopify.
6+
// Copyright (c) 2017 Shopify Inc. All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in
16+
// all copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
// THE SOFTWARE.
25+
//
226

327
open class <%= schema_name %> {
428
<% [['Query', schema.query_root_name], ['Mutation', schema.mutation_root_name]].each do |operation_type, root_name| %>

0 commit comments

Comments
 (0)