Skip to content

Commit 9a28499

Browse files
krbarnesdbart01
authored andcommitted
Avoid calling type(of:). Build errors when schema object has field named type. Swift 4
1 parent ffd4ce0 commit 9a28499

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

codegen/lib/graphql_swift_gen.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,15 @@ def generate_build_input_code(expr, type, wrap: true)
187187
end
188188
end
189189

190-
def deserialize_value_code(field_name, expr, type, untyped: true)
190+
def deserialize_value_code(class_name, field_name, expr, type, untyped: true)
191191
statements = ""
192192

193193
if untyped
194194
json_type = swift_json_type(type.unwrap_non_null, non_null: true)
195195
statements << "if #{expr} is NSNull { return nil }\n" unless type.non_null?
196196
statements << <<-SWIFT
197197
guard let value = #{expr} as? #{json_type} else {
198-
throw SchemaViolationError(type: type(of: self), field: fieldName, value: fieldValue)
198+
throw SchemaViolationError(type: #{class_name}.self, field: fieldName, value: fieldValue)
199199
}
200200
SWIFT
201201
expr = 'value'
@@ -208,7 +208,7 @@ def deserialize_value_code(field_name, expr, type, untyped: true)
208208
when 'LIST'
209209
untyped = !type.of_type.non_null?
210210
rethrow = "try " if %w(OBJECT INTERFACE UNION).include?(type.unwrap.kind) || untyped
211-
sub_statements = "#{rethrow}#{expr}.map { #{deserialize_value_code(field_name, '$0', type.of_type, untyped: untyped)} }"
211+
sub_statements = "#{rethrow}#{expr}.map { #{deserialize_value_code(class_name, field_name, '$0', type.of_type, untyped: untyped)} }"
212212
sub_statements += " as [Any?]" if untyped
213213
sub_statements
214214
when 'OBJECT'

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ extension <%= schema_name %> {
8787
switch fieldName {
8888
<% fields.each do |field| %>
8989
case "<%= field.name %>":
90-
<%= deserialize_value_code(field.name, 'value', field.type) %>
90+
<%= deserialize_value_code(class_name, field.name, 'value', field.type) %>
9191
<% end %>
9292
default:
93-
throw SchemaViolationError(type: type(of: self), field: fieldName, value: fieldValue)
93+
throw SchemaViolationError(type: <%= class_name %>.self, field: fieldName, value: fieldValue)
9494
}
9595
}
9696

0 commit comments

Comments
 (0)