Skip to content

InputValue [4 of 4] #14

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
97 changes: 86 additions & 11 deletions codegen/lib/graphql_swift_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,27 @@ def reformat(code)
Reformatter.new(indent: "\t").reformat(code)
end

def swift_input_type(type, non_null: false)
def swift_input_type(type, non_null: false, wrapped: false)
code = case type.kind
when 'NON_NULL'
return swift_input_type(type.of_type, non_null: true)
return swift_input_type(type.of_type, non_null: true, wrapped: wrapped)
when 'SCALAR'
scalars[type.name].swift_type
when 'LIST'
"[#{swift_input_type(type.of_type, non_null: true)}]"
"[#{swift_input_type(type.of_type, non_null: true, wrapped: wrapped)}]"
when 'INPUT_OBJECT', 'ENUM'
type.name
else
raise NotImplementedError, "Unhandled #{type.kind} input type"
end
code += "?" unless non_null

if wrapped
if !non_null
code = "InputValue<#{code}>"
end
else
code += "?" unless non_null
end
code
end

Expand Down Expand Up @@ -223,22 +230,90 @@ def deserialize_value_code(field_name, expr, type, untyped: true)
end

def generate_input_init(type)
text = "public init("
text = "public static func create("
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, wrapped: true)
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like a painful way to generate code 😕 I think graphql_swift_gen.rb is more of a place to do some calculated logic rather then template the entire init. Having a function that is constantly appening to a string isn't nearly as readable or friendly as using embedded ruby. This should be in type.swift.erb.

text << (field.type.non_null? ? "" : " = .undefined")
text << (field == input_fields.last ? "" : ", ")
end

text << ") -> #{type.name} {"
text << "\n"
text << "return #{type.name}("
text << input_fields.map { |field|
"#{field.name}: #{field.name}"
}.join(", ")
text << ")\n"
text << "}"
end

def deprecated_input_init_required(type)
type.input_fields.each do |field|
unless field.type.non_null?
return true
end
end
false
end

def generate_private_input_init(type)
text = "private 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, wrapped: true)
text << (field.type.non_null? ? "" : " = .undefined")
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

def generate_deprecated_input_init(type)
convenience = deprecated_input_init_required(type) ? "convenience " : ""
deprecation = deprecated_input_init_required(type) ? "@available(*, deprecated)\n" : ""
text = "#{deprecation}public #{convenience}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 << swift_input_type(field.type, wrapped: false)
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

if deprecated_input_init_required(type)
text << "self.init("
text << input_fields.map { |field|
param = "#{field.name}: #{field.name}"
if !field.type.non_null?
param << ".orNull"
end
param
}.join(", ")
text << ")\n"
else
type.input_fields.each do |field|
name = escape_reserved_word(field.camelize_name)
text << "self." + name + " = " + name
if !field.type.non_null?
text << ".orNull"
end
text << "\n"
end
end
text << "}"
end

Expand Down
23 changes: 17 additions & 6 deletions codegen/lib/graphql_swift_gen/templates/type.swift.erb
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,32 @@ extension <%= schema_name %> {
<% type.input_fields.each do |field| %>

<%= swift_doc(field) %>
open var <%= escape_reserved_word(field.camelize_name) %>: <%= swift_input_type(field.type) %>
open var <%= escape_reserved_word(field.camelize_name) %>: <%= swift_input_type(field.type, wrapped: true) %>
<% end %>
/// Creates the input object.
///
<%= input_field_description(type) %>
<%= generate_input_init(type) %>

<% if deprecated_input_init_required(type) %>
<%= generate_private_input_init(type) %>
<% end %>

/// Creates the input object.
///
<%= input_field_description(type) %>
<%= generate_deprecated_input_init(type) %>

internal func serialize() -> String {
var fields: [String] = []
<% type.input_fields.each do |field| %>
<% unless field.type.non_null? %>
if let <%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %> {
<% end %>
fields.append("<%= field.name %>:<%= generate_build_input_code(field.camelize_name, field.type.unwrap_non_null) %>")
<% unless field.type.non_null? %>
<% if field.type.non_null? %>
fields.append("<%= field.name %>:<%= generate_build_input_code(field.camelize_name, field.type.unwrap_non_null) %>")
<% else %>
switch <%= escape_reserved_word(field.camelize_name) %> {
case .some(let <%= escape_reserved_word(field.camelize_name) %>): fields.append("<%= field.name %>:<%= generate_build_input_code(field.camelize_name, field.type.unwrap_non_null) %>")
case .null: fields.append("<%= field.name %>:null")
case .undefined: break
}
<% end %>
<% end %>
Expand Down
32 changes: 32 additions & 0 deletions support/Sources/GraphQL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,38 @@ public struct SchemaViolationError: Error {
}
}

public enum InputValue<T> {
case some(T)
case null
case undefined

public init(orNull optional: Optional<T>) {
if let value = optional {
self = .some(value)
} else {
self = .null
}
}

public init(orUndefined optional: Optional<T>) {
if let value = optional {
self = .some(value)
} else {
self = .undefined
}
}
}

public extension Optional {
public var orNull: InputValue<Wrapped> {
return InputValue(orNull: self)
}

public var orUndefined: InputValue<Wrapped> {
return InputValue(orUndefined: self)
}
Copy link
Contributor

@BenEmdon BenEmdon Sep 8, 2017

Choose a reason for hiding this comment

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

These optional extensions feel redundant to me. Not everyone will want to use them. These could be removed from the generator. Consumers that want this extension can always build it themselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True. Agreed.

}

extension GraphQL.Selection: Equatable {}
public func ==(lhs: GraphQL.Selection, rhs: GraphQL.Selection) -> Bool {
return (lhs === rhs) || (lhs.field == rhs.field && lhs.alias == rhs.alias && lhs.args == rhs.args && lhs.subfields == rhs.subfields)
Expand Down