diff --git a/Package.resolved b/Package.resolved index bb92f71..799428e 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/nerdsupremacist/ContextKit.git", "state": { "branch": null, - "revision": "792019618a7245ca1bb05e8d2163decd00528927", - "version": "0.2.0" + "revision": "d68c64dd4ed4c616bb5fd58f9d19d939a1735f20", + "version": "0.2.1" } }, { diff --git a/Package.swift b/Package.swift index 06802de..7ad000a 100644 --- a/Package.swift +++ b/Package.swift @@ -15,7 +15,7 @@ let package = Package( dependencies: [ .package(url: "https://github.com/nerdsupremacist/GraphQL.git", from: "0.12.1-beta.2"), .package(url: "https://github.com/nerdsupremacist/Runtime.git", from: "2.1.2-beta.1"), - .package(url: "https://github.com/nerdsupremacist/ContextKit.git", from: "0.2.0"), + .package(url: "https://github.com/nerdsupremacist/ContextKit.git", from: "0.2.1"), ], targets: [ .target( diff --git a/Sources/GraphZahl/Resolution/Resolution.swift b/Sources/GraphZahl/Resolution/Resolution.swift index b5dca5b..2cfe9bd 100644 --- a/Sources/GraphZahl/Resolution/Resolution.swift +++ b/Sources/GraphZahl/Resolution/Resolution.swift @@ -6,9 +6,11 @@ public enum Resolution { public struct Context { let resolved: [String : GraphQLType] + let viewerContext: Any.Type - private init(resolved: [String : GraphQLType]) { + private init(resolved: [String : GraphQLType], viewerContext: Any.Type) { self.resolved = resolved + self.viewerContext = viewerContext } } @@ -17,7 +19,7 @@ public enum Resolution { extension Resolution.Context { public func appending(type: GraphQLType, as name: String) -> Resolution.Context { - return Resolution.Context(resolved: resolved.merging([name : type]) { $1 }) + return Resolution.Context(resolved: resolved.merging([name : type]) { $1 }, viewerContext: viewerContext) } public mutating func append(type: GraphQLType, as name: String) { @@ -55,6 +57,8 @@ extension Resolution.Context { extension Resolution.Context { - static let empty = Resolution.Context(resolved: [:]) + static func empty(viewerContext: Any.Type) -> Resolution.Context { + return Resolution.Context(resolved: [:], viewerContext: viewerContext) + } } diff --git a/Sources/GraphZahl/Resolution/Root/GraphQLSchema+resolve.swift b/Sources/GraphZahl/Resolution/Root/GraphQLSchema+resolve.swift index a84aeee..ab1eefa 100644 --- a/Sources/GraphZahl/Resolution/Root/GraphQLSchema+resolve.swift +++ b/Sources/GraphZahl/Resolution/Root/GraphQLSchema+resolve.swift @@ -5,7 +5,7 @@ import GraphQL extension GraphQLSchema { static func resolve() throws -> GraphQL.GraphQLSchema { - var context = Resolution.Context.empty + var context = Resolution.Context.empty(viewerContext: ViewerContext.self) let query = try Query.resolveObject(using: &context) diff --git a/Sources/GraphZahl/Utils/MethodInfo+resolve.swift b/Sources/GraphZahl/Utils/MethodInfo+resolve.swift index 4ddd06c..18bea7d 100644 --- a/Sources/GraphZahl/Utils/MethodInfo+resolve.swift +++ b/Sources/GraphZahl/Utils/MethodInfo+resolve.swift @@ -10,7 +10,9 @@ extension MethodInfo { func resolve(for receiverType: GraphQLObject.Type, using context: inout Resolution.Context) throws -> GraphQLField? { guard let returnType = returnType as? OutputResolvable.Type else { return nil } - let mappedArguments = arguments.compactMap { argument in argument.name.map { ($0, argument) } } + let viewerContext = context.viewerContext + let relevantArguments = arguments.filter { $0.type != MutableContext.self || $0.type != viewerContext } + let mappedArguments = relevantArguments.compactMap { argument in argument.name.map { ($0, argument) } } let arguments = try Dictionary(uniqueKeysWithValues: mappedArguments) .compactMapValues { argument -> GraphQLArgument? in guard let argumentType = argument.type as? InputResolvable.Type else { return nil } @@ -23,7 +25,7 @@ extension MethodInfo { return GraphQLArgument(type: type) } - guard arguments.count == arguments.count else { return nil } + guard arguments.count == relevantArguments.count else { return nil } guard arguments.count <= MethodInfo.maximumNumberOfArgumentsWithReflection else { // Print a warning in such cases to make sure the developers catch it @@ -43,7 +45,12 @@ extension MethodInfo { args: completeArguments) { (source, args, context, eventLoop, _) -> Future in let args = try args.dictionaryValue() - return try self.call(receiver: receiverType.object(from: source), argumentMap: args, context: context as! MutableContext, eventLoop: eventLoop) + let object = receiverType.object(from: source) + return try self.call(receiver: object, + argumentMap: args, + context: context as! MutableContext, + eventLoop: eventLoop, + viewerContext: viewerContext) } } @@ -54,9 +61,18 @@ extension MethodInfo { fileprivate func call(receiver: AnyObject, argumentMap: [String : Map], context: MutableContext, - eventLoop: EventLoopGroup) throws -> EventLoopFuture { + eventLoop: EventLoopGroup, + viewerContext: Any.Type) throws -> EventLoopFuture { let arguments = try self.arguments.map { argument -> Any in + if argument.type == MutableContext.self { + return context + } + + if argument.type == viewerContext { + return context.anyViewerContext + } + guard let name = argument.name, let argumentType = argument.type as? InputResolvable.Type else { fatalError() }