Skip to content

Commit

Permalink
making sure interfaces are distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdsupremacist committed Jan 6, 2021
1 parent 90811e9 commit 7ec5ed1
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,26 @@ extension GraphQLObject {
.compactMap { $0 as? GraphQLObject.Type }
.map { try context.resolveInterface(object: $0) } + propertyResults.flatMap(\.interfaces)

let type = try GraphQLObjectType(name: concreteTypeName, fields: fields, interfaces: interfaces) { value, _, _ in value is Self }
let type = try GraphQLObjectType(name: concreteTypeName, fields: fields, interfaces: interfaces.distinct(by: \.name)) { value, _, _ in value is Self }

return type
}

}

extension Sequence {

fileprivate func distinct<T : Hashable>(by id: (Element) -> T) -> [Element] {
var set = Set<T>()
var result = [Element]()
for element in self {
let elementId = id(element)
if !set.contains(elementId) {
set.formUnion([elementId])
result.append(element)
}
}
return result
}

}

0 comments on commit 7ec5ed1

Please sign in to comment.