From 625b0957d91dd1fa11e261debdb6082b436ab7fc Mon Sep 17 00:00:00 2001 From: Mathias Quintero Date: Mon, 4 Jan 2021 01:21:44 +0100 Subject: [PATCH] Removing getters from property wrappers from objects --- .../Object/GraphQLObject+resolve.swift | 18 +++--------------- .../CustomGraphQLProperty.swift | 0 .../Object/PropertyWrappers}/Ignore.swift | 13 ++++++++++++- .../Object/{ => PropertyWrappers}/Inline.swift | 0 .../PropertyResult.swift | 3 +++ 5 files changed, 18 insertions(+), 16 deletions(-) rename Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/{ => PropertyWrappers}/CustomGraphQLProperty.swift (100%) rename Sources/GraphZahl/{Utils => Resolution/OutputResolvable/Implementations/Object/PropertyWrappers}/Ignore.swift (57%) rename Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/{ => PropertyWrappers}/Inline.swift (100%) rename Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/{ => PropertyWrappers}/PropertyResult.swift (90%) diff --git a/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/GraphQLObject+resolve.swift b/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/GraphQLObject+resolve.swift index 24963f6..601c838 100644 --- a/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/GraphQLObject+resolve.swift +++ b/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/GraphQLObject+resolve.swift @@ -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 } diff --git a/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/CustomGraphQLProperty.swift b/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyWrappers/CustomGraphQLProperty.swift similarity index 100% rename from Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/CustomGraphQLProperty.swift rename to Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyWrappers/CustomGraphQLProperty.swift diff --git a/Sources/GraphZahl/Utils/Ignore.swift b/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyWrappers/Ignore.swift similarity index 57% rename from Sources/GraphZahl/Utils/Ignore.swift rename to Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyWrappers/Ignore.swift index b5dc867..88bf05f 100644 --- a/Sources/GraphZahl/Utils/Ignore.swift +++ b/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyWrappers/Ignore.swift @@ -1,8 +1,9 @@ import Foundation +import Runtime @propertyWrapper -public struct Ignore { +public struct Ignore { public var wrappedValue: T public init(wrappedValue: T) { @@ -10,6 +11,16 @@ public struct Ignore { } } +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 { diff --git a/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/Inline.swift b/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyWrappers/Inline.swift similarity index 100% rename from Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/Inline.swift rename to Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyWrappers/Inline.swift diff --git a/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyResult.swift b/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyWrappers/PropertyResult.swift similarity index 90% rename from Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyResult.swift rename to Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyWrappers/PropertyResult.swift index f13b44c..c8b151a 100644 --- a/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyResult.swift +++ b/Sources/GraphZahl/Resolution/OutputResolvable/Implementations/Object/PropertyWrappers/PropertyResult.swift @@ -4,6 +4,7 @@ import GraphQL enum PropertyResult { case field(String, GraphQLField) case interface(GraphQLInterfaceType, fields: [String : GraphQLField]) + case ignore } extension PropertyResult { @@ -14,6 +15,8 @@ extension PropertyResult { return [name : field] case .interface(_, let fields): return fields + case .ignore: + return [:] } }