Skip to content

feature: Enum parsing throws instead of null fallback #107

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
Changes from 1 commit
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
40 changes: 30 additions & 10 deletions Sources/GraphQL/Type/Definition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1012,23 +1012,43 @@ public final class GraphQLEnumType {
}

public func serialize(value: Any) throws -> Map {
return try valueLookup[map(from: value)].map { .string($0.name) } ?? .null
let mapValue = try map(from: value)
guard let enumValue = valueLookup[mapValue] else {
throw GraphQLError(
message: "Enum '\(name)' cannot represent value '\(mapValue)'."
)
}
return .string(enumValue.name)
}

public func parseValue(value: Map) throws -> Map {
if case let .string(value) = value {
return nameLookup[value]?.value ?? .null
guard let valueStr = value.string else {
throw GraphQLError(
message: "Enum '\(name)' cannot represent non-string value '\(value)'."
)
}

return .null
guard let enumValue = nameLookup[valueStr] else {
throw GraphQLError(
message: "Value '\(valueStr)' does not exist in '\(name)' enum."
)
}
return enumValue.value
}

public func parseLiteral(valueAST: Value) -> Map {
if let enumValue = valueAST as? EnumValue {
return nameLookup[enumValue.value]?.value ?? .null
public func parseLiteral(valueAST: Value) throws -> Map {
guard let enumNode = valueAST as? EnumValue else {
throw GraphQLError(
message: "Enum '\(name)' cannot represent non-enum value '\(valueAST)'.",
nodes: [valueAST]
)
}

return .null
guard let enumValue = nameLookup[enumNode.value] else {
throw GraphQLError(
message: "Value '\(enumNode)' does not exist in '\(name)' enum.",
nodes: [valueAST]
)
}
return enumValue.value
}
}

Expand Down