Skip to content

Commit 3933850

Browse files
committed
Make GraphQLType Equatable
To do so, need to make it conform to AnyObject, which in turn forces us to use Swift 5.6 any syntax for existential types throughout
1 parent 0765b9f commit 3933850

16 files changed

+132
-126
lines changed

Sources/GraphQL/Execution/Execute.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ func doesFragmentConditionMatch(
633633
return true
634634
}
635635

636-
if let abstractType = conditionalType as? GraphQLAbstractType {
636+
if let abstractType = conditionalType as? (any GraphQLAbstractType) {
637637
return exeContext.schema.isSubType(
638638
abstractType: abstractType,
639639
maybeSubType: type
@@ -761,7 +761,7 @@ func resolveOrError(
761761
// in the execution context.
762762
func completeValueCatchingError(
763763
exeContext: ExecutionContext,
764-
returnType: GraphQLType,
764+
returnType: any GraphQLType,
765765
fieldASTs: [Field],
766766
info: GraphQLResolveInfo,
767767
path: IndexPath,
@@ -813,7 +813,7 @@ func completeValueCatchingError(
813813
// location information.
814814
func completeValueWithLocatedError(
815815
exeContext: ExecutionContext,
816-
returnType: GraphQLType,
816+
returnType: any GraphQLType,
817817
fieldASTs: [Field],
818818
info: GraphQLResolveInfo,
819819
path: IndexPath,
@@ -863,7 +863,7 @@ func completeValueWithLocatedError(
863863
*/
864864
func completeValue(
865865
exeContext: ExecutionContext,
866-
returnType: GraphQLType,
866+
returnType: any GraphQLType,
867867
fieldASTs: [Field],
868868
info: GraphQLResolveInfo,
869869
path: IndexPath,
@@ -995,7 +995,7 @@ func completeListValue(
995995
* Complete a Scalar or Enum by serializing to a valid value, returning
996996
* .null if serialization is not possible.
997997
*/
998-
func completeLeafValue(returnType: GraphQLLeafType, result: Any?) throws -> Map {
998+
func completeLeafValue(returnType: any GraphQLLeafType, result: Any?) throws -> Map {
999999
guard let result = result else {
10001000
return .null
10011001
}
@@ -1019,7 +1019,7 @@ func completeLeafValue(returnType: GraphQLLeafType, result: Any?) throws -> Map
10191019
*/
10201020
func completeAbstractValue(
10211021
exeContext: ExecutionContext,
1022-
returnType: GraphQLAbstractType,
1022+
returnType: any GraphQLAbstractType,
10231023
fieldASTs: [Field],
10241024
info: GraphQLResolveInfo,
10251025
path: IndexPath,
@@ -1042,7 +1042,7 @@ func completeAbstractValue(
10421042
}
10431043

10441044
// If resolveType returns a string, we assume it's a GraphQLObjectType name.
1045-
var runtimeType: GraphQLType?
1045+
var runtimeType: (any GraphQLType)?
10461046

10471047
switch resolveResult {
10481048
case .name(let name):
@@ -1139,7 +1139,7 @@ func defaultResolveType(
11391139
value: Any,
11401140
eventLoopGroup: EventLoopGroup,
11411141
info: GraphQLResolveInfo,
1142-
abstractType: GraphQLAbstractType
1142+
abstractType: any GraphQLAbstractType
11431143
) throws -> TypeResolveResult? {
11441144
let possibleTypes = info.schema.getPossibleTypes(abstractType: abstractType)
11451145

Sources/GraphQL/Execution/Values.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func getVariableValue(schema: GraphQLSchema, definitionAST: VariableDefinition,
8282
let type = typeFromAST(schema: schema, inputTypeAST: definitionAST.type)
8383
let variable = definitionAST.variable
8484

85-
guard let inputType = type as? GraphQLInputType else {
85+
guard let inputType = type as? (any GraphQLInputType) else {
8686
throw GraphQLError(
8787
message:
8888
"Variable \"$\(variable.name.value)\" expected value of type " +
@@ -112,11 +112,11 @@ func getVariableValue(schema: GraphQLSchema, definitionAST: VariableDefinition,
112112
/**
113113
* Given a type and any value, return a runtime value coerced to match the type.
114114
*/
115-
func coerceValue(value: Map, type: GraphQLInputType) throws -> Map {
115+
func coerceValue(value: Map, type: any GraphQLInputType) throws -> Map {
116116
if let nonNull = type as? GraphQLNonNull {
117117
// Note: we're not checking that the result of coerceValue is non-null.
118118
// We only call this function after calling validate.
119-
guard let nonNullType = nonNull.ofType as? GraphQLInputType else {
119+
guard let nonNullType = nonNull.ofType as? (any GraphQLInputType) else {
120120
throw GraphQLError(message: "NonNull must wrap an input type")
121121
}
122122
return try coerceValue(value: value, type: nonNullType)
@@ -127,7 +127,7 @@ func coerceValue(value: Map, type: GraphQLInputType) throws -> Map {
127127
}
128128

129129
if let list = type as? GraphQLList {
130-
guard let itemType = list.ofType as? GraphQLInputType else {
130+
guard let itemType = list.ofType as? (any GraphQLInputType) else {
131131
throw GraphQLError(message: "Input list must wrap an input type")
132132
}
133133

@@ -168,7 +168,7 @@ func coerceValue(value: Map, type: GraphQLInputType) throws -> Map {
168168
return .dictionary(object)
169169
}
170170

171-
if let leafType = type as? GraphQLLeafType {
171+
if let leafType = type as? (any GraphQLLeafType) {
172172
return try leafType.parseValue(value: value)
173173
}
174174

Sources/GraphQL/Type/Definition.swift

+41-35
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import NIO
44
/**
55
* These are all of the possible kinds of types.
66
*/
7-
public protocol GraphQLType : CustomDebugStringConvertible, Encodable, KeySubscriptable {}
7+
public protocol GraphQLType : CustomDebugStringConvertible, Encodable, KeySubscriptable, AnyObject, Equatable {}
88
extension GraphQLScalarType : GraphQLType {}
99
extension GraphQLObjectType : GraphQLType {}
1010
extension GraphQLInterfaceType : GraphQLType {}
@@ -14,6 +14,12 @@ extension GraphQLInputObjectType : GraphQLType {}
1414
extension GraphQLList : GraphQLType {}
1515
extension GraphQLNonNull : GraphQLType {}
1616

17+
extension GraphQLType {
18+
public static func == (lhs: Self, rhs: Self) -> Bool {
19+
ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
20+
}
21+
}
22+
1723
/**
1824
* These types may be used as input types for arguments and directives.
1925
*/
@@ -27,9 +33,9 @@ extension GraphQLNonNull : GraphQLInputType {}
2733
//extension GraphQLList : GraphQLInputType where Element : GraphQLInputType {}
2834
//extension GraphQLNonNull : GraphQLInputType where Element : (GraphQLScalarType | GraphQLEnumType | GraphQLInputObjectType | GraphQLList<GraphQLInputType>) {}
2935

30-
func isInputType(type: GraphQLType?) -> Bool {
36+
func isInputType(type: (any GraphQLType)?) -> Bool {
3137
let namedType = getNamedType(type: type)
32-
return namedType is GraphQLInputType
38+
return namedType is any GraphQLInputType
3339
}
3440

3541
/**
@@ -59,7 +65,7 @@ public protocol GraphQLLeafType : GraphQLNamedType {
5965
extension GraphQLScalarType : GraphQLLeafType {}
6066
extension GraphQLEnumType : GraphQLLeafType {}
6167

62-
func isLeafType(type: GraphQLType?) -> Bool {
68+
func isLeafType(type: (any GraphQLType)?) -> Bool {
6369
let namedType = getNamedType(type: type)
6470
return namedType is GraphQLScalarType ||
6571
namedType is GraphQLEnumType
@@ -103,12 +109,12 @@ extension GraphQLEnumType : GraphQLNullableType {}
103109
extension GraphQLInputObjectType : GraphQLNullableType {}
104110
extension GraphQLList : GraphQLNullableType {}
105111

106-
func getNullableType(type: GraphQLType?) -> GraphQLNullableType? {
112+
func getNullableType(type: (any GraphQLType)?) -> (any GraphQLNullableType)? {
107113
if let type = type as? GraphQLNonNull {
108114
return type.ofType
109115
}
110116

111-
return type as? GraphQLNullableType
117+
return type as? any GraphQLNullableType
112118
}
113119

114120
/**
@@ -125,21 +131,21 @@ extension GraphQLUnionType : GraphQLNamedType {}
125131
extension GraphQLEnumType : GraphQLNamedType {}
126132
extension GraphQLInputObjectType : GraphQLNamedType {}
127133

128-
public func getNamedType(type: GraphQLType?) -> GraphQLNamedType? {
134+
public func getNamedType(type: (any GraphQLType)?) -> (any GraphQLNamedType)? {
129135
var unmodifiedType = type
130136

131-
while let type = unmodifiedType as? GraphQLWrapperType {
137+
while let type = unmodifiedType as? (any GraphQLWrapperType) {
132138
unmodifiedType = type.wrappedType
133139
}
134140

135-
return unmodifiedType as? GraphQLNamedType
141+
return unmodifiedType as? (any GraphQLNamedType)
136142
}
137143

138144
/**
139145
* These types wrap other types.
140146
*/
141147
protocol GraphQLWrapperType : GraphQLType {
142-
var wrappedType: GraphQLType { get }
148+
var wrappedType: any GraphQLType { get }
143149
}
144150

145151
extension GraphQLList : GraphQLWrapperType {}
@@ -536,8 +542,8 @@ public typealias GraphQLFieldResolveInput = (
536542
public struct GraphQLResolveInfo {
537543
public let fieldName: String
538544
public let fieldASTs: [Field]
539-
public let returnType: GraphQLOutputType
540-
public let parentType: GraphQLCompositeType
545+
public let returnType: any GraphQLOutputType
546+
public let parentType: any GraphQLCompositeType
541547
public let path: IndexPath
542548
public let schema: GraphQLSchema
543549
public let fragments: [String: FragmentDefinition]
@@ -549,15 +555,15 @@ public struct GraphQLResolveInfo {
549555
public typealias GraphQLFieldMap = [String: GraphQLField]
550556

551557
public struct GraphQLField {
552-
public let type: GraphQLOutputType
558+
public let type: any GraphQLOutputType
553559
public let args: GraphQLArgumentConfigMap
554560
public let deprecationReason: String?
555561
public let description: String?
556562
public let resolve: GraphQLFieldResolve?
557563
public let subscribe: GraphQLFieldResolve?
558564

559565
public init(
560-
type: GraphQLOutputType,
566+
type: any GraphQLOutputType,
561567
description: String? = nil,
562568
deprecationReason: String? = nil,
563569
args: GraphQLArgumentConfigMap = [:]
@@ -571,7 +577,7 @@ public struct GraphQLField {
571577
}
572578

573579
public init(
574-
type: GraphQLOutputType,
580+
type: any GraphQLOutputType,
575581
description: String? = nil,
576582
deprecationReason: String? = nil,
577583
args: GraphQLArgumentConfigMap = [:],
@@ -587,7 +593,7 @@ public struct GraphQLField {
587593
}
588594

589595
public init(
590-
type: GraphQLOutputType,
596+
type: any GraphQLOutputType,
591597
description: String? = nil,
592598
deprecationReason: String? = nil,
593599
args: GraphQLArgumentConfigMap = [:],
@@ -611,7 +617,7 @@ public typealias GraphQLFieldDefinitionMap = [String: GraphQLFieldDefinition]
611617
public final class GraphQLFieldDefinition {
612618
public let name: String
613619
public let description: String?
614-
public internal(set) var type: GraphQLOutputType
620+
public internal(set) var type: any GraphQLOutputType
615621
public let args: [GraphQLArgumentDefinition]
616622
public let resolve: GraphQLFieldResolve?
617623
public let subscribe: GraphQLFieldResolve?
@@ -620,7 +626,7 @@ public final class GraphQLFieldDefinition {
620626

621627
init(
622628
name: String,
623-
type: GraphQLOutputType,
629+
type: any GraphQLOutputType,
624630
description: String? = nil,
625631
deprecationReason: String? = nil,
626632
args: [GraphQLArgumentDefinition] = [],
@@ -640,7 +646,7 @@ public final class GraphQLFieldDefinition {
640646
func replaceTypeReferences(typeMap: TypeMap) throws {
641647
let resolvedType = try resolveTypeReference(type: type, typeMap: typeMap)
642648

643-
guard let outputType = resolvedType as? GraphQLOutputType else {
649+
guard let outputType = resolvedType as? (any GraphQLOutputType) else {
644650
throw GraphQLError(
645651
message: "Resolved type \"\(resolvedType)\" is not a valid output type."
646652
)
@@ -695,12 +701,12 @@ extension GraphQLFieldDefinition : KeySubscriptable {
695701
public typealias GraphQLArgumentConfigMap = [String: GraphQLArgument]
696702

697703
public struct GraphQLArgument {
698-
public let type: GraphQLInputType
704+
public let type: any GraphQLInputType
699705
public let description: String?
700706
public let defaultValue: Map?
701707

702708
public init(
703-
type: GraphQLInputType,
709+
type: any GraphQLInputType,
704710
description: String? = nil,
705711
defaultValue: Map? = nil
706712
) {
@@ -712,13 +718,13 @@ public struct GraphQLArgument {
712718

713719
public struct GraphQLArgumentDefinition {
714720
public let name: String
715-
public let type: GraphQLInputType
721+
public let type: any GraphQLInputType
716722
public let defaultValue: Map?
717723
public let description: String?
718724

719725
init(
720726
name: String,
721-
type: GraphQLInputType,
727+
type: any GraphQLInputType,
722728
defaultValue: Map? = nil,
723729
description: String? = nil
724730
) {
@@ -1365,11 +1371,11 @@ func defineInputObjectFieldMap(
13651371
}
13661372

13671373
public struct InputObjectField {
1368-
public let type: GraphQLInputType
1374+
public let type: any GraphQLInputType
13691375
public let defaultValue: Map?
13701376
public let description: String?
13711377

1372-
public init(type: GraphQLInputType, defaultValue: Map? = nil, description: String? = nil) {
1378+
public init(type: any GraphQLInputType, defaultValue: Map? = nil, description: String? = nil) {
13731379
self.type = type
13741380
self.defaultValue = defaultValue
13751381
self.description = description
@@ -1380,13 +1386,13 @@ public typealias InputObjectFieldMap = [String: InputObjectField]
13801386

13811387
public final class InputObjectFieldDefinition {
13821388
public let name: String
1383-
public internal(set) var type: GraphQLInputType
1389+
public internal(set) var type: any GraphQLInputType
13841390
public let description: String?
13851391
public let defaultValue: Map?
13861392

13871393
init(
13881394
name: String,
1389-
type: GraphQLInputType,
1395+
type: any GraphQLInputType,
13901396
description: String? = nil,
13911397
defaultValue: Map? = nil
13921398
) {
@@ -1399,7 +1405,7 @@ public final class InputObjectFieldDefinition {
13991405
func replaceTypeReferences(typeMap: TypeMap) throws {
14001406
let resolvedType = try resolveTypeReference(type: type, typeMap: typeMap)
14011407

1402-
guard let inputType = resolvedType as? GraphQLInputType else {
1408+
guard let inputType = resolvedType as? (any GraphQLInputType) else {
14031409
throw GraphQLError(
14041410
message: "Resolved type \"\(resolvedType)\" is not a valid input type."
14051411
)
@@ -1464,18 +1470,18 @@ public typealias InputObjectFieldDefinitionMap = [String: InputObjectFieldDefini
14641470
*
14651471
*/
14661472
public final class GraphQLList {
1467-
public let ofType: GraphQLType
1473+
public let ofType: any GraphQLType
14681474
public let kind: TypeKind = .list
14691475

1470-
public init(_ type: GraphQLType) {
1476+
public init(_ type: any GraphQLType) {
14711477
self.ofType = type
14721478
}
14731479

14741480
public init(_ name: String) {
14751481
self.ofType = GraphQLTypeReference(name)
14761482
}
14771483

1478-
var wrappedType: GraphQLType {
1484+
var wrappedType: any GraphQLType {
14791485
return ofType
14801486
}
14811487

@@ -1548,25 +1554,25 @@ extension GraphQLList : Hashable {
15481554
* Note: the enforcement of non-nullability occurs within the executor.
15491555
*/
15501556
public final class GraphQLNonNull {
1551-
public let ofType: GraphQLNullableType
1557+
public let ofType: any GraphQLNullableType
15521558
public let kind: TypeKind = .nonNull
15531559

1554-
public init(_ type: GraphQLNullableType) {
1560+
public init(_ type: any GraphQLNullableType) {
15551561
self.ofType = type
15561562
}
15571563

15581564
public init(_ name: String) {
15591565
self.ofType = GraphQLTypeReference(name)
15601566
}
15611567

1562-
var wrappedType: GraphQLType {
1568+
var wrappedType: any GraphQLType {
15631569
return ofType
15641570
}
15651571

15661572
func replaceTypeReferences(typeMap: TypeMap) throws -> GraphQLNonNull {
15671573
let resolvedType = try resolveTypeReference(type: ofType, typeMap: typeMap)
15681574

1569-
guard let nullableType = resolvedType as? GraphQLNullableType else {
1575+
guard let nullableType = resolvedType as? (any GraphQLNullableType) else {
15701576
throw GraphQLError(
15711577
message: "Resolved type \"\(resolvedType)\" is not a valid nullable type."
15721578
)

0 commit comments

Comments
 (0)