Skip to content

Commit

Permalink
Allowing viewerContext and mutable context usage inside of methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdsupremacist committed Mar 22, 2020
1 parent 37708d9 commit bca3a09
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 7 additions & 3 deletions Sources/GraphZahl/Resolution/Resolution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
24 changes: 20 additions & 4 deletions Sources/GraphZahl/Utils/MethodInfo+resolve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand All @@ -43,7 +45,12 @@ extension MethodInfo {
args: completeArguments) { (source, args, context, eventLoop, _) -> Future<Any?> 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)
}
}

Expand All @@ -54,9 +61,18 @@ extension MethodInfo {
fileprivate func call(receiver: AnyObject,
argumentMap: [String : Map],
context: MutableContext,
eventLoop: EventLoopGroup) throws -> EventLoopFuture<Any?> {
eventLoop: EventLoopGroup,
viewerContext: Any.Type) throws -> EventLoopFuture<Any?> {

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() }

Expand Down

0 comments on commit bca3a09

Please sign in to comment.