Skip to content

Commit generated code for tests to provide more information to reviewers #17

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

Merged
merged 1 commit into from
Sep 11, 2017
Merged
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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
/Gemfile.lock
/pkg/
/tmp/
/support/Tests/GraphQLSupportTests/Generated.swift
/support/Tests/GraphQLSupportTests/Generated/
15 changes: 15 additions & 0 deletions support/Tests/GraphQLSupportTests/Generated.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Generated from graphql_swift_gen gem

open class Generated {
open static func buildQuery(_ subfields: (QueryRootQuery) -> Void) -> QueryRootQuery {
let root = QueryRootQuery()
subfields(root)
return root
}

open static func buildMutation(_ subfields: (MutationQuery) -> Void) -> MutationQuery {
let root = MutationQuery()
subfields(root)
return root
}
}
152 changes: 152 additions & 0 deletions support/Tests/GraphQLSupportTests/Generated/Entry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Generated from graphql_swift_gen gem
import Foundation

import GraphQLSupport

public protocol Entry {
var typeName: String { get }

var key: String { get }

var ttl: Date? { get }

func childResponseObjectMap() -> [GraphQL.AbstractResponse]

func responseObject() -> GraphQL.AbstractResponse
}

extension Generated {
open class EntryQuery: GraphQL.AbstractQuery, GraphQLQuery {
public typealias Response = Entry

@discardableResult
open func key(aliasSuffix: String? = nil) -> EntryQuery {
addField(field: "key", aliasSuffix: aliasSuffix)
return self
}

@discardableResult
open func ttl(aliasSuffix: String? = nil) -> EntryQuery {
addField(field: "ttl", aliasSuffix: aliasSuffix)
return self
}

override init() {
super.init()
addField(field: "__typename")
}

@discardableResult
open func onIntegerEntry(subfields: (IntegerEntryQuery) -> Void) -> EntryQuery {
let subquery = IntegerEntryQuery()
subfields(subquery)
addInlineFragment(on: "IntegerEntry", subfields: subquery)
return self
}

@discardableResult
open func onStringEntry(subfields: (StringEntryQuery) -> Void) -> EntryQuery {
let subquery = StringEntryQuery()
subfields(subquery)
addInlineFragment(on: "StringEntry", subfields: subquery)
return self
}
}

open class UnknownEntry: GraphQL.AbstractResponse, GraphQLObject, Entry {
public typealias Query = EntryQuery

open override func deserializeValue(fieldName: String, value: Any) throws -> Any? {
let fieldValue = value
switch fieldName {
case "key":
guard let value = value as? String else {
throw SchemaViolationError(type: UnknownEntry.self, field: fieldName, value: fieldValue)
}
return value

case "ttl":
if value is NSNull { return nil }
guard let value = value as? String else {
throw SchemaViolationError(type: UnknownEntry.self, field: fieldName, value: fieldValue)
}
return iso8601DateParser.date(from: value)!

Choose a reason for hiding this comment

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

🚨 If the date string is in an unexpected format, this will cause our clients to die at runtime with a fatal error that is out of their control.

Instead, the date parsing could be rolled into the guard clause so that failure to parse becomes a SchemaViolationError as well.

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 code to serialize and deserialize a date string is specified to the generator, so this would only need to be improved for the example. It would be worth checking to see what we do for our shopify ios app though.

Choose a reason for hiding this comment

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

Looks like we use essentially the same code:

return GraphQL.iso8601DateParser.date(from: value)!


default:
throw SchemaViolationError(type: UnknownEntry.self, field: fieldName, value: fieldValue)
}
}

open var typeName: String { return field(field: "__typename") as! String }

open static func create(fields: [String: Any]) throws -> Entry {
guard let typeName = fields["__typename"] as? String else {
throw SchemaViolationError(type: UnknownEntry.self, field: "__typename", value: fields["__typename"] ?? NSNull())
}
switch typeName {
case "IntegerEntry":
return try IntegerEntry.init(fields: fields)

case "StringEntry":
return try StringEntry.init(fields: fields)

default:
return try UnknownEntry.init(fields: fields)
}
}

open var key: String {
return internalGetKey()
}

func internalGetKey(aliasSuffix: String? = nil) -> String {
return field(field: "key", aliasSuffix: aliasSuffix) as! String
}

open var ttl: Date? {
return internalGetTtl()
}

func internalGetTtl(aliasSuffix: String? = nil) -> Date? {
return field(field: "ttl", aliasSuffix: aliasSuffix) as! Date?
}

override open func childObjectType(key: String) -> GraphQL.ChildObjectType {
switch(key) {
case "key":

return .scalar

case "ttl":

return .scalar

default:
return .scalar
}
}

override open func fetchChildObject(key: String) -> GraphQL.AbstractResponse? {
switch(key) {
default:
break
}
return nil
}

override open func fetchChildObjectList(key: String) -> [GraphQL.AbstractResponse] {
switch(key) {
default:
return []
}
}

open func childResponseObjectMap() -> [GraphQL.AbstractResponse] {
return []
}

open func responseObject() -> GraphQL.AbstractResponse {
return self as GraphQL.AbstractResponse
}
}
}
99 changes: 99 additions & 0 deletions support/Tests/GraphQLSupportTests/Generated/EntryUnion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Generated from graphql_swift_gen gem
import Foundation

import GraphQLSupport

public protocol EntryUnion {
var typeName: String { get }

func childResponseObjectMap() -> [GraphQL.AbstractResponse]

func responseObject() -> GraphQL.AbstractResponse
}

extension Generated {
open class EntryUnionQuery: GraphQL.AbstractQuery, GraphQLQuery {
public typealias Response = EntryUnion

override init() {
super.init()
addField(field: "__typename")
}

@discardableResult
open func onIntegerEntry(subfields: (IntegerEntryQuery) -> Void) -> EntryUnionQuery {
let subquery = IntegerEntryQuery()
subfields(subquery)
addInlineFragment(on: "IntegerEntry", subfields: subquery)
return self
}

@discardableResult
open func onStringEntry(subfields: (StringEntryQuery) -> Void) -> EntryUnionQuery {
let subquery = StringEntryQuery()
subfields(subquery)
addInlineFragment(on: "StringEntry", subfields: subquery)
return self
}
}

open class UnknownEntryUnion: GraphQL.AbstractResponse, GraphQLObject, EntryUnion {
public typealias Query = EntryUnionQuery

open override func deserializeValue(fieldName: String, value: Any) throws -> Any? {
let fieldValue = value
switch fieldName {
default:
throw SchemaViolationError(type: UnknownEntryUnion.self, field: fieldName, value: fieldValue)
}
}

open var typeName: String { return field(field: "__typename") as! String }

open static func create(fields: [String: Any]) throws -> EntryUnion {
guard let typeName = fields["__typename"] as? String else {
throw SchemaViolationError(type: UnknownEntryUnion.self, field: "__typename", value: fields["__typename"] ?? NSNull())
}
switch typeName {
case "IntegerEntry":
return try IntegerEntry.init(fields: fields)

case "StringEntry":
return try StringEntry.init(fields: fields)

default:
return try UnknownEntryUnion.init(fields: fields)
}
}

override open func childObjectType(key: String) -> GraphQL.ChildObjectType {
switch(key) {
default:
return .scalar
}
}

override open func fetchChildObject(key: String) -> GraphQL.AbstractResponse? {
switch(key) {
default:
break
}
return nil
}

override open func fetchChildObjectList(key: String) -> [GraphQL.AbstractResponse] {
switch(key) {
default:
return []
}
}

open func childResponseObjectMap() -> [GraphQL.AbstractResponse] {
return []
}

open func responseObject() -> GraphQL.AbstractResponse {
return self as GraphQL.AbstractResponse
}
}
}
Loading