Skip to content

Commit

Permalink
Add UUID scalar.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulofaria committed Jun 27, 2020
1 parent 78878bd commit 6a95768
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion Sources/Graphiti/ComponentsInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public final class ComponentsInitializer<RootType : Keyable, Context> {
},
parse: { map in
guard let string = map.string else {
throw GraphQLError(message: "Invalid type for URL scalar.")
throw GraphQLError(message: "Invalid type for URL scalar. Expected string, but got \(map.typeDescription)")
}

guard let url = URL(string: string) else {
Expand All @@ -226,4 +226,25 @@ public final class ComponentsInitializer<RootType : Keyable, Context> {
}
)
}

@discardableResult
public func uuidScalar() -> ComponentInitializer<RootType, Context> {
scalar(
UUID.self,
serialize: { uuid in
.string(uuid.uuidString)
},
parse: { map in
guard let string = map.string else {
throw GraphQLError(message: "Invalid type for UUID scalar. Expected string, but got \(map.typeDescription)")
}

guard let uuid = UUID(uuidString: string) else {
throw GraphQLError(message: "Invalid uuid string for UUID scalar.")
}

return uuid
}
)
}
}

0 comments on commit 6a95768

Please sign in to comment.