Skip to content

Commit f12c6ba

Browse files
committed
WIP
1 parent 75fb18f commit f12c6ba

File tree

7 files changed

+71
-65
lines changed

7 files changed

+71
-65
lines changed

Sources/GraphQL/Language/AST.swift

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public protocol Node: TextOutputStreamable {
142142
}
143143

144144
extension Node {
145-
var printed: String {
145+
public var printed: String {
146146
var s = ""
147147
self.write(to: &s)
148148
return s
@@ -248,17 +248,34 @@ public struct Document {
248248

249249
extension Document : Equatable {
250250
public static func == (lhs: Document, rhs: Document) -> Bool {
251-
guard lhs.definitions.count == rhs.definitions.count else {
252-
return false
253-
}
251+
return lhs.definitions == rhs.definitions
252+
}
253+
}
254254

255-
for (l, r) in zip(lhs.definitions, rhs.definitions) {
256-
guard l == r else {
257-
return false
258-
}
255+
public struct ExecutableDocument {
256+
public let loc: Location?
257+
public var definitions: [ExecutableDefinition]
258+
259+
init(loc: Location? = nil, definitions: [ExecutableDefinition]) {
260+
self.loc = loc
261+
self.definitions = definitions
262+
}
263+
264+
public mutating func descend(descender: inout Descender) {
265+
descender.descend(&self, \.definitions)
266+
}
267+
268+
public func write<Target>(to target: inout Target) where Target : TextOutputStream {
269+
definitions.forEach {
270+
$0.write(to: &target)
271+
target.write("\n\n")
259272
}
273+
}
274+
}
260275

261-
return true
276+
extension ExecutableDocument: Equatable {
277+
public static func == (lhs: ExecutableDocument, rhs: ExecutableDocument) -> Bool {
278+
return lhs.definitions == rhs.definitions
262279
}
263280
}
264281

Sources/GraphQL/Language/Visitor.swift

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
public struct Descender {
22

3-
enum Mutation<T: Node> {
4-
case replace(T)
5-
case remove
6-
}
73
private let visitor: Visitor
84

95
fileprivate var parentStack: [VisitorParent] = []
106
private var path: [AnyKeyPath] = []
117
private var isBreaking = false
128

13-
fileprivate mutating func go<H: Node>(node: inout H, key: AnyKeyPath?) -> Mutation<H>? {
14-
if isBreaking { return nil }
9+
/**
10+
The meat of the traversal
11+
12+
- Returns
13+
`true` if node should be removed
14+
*/
15+
fileprivate mutating func go<H: Node>(node: inout H, key: AnyKeyPath?) -> Bool {
16+
if isBreaking { return false }
1517
let parent = parentStack.last
1618
let newPath: [AnyKeyPath]
1719
if let key = key {
@@ -20,23 +22,23 @@ public struct Descender {
2022
newPath = path
2123
}
2224

23-
var mutation: Mutation<H>? = nil
25+
var shouldRemove = false
2426

2527
switch visitor.enter(node: node, key: key, parent: parent, path: newPath, ancestors: parentStack) {
2628
case .skip:
27-
return nil// .node(Optional(result))
29+
return false
2830
case .continue:
2931
break
3032
case let .node(newNode):
3133
if let newNode = newNode {
32-
mutation = .replace(newNode)
34+
node = newNode
3335
} else {
3436
// TODO: Should we still be traversing the children here?
35-
mutation = .remove
37+
shouldRemove = true
3638
}
3739
case .break:
3840
isBreaking = true
39-
return nil
41+
return false
4042
}
4143
parentStack.append(.node(node))
4244
if let key = key {
@@ -48,46 +50,39 @@ public struct Descender {
4850
}
4951
parentStack.removeLast()
5052

51-
if isBreaking { return mutation }
53+
if isBreaking { return shouldRemove }
5254

5355
switch visitor.leave(node: node, key: key, parent: parent, path: newPath, ancestors: parentStack) {
5456
case .skip, .continue:
55-
return mutation
57+
return shouldRemove
5658
case let .node(newNode):
5759
if let newNode = newNode {
58-
return .replace(newNode)
60+
node = newNode
61+
return false
5962
} else {
6063
// TODO: Should we still be traversing the children here?
61-
return .remove
64+
return true
6265
}
6366
case .break:
6467
isBreaking = true
65-
return mutation
68+
return shouldRemove
6669
}
6770
}
6871

6972

7073
mutating func descend<T: Node, U: Node>(_ node: inout T, _ kp: WritableKeyPath<T, U>) {
71-
switch go(node: &node[keyPath: kp], key: kp) {
72-
case nil:
73-
break
74-
case .replace(let child):
75-
node[keyPath: kp] = child
76-
case .remove:
74+
if go(node: &node[keyPath: kp], key: kp) {
7775
fatalError("Can't remove this node")
7876
}
7977
}
8078
mutating func descend<T, U: Node>(_ node: inout T, _ kp: WritableKeyPath<T, U?>) {
8179
guard var oldVal = node[keyPath: kp] else {
8280
return
8381
}
84-
switch go(node: &oldVal, key: kp) {
85-
case nil:
86-
node[keyPath: kp] = oldVal
87-
case .replace(let child):
88-
node[keyPath: kp] = child
89-
case .remove:
82+
if go(node: &oldVal, key: kp) {
9083
node[keyPath: kp] = nil
84+
} else {
85+
node[keyPath: kp] = oldVal
9186
}
9287
}
9388
mutating func descend<T, U: Node>(_ node: inout T, _ kp: WritableKeyPath<T, [U]>) {
@@ -97,12 +92,7 @@ public struct Descender {
9792

9893
var i = node[keyPath: kp].startIndex
9994
while i != node[keyPath: kp].endIndex {
100-
switch go(node: &node[keyPath: kp][i], key: \[U].[i]) {
101-
case nil:
102-
break
103-
case .replace(let child):
104-
node[keyPath: kp][i] = child
105-
case .remove:
95+
if go(node: &node[keyPath: kp][i], key: \[U].[i]) {
10696
toRemove.append(i)
10797
}
10898
i = node[keyPath: kp].index(after: i)
@@ -112,12 +102,7 @@ public struct Descender {
112102
}
113103

114104
mutating func descend<T: Node>(enumCase: inout T) {
115-
switch go(node: &enumCase, key: nil) {
116-
case nil:
117-
break
118-
case .replace(let node):
119-
enumCase = node
120-
case .remove:
105+
if go(node: &enumCase, key: nil) {
121106
//TODO: figure this out
122107
fatalError("What happens here?")
123108
}
@@ -169,14 +154,10 @@ public func visit<T: Node, V: Visitor>(root: T, visitor: V) -> T {
169154
var descender = Descender(visitor: visitor)
170155

171156
var result = root
172-
switch descender.go(node: &result, key: nil) {
173-
case .remove:
157+
if descender.go(node: &result, key: nil) {
174158
fatalError("Root node in the AST was removed")
175-
case .replace(let node):
176-
return node
177-
case nil:
178-
return result
179159
}
160+
return result
180161
}
181162

182163
public enum VisitorParent {

Sources/GraphQL/Type/Definition.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -915,11 +915,12 @@ public final class GraphQLUnionType {
915915
public let resolveType: GraphQLTypeResolve?
916916
private let typesThunk: () -> [GraphQLObjectType]
917917
public lazy var types = {
918-
try! defineTypes(
919-
name: name,
920-
hasResolve: resolveType != nil,
921-
types: typesThunk()
922-
)
918+
typesThunk()
919+
// try! defineTypes(
920+
// name: name,
921+
// hasResolve: resolveType != nil,
922+
// types: typesThunk()
923+
// )
923924
}()
924925
public let possibleTypeNames: [String: Bool]
925926
public let kind: TypeKind = .union

Sources/GraphQL/Utilities/BuildClientSchema.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,16 @@ public func buildClientSchema(introspection: IntrospectionQuery) throws -> Graph
152152

153153
// let directives = []
154154

155+
let mutationType: GraphQLObjectType?
156+
if let mutationTypeRef = schemaIntrospection.mutationType {
157+
mutationType = try getObjectType(name: mutationTypeRef.name)
158+
} else {
159+
mutationType = nil
160+
}
161+
155162
return try GraphQLSchema(
156163
query: try getObjectType(name: schemaIntrospection.queryType.name),
157-
mutation: nil,
164+
mutation: mutationType,
158165
subscription: nil,
159166
types: Array(typeMap.values),
160167
directives: []

Sources/GraphQL/Utilities/GetIntrospectionQuery.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func getIntrospectionQuery(
1+
public func getIntrospectionQuery(
22
descriptions: Bool = true,
33
specifiedByUrl: Bool = false,
44
directiveIsRepeatable: Bool = false,

Sources/GraphQL/Utilities/TypeComparators.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Provided two types, return true if the types are equal (invariant).
33
*/
4-
func isEqualType(_ typeA: GraphQLType, _ typeB: GraphQLType) -> Bool {
4+
public func isEqualType(_ typeA: GraphQLType, _ typeB: GraphQLType) -> Bool {
55
// Equivalent types are equal.
66
if typeA == typeB {
77
return true
@@ -70,7 +70,7 @@ func == (lhs: GraphQLType, rhs: GraphQLType) -> Bool {
7070
* Provided a type and a super type, return true if the first type is either
7171
* equal or a subset of the second super type (covariant).
7272
*/
73-
func isTypeSubTypeOf(
73+
public func isTypeSubTypeOf(
7474
_ schema: GraphQLSchema,
7575
_ maybeSubType: GraphQLType,
7676
_ superType: GraphQLType

Sources/GraphQL/Utilities/TypeFromAST.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func typeFromAST(schema: GraphQLSchema, inputTypeAST: Type) -> GraphQLType? {
1+
public func typeFromAST(schema: GraphQLSchema, inputTypeAST: Type) -> GraphQLType? {
22
switch inputTypeAST {
33
case let .listType(listType):
44
if let innerType = typeFromAST(schema: schema, inputTypeAST: listType.type) {

0 commit comments

Comments
 (0)