Skip to content

Commit

Permalink
Adding outtput delegation API
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdsupremacist committed May 29, 2020
1 parent c92029e commit 87c66ff
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

import Foundation
import ContextKit
import NIO
import GraphQL

protocol DelegatedOutputResolvable: OutputResolvable {
associatedtype Output: OutputResolvable

func resolve(source: Any,
arguments: [String : Map],
context: MutableContext,
eventLoop: EventLoopGroup) throws -> Output
}

extension DelegatedOutputResolvable {

/**
- Warning: default implementation from `GraphZahl`. Do not override unless you know exactly what you are doing.
*/
public static var additionalArguments: [String : InputResolvable.Type] {
return Output.additionalArguments
}

/**
- Warning: default implementation from `GraphZahl`. Do not override unless you know exactly what you are doing.
*/
public static func reference(using context: inout Resolution.Context) throws -> GraphQLOutputType {
return try context.reference(for: Output.self)
}

/**
- Warning: default implementation from `GraphZahl`. Do not override unless you know exactly what you are doing.
*/
public static func resolve(using context: inout Resolution.Context) throws -> GraphQLOutputType {
return try context.resolve(type: Output.self)
}

/**
- Warning: default implementation from `GraphZahl`. Do not override unless you know exactly what you are doing.
*/
public func resolve(source: Any,
arguments: [String : Map],
context: MutableContext,
eventLoop: EventLoopGroup) throws -> EventLoopFuture<Any?> {

return try resolve(source: source, arguments: arguments, context: context, eventLoop: eventLoop)
.resolve(source: source, arguments: arguments, context: context, eventLoop: eventLoop)
}


}
22 changes: 22 additions & 0 deletions Sources/GraphZahlTests/SchemaResolutionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ class SchemaResolutionTests: XCTestCase {
XCTAssertEqual(expectedData, result.data)
}

func testDelegatedOutputs() throws {
struct SomeInt: DelegatedOutputResolvable {
let int: Int

func resolve(source: Any, arguments: [String : Map], context: MutableContext, eventLoop: EventLoopGroup) throws -> some OutputResolvable {
return int
}
}

var context = Resolution.Context.empty(viewerContextType: Int.self, viewerContext: 42)
let output = try SomeInt.resolve(using: &context)
guard let nonNull = output as? GraphQLNonNull else {
XCTFail("outptut of some int is nullable")
return
}
guard let scalar = nonNull.ofType as? GraphQLScalarType else {
XCTFail("outptut of some int is not a scalar")
return
}
XCTAssertEqual(scalar, GraphQLInt)
}

func testKeypaths() throws {
let query = """
{
Expand Down

0 comments on commit 87c66ff

Please sign in to comment.