Skip to content

Commit

Permalink
Make GraphQLType Equatable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lukel97 committed Jun 9, 2022
1 parent 0765b9f commit 3933850
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 126 deletions.
16 changes: 8 additions & 8 deletions Sources/GraphQL/Execution/Execute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ func doesFragmentConditionMatch(
return true
}

if let abstractType = conditionalType as? GraphQLAbstractType {
if let abstractType = conditionalType as? (any GraphQLAbstractType) {
return exeContext.schema.isSubType(
abstractType: abstractType,
maybeSubType: type
Expand Down Expand Up @@ -761,7 +761,7 @@ func resolveOrError(
// in the execution context.
func completeValueCatchingError(
exeContext: ExecutionContext,
returnType: GraphQLType,
returnType: any GraphQLType,
fieldASTs: [Field],
info: GraphQLResolveInfo,
path: IndexPath,
Expand Down Expand Up @@ -813,7 +813,7 @@ func completeValueCatchingError(
// location information.
func completeValueWithLocatedError(
exeContext: ExecutionContext,
returnType: GraphQLType,
returnType: any GraphQLType,
fieldASTs: [Field],
info: GraphQLResolveInfo,
path: IndexPath,
Expand Down Expand Up @@ -863,7 +863,7 @@ func completeValueWithLocatedError(
*/
func completeValue(
exeContext: ExecutionContext,
returnType: GraphQLType,
returnType: any GraphQLType,
fieldASTs: [Field],
info: GraphQLResolveInfo,
path: IndexPath,
Expand Down Expand Up @@ -995,7 +995,7 @@ func completeListValue(
* Complete a Scalar or Enum by serializing to a valid value, returning
* .null if serialization is not possible.
*/
func completeLeafValue(returnType: GraphQLLeafType, result: Any?) throws -> Map {
func completeLeafValue(returnType: any GraphQLLeafType, result: Any?) throws -> Map {
guard let result = result else {
return .null
}
Expand All @@ -1019,7 +1019,7 @@ func completeLeafValue(returnType: GraphQLLeafType, result: Any?) throws -> Map
*/
func completeAbstractValue(
exeContext: ExecutionContext,
returnType: GraphQLAbstractType,
returnType: any GraphQLAbstractType,
fieldASTs: [Field],
info: GraphQLResolveInfo,
path: IndexPath,
Expand All @@ -1042,7 +1042,7 @@ func completeAbstractValue(
}

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

switch resolveResult {
case .name(let name):
Expand Down Expand Up @@ -1139,7 +1139,7 @@ func defaultResolveType(
value: Any,
eventLoopGroup: EventLoopGroup,
info: GraphQLResolveInfo,
abstractType: GraphQLAbstractType
abstractType: any GraphQLAbstractType
) throws -> TypeResolveResult? {
let possibleTypes = info.schema.getPossibleTypes(abstractType: abstractType)

Expand Down
10 changes: 5 additions & 5 deletions Sources/GraphQL/Execution/Values.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func getVariableValue(schema: GraphQLSchema, definitionAST: VariableDefinition,
let type = typeFromAST(schema: schema, inputTypeAST: definitionAST.type)
let variable = definitionAST.variable

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

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

Expand Down Expand Up @@ -168,7 +168,7 @@ func coerceValue(value: Map, type: GraphQLInputType) throws -> Map {
return .dictionary(object)
}

if let leafType = type as? GraphQLLeafType {
if let leafType = type as? (any GraphQLLeafType) {
return try leafType.parseValue(value: value)
}

Expand Down
76 changes: 41 additions & 35 deletions Sources/GraphQL/Type/Definition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import NIO
/**
* These are all of the possible kinds of types.
*/
public protocol GraphQLType : CustomDebugStringConvertible, Encodable, KeySubscriptable {}
public protocol GraphQLType : CustomDebugStringConvertible, Encodable, KeySubscriptable, AnyObject, Equatable {}
extension GraphQLScalarType : GraphQLType {}
extension GraphQLObjectType : GraphQLType {}
extension GraphQLInterfaceType : GraphQLType {}
Expand All @@ -14,6 +14,12 @@ extension GraphQLInputObjectType : GraphQLType {}
extension GraphQLList : GraphQLType {}
extension GraphQLNonNull : GraphQLType {}

extension GraphQLType {
public static func == (lhs: Self, rhs: Self) -> Bool {
ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
}
}

/**
* These types may be used as input types for arguments and directives.
*/
Expand All @@ -27,9 +33,9 @@ extension GraphQLNonNull : GraphQLInputType {}
//extension GraphQLList : GraphQLInputType where Element : GraphQLInputType {}
//extension GraphQLNonNull : GraphQLInputType where Element : (GraphQLScalarType | GraphQLEnumType | GraphQLInputObjectType | GraphQLList<GraphQLInputType>) {}

func isInputType(type: GraphQLType?) -> Bool {
func isInputType(type: (any GraphQLType)?) -> Bool {
let namedType = getNamedType(type: type)
return namedType is GraphQLInputType
return namedType is any GraphQLInputType
}

/**
Expand Down Expand Up @@ -59,7 +65,7 @@ public protocol GraphQLLeafType : GraphQLNamedType {
extension GraphQLScalarType : GraphQLLeafType {}
extension GraphQLEnumType : GraphQLLeafType {}

func isLeafType(type: GraphQLType?) -> Bool {
func isLeafType(type: (any GraphQLType)?) -> Bool {
let namedType = getNamedType(type: type)
return namedType is GraphQLScalarType ||
namedType is GraphQLEnumType
Expand Down Expand Up @@ -103,12 +109,12 @@ extension GraphQLEnumType : GraphQLNullableType {}
extension GraphQLInputObjectType : GraphQLNullableType {}
extension GraphQLList : GraphQLNullableType {}

func getNullableType(type: GraphQLType?) -> GraphQLNullableType? {
func getNullableType(type: (any GraphQLType)?) -> (any GraphQLNullableType)? {
if let type = type as? GraphQLNonNull {
return type.ofType
}

return type as? GraphQLNullableType
return type as? any GraphQLNullableType
}

/**
Expand All @@ -125,21 +131,21 @@ extension GraphQLUnionType : GraphQLNamedType {}
extension GraphQLEnumType : GraphQLNamedType {}
extension GraphQLInputObjectType : GraphQLNamedType {}

public func getNamedType(type: GraphQLType?) -> GraphQLNamedType? {
public func getNamedType(type: (any GraphQLType)?) -> (any GraphQLNamedType)? {
var unmodifiedType = type

while let type = unmodifiedType as? GraphQLWrapperType {
while let type = unmodifiedType as? (any GraphQLWrapperType) {
unmodifiedType = type.wrappedType
}

return unmodifiedType as? GraphQLNamedType
return unmodifiedType as? (any GraphQLNamedType)
}

/**
* These types wrap other types.
*/
protocol GraphQLWrapperType : GraphQLType {
var wrappedType: GraphQLType { get }
var wrappedType: any GraphQLType { get }
}

extension GraphQLList : GraphQLWrapperType {}
Expand Down Expand Up @@ -536,8 +542,8 @@ public typealias GraphQLFieldResolveInput = (
public struct GraphQLResolveInfo {
public let fieldName: String
public let fieldASTs: [Field]
public let returnType: GraphQLOutputType
public let parentType: GraphQLCompositeType
public let returnType: any GraphQLOutputType
public let parentType: any GraphQLCompositeType
public let path: IndexPath
public let schema: GraphQLSchema
public let fragments: [String: FragmentDefinition]
Expand All @@ -549,15 +555,15 @@ public struct GraphQLResolveInfo {
public typealias GraphQLFieldMap = [String: GraphQLField]

public struct GraphQLField {
public let type: GraphQLOutputType
public let type: any GraphQLOutputType
public let args: GraphQLArgumentConfigMap
public let deprecationReason: String?
public let description: String?
public let resolve: GraphQLFieldResolve?
public let subscribe: GraphQLFieldResolve?

public init(
type: GraphQLOutputType,
type: any GraphQLOutputType,
description: String? = nil,
deprecationReason: String? = nil,
args: GraphQLArgumentConfigMap = [:]
Expand All @@ -571,7 +577,7 @@ public struct GraphQLField {
}

public init(
type: GraphQLOutputType,
type: any GraphQLOutputType,
description: String? = nil,
deprecationReason: String? = nil,
args: GraphQLArgumentConfigMap = [:],
Expand All @@ -587,7 +593,7 @@ public struct GraphQLField {
}

public init(
type: GraphQLOutputType,
type: any GraphQLOutputType,
description: String? = nil,
deprecationReason: String? = nil,
args: GraphQLArgumentConfigMap = [:],
Expand All @@ -611,7 +617,7 @@ public typealias GraphQLFieldDefinitionMap = [String: GraphQLFieldDefinition]
public final class GraphQLFieldDefinition {
public let name: String
public let description: String?
public internal(set) var type: GraphQLOutputType
public internal(set) var type: any GraphQLOutputType
public let args: [GraphQLArgumentDefinition]
public let resolve: GraphQLFieldResolve?
public let subscribe: GraphQLFieldResolve?
Expand All @@ -620,7 +626,7 @@ public final class GraphQLFieldDefinition {

init(
name: String,
type: GraphQLOutputType,
type: any GraphQLOutputType,
description: String? = nil,
deprecationReason: String? = nil,
args: [GraphQLArgumentDefinition] = [],
Expand All @@ -640,7 +646,7 @@ public final class GraphQLFieldDefinition {
func replaceTypeReferences(typeMap: TypeMap) throws {
let resolvedType = try resolveTypeReference(type: type, typeMap: typeMap)

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

public struct GraphQLArgument {
public let type: GraphQLInputType
public let type: any GraphQLInputType
public let description: String?
public let defaultValue: Map?

public init(
type: GraphQLInputType,
type: any GraphQLInputType,
description: String? = nil,
defaultValue: Map? = nil
) {
Expand All @@ -712,13 +718,13 @@ public struct GraphQLArgument {

public struct GraphQLArgumentDefinition {
public let name: String
public let type: GraphQLInputType
public let type: any GraphQLInputType
public let defaultValue: Map?
public let description: String?

init(
name: String,
type: GraphQLInputType,
type: any GraphQLInputType,
defaultValue: Map? = nil,
description: String? = nil
) {
Expand Down Expand Up @@ -1365,11 +1371,11 @@ func defineInputObjectFieldMap(
}

public struct InputObjectField {
public let type: GraphQLInputType
public let type: any GraphQLInputType
public let defaultValue: Map?
public let description: String?

public init(type: GraphQLInputType, defaultValue: Map? = nil, description: String? = nil) {
public init(type: any GraphQLInputType, defaultValue: Map? = nil, description: String? = nil) {
self.type = type
self.defaultValue = defaultValue
self.description = description
Expand All @@ -1380,13 +1386,13 @@ public typealias InputObjectFieldMap = [String: InputObjectField]

public final class InputObjectFieldDefinition {
public let name: String
public internal(set) var type: GraphQLInputType
public internal(set) var type: any GraphQLInputType
public let description: String?
public let defaultValue: Map?

init(
name: String,
type: GraphQLInputType,
type: any GraphQLInputType,
description: String? = nil,
defaultValue: Map? = nil
) {
Expand All @@ -1399,7 +1405,7 @@ public final class InputObjectFieldDefinition {
func replaceTypeReferences(typeMap: TypeMap) throws {
let resolvedType = try resolveTypeReference(type: type, typeMap: typeMap)

guard let inputType = resolvedType as? GraphQLInputType else {
guard let inputType = resolvedType as? (any GraphQLInputType) else {
throw GraphQLError(
message: "Resolved type \"\(resolvedType)\" is not a valid input type."
)
Expand Down Expand Up @@ -1464,18 +1470,18 @@ public typealias InputObjectFieldDefinitionMap = [String: InputObjectFieldDefini
*
*/
public final class GraphQLList {
public let ofType: GraphQLType
public let ofType: any GraphQLType
public let kind: TypeKind = .list

public init(_ type: GraphQLType) {
public init(_ type: any GraphQLType) {
self.ofType = type
}

public init(_ name: String) {
self.ofType = GraphQLTypeReference(name)
}

var wrappedType: GraphQLType {
var wrappedType: any GraphQLType {
return ofType
}

Expand Down Expand Up @@ -1548,25 +1554,25 @@ extension GraphQLList : Hashable {
* Note: the enforcement of non-nullability occurs within the executor.
*/
public final class GraphQLNonNull {
public let ofType: GraphQLNullableType
public let ofType: any GraphQLNullableType
public let kind: TypeKind = .nonNull

public init(_ type: GraphQLNullableType) {
public init(_ type: any GraphQLNullableType) {
self.ofType = type
}

public init(_ name: String) {
self.ofType = GraphQLTypeReference(name)
}

var wrappedType: GraphQLType {
var wrappedType: any GraphQLType {
return ofType
}

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

guard let nullableType = resolvedType as? GraphQLNullableType else {
guard let nullableType = resolvedType as? (any GraphQLNullableType) else {
throw GraphQLError(
message: "Resolved type \"\(resolvedType)\" is not a valid nullable type."
)
Expand Down
Loading

0 comments on commit 3933850

Please sign in to comment.