Skip to content

Commit

Permalink
Removing getters from property wrappers from objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdsupremacist committed Jan 4, 2021
1 parent 1b83fdb commit 625b095
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,15 @@ extension GraphQLObject {
static func resolveObject(using context: inout Resolution.Context) throws -> GraphQLObjectType {
let (typeProperties, typeMethods, inheritance) = try typeInfo(of: Self.self, .properties, .methods, .inheritance)

let gettersThatShouldBeIgnored = Set(typeProperties.filter { $0.type is CustomGraphQLProperty.Type }.map { $0.name.deleting(prefix: "_") })
let propertyResults = try typeProperties.compactMap { try $0.resolve(for: Self.self, using: &context) }
let properties = propertyResults.reduce([:]) { dictionary, result in
return dictionary.merging(result.fieldMap) { first, _ in first }
}

let methodMap = Dictionary(typeMethods.map { ($0.methodName.deleting(prefix: "$"), $0) }) { first, _ in first }
let methods = try methodMap.compactMapValues { try $0.resolve(for: Self.self, using: &context) }

let fields = properties.merging(methods) { property, method in
guard method.args.isEmpty else { return method }

if property.type.debugDescription == method.type.debugDescription {
return property
}

if property.args.isEmpty {
return method
} else {
return property
}
}
let methods = try methodMap.filter { !gettersThatShouldBeIgnored.contains($0.key) }.compactMapValues { try $0.resolve(for: Self.self, using: &context) }
let fields = properties.merging(methods) { $1 }

let interfaces = try inheritance
.compactMap { $0 as? GraphQLObject.Type }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@

import Foundation
import Runtime

@propertyWrapper
public struct Ignore<T> {
public struct Ignore<T : OutputResolvable> {
public var wrappedValue: T

public init(wrappedValue: T) {
self.wrappedValue = wrappedValue
}
}

extension Ignore: CustomGraphQLProperty {

static func resolve(with property: PropertyInfo,
for receiverType: GraphQLObject.Type,
using context: inout Resolution.Context) throws -> PropertyResult {
return .ignore
}

}

extension Ignore: Encodable where T: Encodable {

public func encode(to encoder: Encoder) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import GraphQL
enum PropertyResult {
case field(String, GraphQLField)
case interface(GraphQLInterfaceType, fields: [String : GraphQLField])
case ignore
}

extension PropertyResult {
Expand All @@ -14,6 +15,8 @@ extension PropertyResult {
return [name : field]
case .interface(_, let fields):
return fields
case .ignore:
return [:]
}
}

Expand Down

0 comments on commit 625b095

Please sign in to comment.