Skip to content

Commit 1b1f8d3

Browse files
committed
improve introspection
1 parent 57a7f30 commit 1b1f8d3

File tree

9 files changed

+387
-232
lines changed

9 files changed

+387
-232
lines changed

Sources/GraphQL/Error/GraphQLError.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ extension GraphQLError : CustomStringConvertible {
9090
}
9191
}
9292

93-
extension GraphQLError : Equatable {
93+
extension GraphQLError : Hashable {
9494
public var hashValue: Int {
9595
return message.hashValue
9696
}
97-
}
9897

99-
public func == (lhs: GraphQLError, rhs: GraphQLError) -> Bool {
100-
return lhs.hashValue == rhs.hashValue
98+
public static func == (lhs: GraphQLError, rhs: GraphQLError) -> Bool {
99+
return lhs.hashValue == rhs.hashValue
100+
}
101101
}
102102

103103
extension GraphQLError : MapRepresentable {

Sources/GraphQL/Execution/Execute.swift

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,7 @@ func executeFieldsSerially(
264264
path: fieldPath
265265
)
266266

267-
guard let r = result else {
268-
return results
269-
}
270-
271-
results[field.key] = r
272-
267+
results[field.key] = result
273268
return results
274269
}
275270
}
@@ -285,29 +280,13 @@ func executeFields(
285280
path: [IndexPathElement],
286281
fields: [String: [Field]]
287282
) throws -> Map {
288-
let finalResults: [String: Map] = try fields.reduce([:]) { results, field in
289-
var results = results
290-
let fieldASTs = field.value
291-
let fieldPath = path + [field.key] as [IndexPathElement]
292-
293-
let result = try resolveField(
294-
exeContext: exeContext,
295-
parentType: parentType,
296-
source: sourceValue,
297-
fieldASTs: fieldASTs,
298-
path: fieldPath
299-
)
300-
301-
guard let r = result else {
302-
return results
303-
}
304-
305-
results[field.key] = r
306-
307-
return results
308-
}
309-
310-
return .dictionary(finalResults)
283+
return try executeFieldsSerially(
284+
exeContext: exeContext,
285+
parentType: parentType,
286+
sourceValue: sourceValue,
287+
path: path,
288+
fields: fields
289+
)
311290
}
312291

313292
/**
@@ -491,13 +470,15 @@ func resolveField(
491470
source: Map,
492471
fieldASTs: [Field],
493472
path: [IndexPathElement]
494-
) throws -> Map? {
473+
) throws -> Map {
495474
let fieldAST = fieldASTs[0]
496475
let fieldName = fieldAST.name.value
497476

498-
guard let fieldDef = getFieldDef(schema: exeContext.schema, parentType: parentType, fieldName: fieldName) else {
499-
return nil // TODO: this used to be "undefined"
500-
}
477+
let fieldDef = getFieldDef(
478+
schema: exeContext.schema,
479+
parentType: parentType,
480+
fieldName: fieldName
481+
)
501482

502483
let returnType = fieldDef.type
503484
let resolve = fieldDef.resolve ?? defaultResolve
@@ -612,7 +593,7 @@ func completeValueCatchingError(
612593
// If `completeValueWithLocatedError` returned abruptly (threw an error),
613594
// log the error and return null.
614595
exeContext.errors.append(error)
615-
return .null // TODO: this was nil before
596+
return .null
616597
} catch {
617598
fatalError()
618599
}
@@ -948,19 +929,7 @@ func defaultResolveType(
948929
* of calling that func while passing along args and context.
949930
*/
950931
func defaultResolve(source: Map, args: [String: Map], context: Map, info: GraphQLResolveInfo) -> Map {
951-
// ensure source is a value for which property access is acceptable.
952-
if case .dictionary(let source) = source {
953-
let property = source[info.fieldName]
954-
955-
// TODO: Dynamic Shit
956-
// if (typeof property === 'func') {
957-
// return source[info.fieldName](args, context)
958-
// }
959-
960-
return property!
961-
}
962-
963-
return .null
932+
return source[info.fieldName]
964933
}
965934

966935
/**
@@ -976,7 +945,7 @@ func getFieldDef(
976945
schema: GraphQLSchema,
977946
parentType: GraphQLObjectType,
978947
fieldName: String
979-
) -> GraphQLFieldDefinition? {
948+
) -> GraphQLFieldDefinition {
980949
if fieldName == SchemaMetaFieldDef.name && schema.queryType.name == parentType.name {
981950
return SchemaMetaFieldDef
982951
} else if fieldName == TypeMetaFieldDef.name && schema.queryType.name == parentType.name {
@@ -985,5 +954,6 @@ func getFieldDef(
985954
return TypeNameMetaFieldDef
986955
}
987956

988-
return parentType.fields[fieldName]
957+
// we know this field exists because we passed validation before execution
958+
return parentType.fields[fieldName]!
989959
}

Sources/GraphQL/Language/AST.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,6 @@ final class OperationDefinition {
314314
self.selectionSet = selectionSet
315315
}
316316

317-
var key: String {
318-
return "operation"
319-
}
320-
321317
func get(key: String) -> NodeResult? {
322318
switch key {
323319
case "name":
@@ -563,7 +559,7 @@ final class Argument {
563559
self.value = value
564560
}
565561

566-
public func get(key: String) -> NodeResult? {
562+
func get(key: String) -> NodeResult? {
567563
switch key {
568564
case "name":
569565
return .node(name)
@@ -661,7 +657,7 @@ extension InlineFragment {
661657
return nil
662658
}
663659
return .array(directives)
664-
case "directives":
660+
case "selectionSet":
665661
return .node(selectionSet)
666662
default:
667663
return nil
@@ -1028,7 +1024,7 @@ final class NonNullType {
10281024
self.type = type
10291025
}
10301026

1031-
public func get(key: String) -> NodeResult? {
1027+
func get(key: String) -> NodeResult? {
10321028
switch key {
10331029
case "type":
10341030
return .node(type)

Sources/GraphQL/Swift Utilities/Invariant.swift

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)