Skip to content

Commit

Permalink
Making value resolvable not mandatory for inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdsupremacist committed Apr 15, 2020
1 parent 611f513 commit e9b2189
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import GraphQL

extension Array: ValueResolvable where Element: ValueResolvable {

public func map() throws -> Map? {
return .array(try compactMap { try $0.map() })
public func map() throws -> Map {
return .array(try map { try $0.map() })
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Runtime

private var propertiesForType = [Int : [String : PropertyInfo]]()

public protocol GraphQLInputObject: InputResolvable, ConcreteResolvable, KeyPathListable { }
public protocol GraphQLInputObject: InputResolvable, ConcreteResolvable, ValueResolvable, KeyPathListable { }

extension GraphQLInputObject {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import GraphQL

extension Optional: ValueResolvable where Wrapped: ValueResolvable {

public func map() throws -> Map? {
guard let self = self else { return .null }
return try self.map()
public func map() throws -> Map {
return try map { try $0.map() } ?? .null
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import GraphQL

extension RawRepresentable where Self: ValueResolvable, RawValue: ValueResolvable {

public func map() throws -> Map? {
public func map() throws -> Map {
return try rawValue.map()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Foundation
import GraphQL

public protocol InputResolvable: ValueResolvable {
public protocol InputResolvable: Resolvable {
static func resolve(using context: inout Resolution.Context) throws -> GraphQLInputType

static func create(from map: Map) throws -> Self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import Foundation
import GraphQL

public protocol ValueResolvable: Resolvable {
func map() throws -> Map?
func map() throws -> Map
}
4 changes: 2 additions & 2 deletions Sources/GraphZahl/Resolution/Scalar/Enum/GraphQLEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import GraphQL
import NIO
import ContextKit

public protocol GraphQLEnum: OutputResolvable, InputResolvable, ConcreteResolvable {
public protocol GraphQLEnum: OutputResolvable, InputResolvable, ConcreteResolvable, ValueResolvable {
static func cases(using context: inout Resolution.Context) throws -> [String : Map]
}

extension GraphQLEnum where Self: CaseIterable & RawRepresentable, RawValue == String {

public static func cases(using context: inout Resolution.Context) throws -> [String : Map] {
let keysAndValues = try allCases.map { ($0.rawValue, try $0.map()) }
return Dictionary(uniqueKeysWithValues: keysAndValues).compactMapValues { $0 }
return Dictionary(uniqueKeysWithValues: keysAndValues)
}

}
Expand Down
21 changes: 7 additions & 14 deletions Sources/GraphZahl/Resolution/Scalar/Enum/KeyPath+GraphQLEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,23 @@ extension KeyPath: ConcreteResolvable where Root: ConcreteResolvable & KeyPathLi

}

extension KeyPath: ValueResolvable where Root: ConcreteResolvable & KeyPathListable {
public func map() throws -> Map? {
return nil
}
}

extension KeyPath: InputResolvable where Root: ConcreteResolvable & KeyPathListable {

public static func resolve(using context: inout Resolution.Context) throws -> GraphQLInputType {
return GraphQLNonNull(try GraphQLEnumType(name: concreteTypeName, values: try cases(using: &context)))
}

public static func create(from map: Map) throws -> Self {
let name = try map.stringValue()
return (\Root.[checkedMirrorDescendant: name] as WritableKeyPath<Root, Value>) as! Self
}

}

extension KeyPath: OutputResolvable where Root: ConcreteResolvable & KeyPathListable { }

extension KeyPath: GraphQLEnum where Root: ConcreteResolvable & KeyPathListable {
public static func cases(using context: inout Resolution.Context) throws -> [String : Map] {
private static func cases(using context: inout Resolution.Context) throws -> [String : GraphQLEnumValue] {
let keyPaths: [String : KeyPath<Root, Value>] = try Root.resolve()

let cases = try keyPaths
.map { ($0.key, try $0.key.map()) }
.map { ($0.key.upperCamelized, try $0.key.map()) }

return Dictionary(cases) { $1 }.compactMapValues { $0 }
return Dictionary(cases) { $1 }.mapValues { GraphQLEnumValue(value: $0) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Foundation
import GraphQL

public protocol GraphQLScalar: OutputResolvable, InputResolvable, ConcreteResolvable, KeyPathListable {
public protocol GraphQLScalar: OutputResolvable, InputResolvable, ConcreteResolvable, ValueResolvable, KeyPathListable {
static func resolve() throws -> GraphQLScalarType

init(scalar: ScalarValue) throws
Expand All @@ -15,7 +15,7 @@ extension GraphQLScalar {
return try GraphQLScalarType(name: concreteTypeName) { $0 as! Map }
}

public func map() throws -> Map? {
public func map() throws -> Map {
return try encodeScalar().graphql()
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/GraphZahl/Utils/MethodInfo+resolve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ extension MethodInfo {

let type = try context.resolve(type: argumentType)

guard let defaultValue = try argument.defaultValue() as? ValueResolvable else {
guard let defaultValue = try argument.defaultValue() else {
return GraphQLArgument(type: type, defaultValue: nil)
}

guard let map = try defaultValue.map() else {
guard let valueResolvable = defaultValue as? ValueResolvable else {
switch type {
case let type as GraphQLNonNull:
return GraphQLArgument(type: type.ofType as! GraphQLInputType, defaultValue: nil)
Expand All @@ -32,7 +32,7 @@ extension MethodInfo {
}
}

return GraphQLArgument(type: type, defaultValue: map)
return GraphQLArgument(type: type, defaultValue: try valueResolvable.map())
}

guard arguments.count == relevantArguments.count else { return nil }
Expand Down
28 changes: 28 additions & 0 deletions Sources/GraphZahlTests/SchemaResolutionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ class SchemaResolutionTests: XCTestCase {
XCTAssertEqual(expectedData, result.data)
}

func testKeypaths() throws {
let query = """
{
foo(filter: Foo, equals: "Foo1") {
foo
}
}
"""

let result = try Schema.perform(request: query).wait()

let expectedData: Map = [
"foo" : [
["foo": "Foo1"]
]
]

XCTAssertEqual(expectedData, result.data)
}

}

class Schema: GraphZahl.GraphQLSchema {
Expand All @@ -47,6 +67,14 @@ class Schema: GraphZahl.GraphQLSchema {
return .bar(Bar(bar: 42))
}

func foo(filter: KeyPath<Foo, String>, equals: String) -> [Foo] {
return [
Foo(foo: "Foo"),
Foo(foo: "Foo1"),
Foo(foo: "Foo2"),
].filter { $0[keyPath: filter] == equals }
}

required init(viewerContext: ()) { }
}

Expand Down

0 comments on commit e9b2189

Please sign in to comment.