Skip to content

Docs [3 of 4] #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 97 additions & 5 deletions codegen/lib/graphql_swift_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,45 @@ def deserialize_value_code(field_name, expr, type, untyped: true)
raise NotImplementedError, "Unexpected #{type.kind} argument type"
end
end


def generate_input_init(type)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why move the template code into a function that builds a string? This seems like a step backwards in readability

text = "public init("
input_fields = type.required_input_fields + type.optional_input_fields
input_fields.each do |field|
text << escape_reserved_word(field.camelize_name)
text << ": "
text << swift_input_type(field.type)
text << (field.type.non_null? ? "" : " = nil")
text << (field == input_fields.last ? "" : ", ")
end
text << ")"
text << " {\n"
type.input_fields.each do |field|
name = escape_reserved_word(field.camelize_name)
text << "self." + name + " = " + name
text << "\n"
end
text << "}"
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you just converted the code from the ERB file into a ruby method with string concatenation. I find the code more readable the way it was before. I also find PRs with miscellaneous changes like this a lot harder to review, since a refactor can hide functional changes.


def remove_linebreaks(text)
text.gsub("\n", " ")
end

def input_field_description(type)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done at the ERB layer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason it's done here is to avoid duplicating the logic across the whole .erb file.

unless type.input_fields.count == 0
text = "/// - parameters:" + ""
type.input_fields.each do |field|
description = (field.description.nil? ? "No description" : remove_linebreaks(field.description))
text << "\n/// - " + field.name + ": " + description
end
text << "\n///"
text
end
end

def swift_arg_defs(field)
defs = ["aliasSuffix: String? = nil"]
defs = ["alias: String? = nil"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These "minor renames" are actually breaking changes. Also, the reason why it isn't just called alias is because we prefix what is passed in with the field name so that there aren't conflicts between fields. By just calling it alias it makes it sound like it is used literally as the GraphQL alias, which makes it seem like the caller needs to worry about preventing conflicts with other fields of the same name.

field.args.each do |arg|
arg_def = "#{escape_reserved_word(arg.name)}: #{swift_input_type(arg.type)}"
arg_def << " = nil" unless arg.type.non_null?
Expand All @@ -245,17 +281,73 @@ def generate_append_objects_code(expr, type, non_null: false)
end
return "#{expr}.forEach {\n#{generate_append_objects_code('$0', type.of_type)}\n}" if type.list?

abstract_response = type.object? ? expr : "#{expr} as! GraphQL.AbstractResponse"
abstract_response = type.object? ? expr : "(#{expr} as! GraphQL.AbstractResponse)"
"response.append(#{abstract_response})\n" \
"response.append(contentsOf: #{expr}.childResponseObjectMap())"
"response.append(contentsOf: #{abstract_response}.childResponseObjectMap())"
end

def swift_attributes(deprecatable)
return unless deprecatable.deprecated?
if deprecatable.deprecation_reason
message_argument = ", message:#{deprecatable.deprecation_reason.inspect}"
end
"@available(*, deprecated#{message_argument})"
"@available(*, deprecated#{message_argument})\n"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the addition of the \n? existing code seems to generate without this being necessary

end

def swift_doc(element, include_args=true)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also be at the ERB layer

doc = ''

unless element.description.nil?
description = element.description
description = wrap_text(description, '/// ')
doc << "\n\n" + description
end

if include_args && element.respond_to?(:args)
if element.args.count > 0
doc << "\n///\n"
doc << "/// - parameters:"
element.args.each do |arg|
doc << "\n"
doc << '/// - ' + arg.name + ': ' + (arg.description.nil? ? "No description" : format_arg_list(arg.description, 7))
end
doc << "\n///"
Copy link
Contributor

@dylanahsmith dylanahsmith Sep 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the existing coding style. Specifically, here it looks like you are indenting with tabs instead of two spaces per indent

end
end
doc
end

def wrap_text(text, prefix, width=80)
container = ''
line = "" + prefix

parts = text.split(" ")
parts.each do |part|
if line.length + part.length < width
line << part
line << ' '
else
container << line
container << "\n"
line = "" + prefix
line << part
line << ' '
end
end

if line.length > 0
container << line
end
container
end

def format_arg_list(text, spacing)
parts = text.split("\n")
commented = parts.drop(1).map do |part|
"/// " + (" " * spacing) + part
end
commented.unshift(parts.first)
commented.join("\n")
end

def swift_protocols(type)
Expand Down
26 changes: 25 additions & 1 deletion codegen/lib/graphql_swift_gen/templates/ApiSchema.swift.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
// Generated from <%= script_name %>
//
// <%= schema_name %>.swift
// Buy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not always the BUY SDK and idea was brought up in #14 (comment) for a solution

//
// Created by Shopify.
// Copyright (c) 2017 Shopify Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

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