Skip to content

Commit f14da96

Browse files
committed
Improve API.
1 parent 0e36422 commit f14da96

File tree

4 files changed

+59
-59
lines changed

4 files changed

+59
-59
lines changed

Sources/Graphiti/Field/Field/Field.swift

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import GraphQL
22
import Runtime
33

4-
public class Field<ObjectType, Context, Arguments : Decodable, FieldType, ResolveType> : FieldComponent<ObjectType, Context> {
4+
public class Field<ObjectType, Context, FieldType, Arguments : Decodable> : FieldComponent<ObjectType, Context> {
55
let name: String
66
let arguments: [ArgumentComponent<Arguments>]
77
let resolve: GraphQLFieldResolve
@@ -92,104 +92,104 @@ public class Field<ObjectType, Context, Arguments : Decodable, FieldType, Resolv
9292
}
9393
}
9494

95-
// MARK: Keypath Initializers
95+
// MARK: AsyncResolve Initializers
9696

97-
public extension Field where FieldType == ResolveType, Arguments == NoArguments {
97+
public extension Field where FieldType : Encodable {
9898
convenience init(
9999
_ name: String,
100-
at keyPath: KeyPath<ObjectType, ResolveType>
100+
at function: @escaping AsyncResolve<ObjectType, Context, Arguments, FieldType>,
101+
_ arguments: ArgumentComponent<Arguments>...
101102
) {
102-
let syncResolve: SyncResolve<ObjectType, Context, NoArguments, ResolveType> = { type in
103-
{ context, _ in
104-
type[keyPath: keyPath]
105-
}
106-
}
107-
108-
self.init(name: name, arguments: [], syncResolve: syncResolve)
103+
self.init(name: name, arguments: arguments, asyncResolve: function)
109104
}
110105
}
111106

112-
public extension Field where Arguments == NoArguments {
113-
convenience init(
107+
public extension Field {
108+
convenience init<ResolveType>(
114109
_ name: String,
115-
at keyPath: KeyPath<ObjectType, ResolveType>,
116-
overridingType: FieldType.Type = FieldType.self
110+
at function: @escaping AsyncResolve<ObjectType, Context, Arguments, ResolveType>,
111+
as: FieldType.Type,
112+
_ arguments: ArgumentComponent<Arguments>...
117113
) {
118-
let syncResolve: SyncResolve<ObjectType, Context, NoArguments, ResolveType> = { type in
119-
return { context, _ in
120-
return type[keyPath: keyPath]
121-
}
122-
}
123-
124-
self.init(name: name, arguments: [], syncResolve: syncResolve)
114+
self.init(name: name, arguments: arguments, asyncResolve: function)
125115
}
126116
}
127117

128-
// MARK: SyncResolve Initializers
118+
// MARK: SimpleAsyncResolve Initializers
129119

130-
public extension Field where FieldType == ResolveType {
120+
public extension Field where FieldType : Encodable {
131121
convenience init(
132122
_ name: String,
133-
at function: @escaping SyncResolve<ObjectType, Context, Arguments, ResolveType>,
123+
at function: @escaping SimpleAsyncResolve<ObjectType, Context, Arguments, FieldType>,
134124
_ arguments: ArgumentComponent<Arguments>...
135125
) {
136-
self.init(name: name, arguments: arguments, syncResolve: function)
126+
self.init(name: name, arguments: arguments, simpleAsyncResolve: function)
137127
}
138128
}
139129

140130
public extension Field {
141-
convenience init(
131+
convenience init<ResolveType>(
142132
_ name: String,
143-
at function: @escaping SyncResolve<ObjectType, Context, Arguments, ResolveType>,
144-
overridingType: FieldType.Type = FieldType.self,
133+
at function: @escaping SimpleAsyncResolve<ObjectType, Context, Arguments, ResolveType>,
134+
as: FieldType.Type,
145135
_ arguments: ArgumentComponent<Arguments>...
146136
) {
147-
self.init(name: name, arguments: arguments, syncResolve: function)
137+
self.init(name: name, arguments: arguments, simpleAsyncResolve: function)
148138
}
149139
}
150140

151-
// MARK: AsyncResolve Initializers
141+
// MARK: SyncResolve Initializers
152142

153-
public extension Field where FieldType == ResolveType {
143+
public extension Field where FieldType : Encodable {
154144
convenience init(
155145
_ name: String,
156-
at function: @escaping AsyncResolve<ObjectType, Context, Arguments, ResolveType>,
146+
at function: @escaping SyncResolve<ObjectType, Context, Arguments, FieldType>,
157147
_ arguments: ArgumentComponent<Arguments>...
158148
) {
159-
self.init(name: name, arguments: arguments, asyncResolve: function)
149+
self.init(name: name, arguments: arguments, syncResolve: function)
160150
}
161151
}
162152

163153
public extension Field {
164-
convenience init(
154+
convenience init<ResolveType>(
165155
_ name: String,
166-
at function: @escaping AsyncResolve<ObjectType, Context, Arguments, ResolveType>,
167-
overridingType: FieldType.Type = FieldType.self,
156+
at function: @escaping SyncResolve<ObjectType, Context, Arguments, ResolveType>,
157+
as: FieldType.Type,
168158
_ arguments: ArgumentComponent<Arguments>...
169159
) {
170-
self.init(name: name, arguments: arguments, asyncResolve: function)
160+
self.init(name: name, arguments: arguments, syncResolve: function)
171161
}
172162
}
173163

174-
// MARK: SimpleAsyncResolve Initializers
164+
// MARK: Keypath Initializers
175165

176-
public extension Field where FieldType == ResolveType {
166+
public extension Field where Arguments == NoArguments {
177167
convenience init(
178168
_ name: String,
179-
at function: @escaping SimpleAsyncResolve<ObjectType, Context, Arguments, ResolveType>,
180-
_ arguments: ArgumentComponent<Arguments>...
169+
at keyPath: KeyPath<ObjectType, FieldType>
181170
) {
182-
self.init(name: name, arguments: arguments, simpleAsyncResolve: function)
171+
let syncResolve: SyncResolve<ObjectType, Context, NoArguments, FieldType> = { type in
172+
{ context, _ in
173+
type[keyPath: keyPath]
174+
}
175+
}
176+
177+
self.init(name: name, arguments: [], syncResolve: syncResolve)
183178
}
184179
}
185180

186-
public extension Field {
187-
convenience init(
181+
public extension Field where Arguments == NoArguments {
182+
convenience init<ResolveType>(
188183
_ name: String,
189-
at function: @escaping SimpleAsyncResolve<ObjectType, Context, Arguments, ResolveType>,
190-
overridingType: FieldType.Type = FieldType.self,
191-
_ arguments: ArgumentComponent<Arguments>...
184+
at keyPath: KeyPath<ObjectType, ResolveType>,
185+
as: FieldType.Type
192186
) {
193-
self.init(name: name, arguments: arguments, simpleAsyncResolve: function)
187+
let syncResolve: SyncResolve<ObjectType, Context, NoArguments, ResolveType> = { type in
188+
return { context, _ in
189+
return type[keyPath: keyPath]
190+
}
191+
}
192+
193+
self.init(name: name, arguments: [], syncResolve: syncResolve)
194194
}
195195
}

Tests/GraphitiTests/StarWarsAPI/API.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public struct StarWarsAPI : API {
2626
Field("name", at: \.name)
2727
.description("The name of the character."),
2828

29-
Field("friends", at: \.friends, overridingType: [TypeReference<Character>].self)
29+
Field("friends", at: \.friends, as: [TypeReference<Character>].self)
3030
.description("The friends of the character, or an empty list if they have none."),
3131

3232
Field("appearsIn", at: \.appearsIn)
@@ -43,7 +43,7 @@ public struct StarWarsAPI : API {
4343
Field("diameter", at: \.diameter),
4444
Field("rotationPeriod", at: \.rotationPeriod),
4545
Field("orbitalPeriod", at: \.orbitalPeriod),
46-
Field("residents", at: \.residents, overridingType: [TypeReference<Human>].self)
46+
Field("residents", at: \.residents, as: [TypeReference<Human>].self)
4747
)
4848
.description("A large mass, planet or planetoid in the Star Wars Universe, at the time of 0 ABY."),
4949

@@ -53,7 +53,7 @@ public struct StarWarsAPI : API {
5353
Field("appearsIn", at: \.appearsIn),
5454
Field("homePlanet", at: \.homePlanet),
5555

56-
Field("friends", at: Human.getFriends)
56+
Field("friends", at: Human.getFriends, as: [Character].self)
5757
.description("The friends of the human, or an empty list if they have none."),
5858

5959
Field("secretBackstory", at: Human.getSecretBackstory)
@@ -67,7 +67,7 @@ public struct StarWarsAPI : API {
6767
Field("appearsIn", at: \.appearsIn),
6868
Field("primaryFunction", at: \.primaryFunction),
6969

70-
Field("friends", at: Droid.getFriends)
70+
Field("friends", at: Droid.getFriends, as: [Character].self)
7171
.description("The friends of the droid, or an empty list if they have none."),
7272

7373
Field("secretBackstory", at: Droid.getSecretBackstory)
@@ -78,7 +78,7 @@ public struct StarWarsAPI : API {
7878
Union(SearchResult.self, members: Planet.self, Human.self, Droid.self),
7979

8080
Query(
81-
Field("hero", at: Root.hero,
81+
Field("hero", at: Root.hero, as: Character.self,
8282
Argument("episode", at: \.episode)
8383
.description("If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.")
8484
)
@@ -95,7 +95,7 @@ public struct StarWarsAPI : API {
9595
.description("Id of the droid.")
9696
),
9797

98-
Field("search", at: Root.search,
98+
Field("search", at: Root.search, as: [SearchResult].self,
9999
Argument("query", at: \.query)
100100
.defaultValue("R2-D2")
101101
)

Tests/GraphitiTests/StarWarsAPI/Entities.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ public enum Episode : String, Codable, CaseIterable {
44
case jedi = "JEDI"
55
}
66

7-
public protocol Character {
7+
public protocol Character : Codable {
88
var id: String { get }
99
var name: String { get }
1010
var friends: [String] { get }
1111
var appearsIn: [Episode] { get }
1212
}
1313

14-
public protocol SearchResult {}
14+
public protocol SearchResult : Codable {}
1515

1616
public struct Planet : SearchResult, Codable {
1717
public let id: String

Tests/GraphitiTests/StarWarsTests/StarWarsQueryTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,8 @@ class StarWarsQueryTests : XCTestCase {
656656

657657
let schema = try! Schema<Root, NoContext>(
658658
Type(A.self,
659-
Field("nullableA", at: A.nullableA, overridingType: (TypeReference<A>?).self),
660-
Field("nonNullA", at: A.nonNullA, overridingType: TypeReference<A>.self),
659+
Field("nullableA", at: A.nullableA, as: (TypeReference<A>?).self),
660+
Field("nonNullA", at: A.nonNullA, as: TypeReference<A>.self),
661661
Field("throws", at: A.throws)
662662
),
663663

0 commit comments

Comments
 (0)