Skip to content

Commit f8e6ea0

Browse files
refactor(perf): Reduces validation type casting
Reduces benchmark `graphql` request time by 5%, and `validateRequest` time by 13%
1 parent ea7564e commit f8e6ea0

34 files changed

Lines changed: 340 additions & 174 deletions

Sources/GraphQL/Validation/Rules/Custom/NoDeprecatedCustomRule.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
public func NoDeprecatedCustomRule(context: ValidationContext) -> Visitor {
1313
return Visitor(
1414
enter: { node, _, _, _, _ in
15-
if let node = node as? Field {
15+
switch node.kind {
16+
case .field:
17+
let node = node as! Field
1618
if
1719
let fieldDef = context.fieldDef,
1820
let deprecationReason = fieldDef.deprecationReason,
@@ -25,8 +27,9 @@ public func NoDeprecatedCustomRule(context: ValidationContext) -> Visitor {
2527
)
2628
)
2729
}
28-
}
29-
if let node = node as? Argument {
30+
return .continue
31+
case .argument:
32+
let node = node as! Argument
3033
if
3134
let argDef = context.argument,
3235
let deprecationReason = argDef.deprecationReason
@@ -50,8 +53,9 @@ public func NoDeprecatedCustomRule(context: ValidationContext) -> Visitor {
5053
)
5154
}
5255
}
53-
}
54-
if let node = node as? ObjectField {
56+
return .continue
57+
case .objectField:
58+
let node = node as! ObjectField
5559
if
5660
let inputObjectDef = context.parentInputType as? GraphQLInputObjectType,
5761
let inputFieldDef = try? inputObjectDef.getFields()[node.name.value],
@@ -64,8 +68,9 @@ public func NoDeprecatedCustomRule(context: ValidationContext) -> Visitor {
6468
)
6569
)
6670
}
67-
}
68-
if let node = node as? EnumValue {
71+
return .continue
72+
case .enumValue:
73+
let node = node as! EnumValue
6974
if
7075
let enumValueDef = context.typeInfo.enumValue,
7176
let deprecationReason = enumValueDef.deprecationReason,
@@ -78,8 +83,10 @@ public func NoDeprecatedCustomRule(context: ValidationContext) -> Visitor {
7883
)
7984
)
8085
}
86+
return .continue
87+
default:
88+
return .continue
8189
}
82-
return .continue
8390
}
8491
)
8592
}

Sources/GraphQL/Validation/Rules/Custom/NoSchemaIntrospectionCustomRule.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
public func NoSchemaIntrospectionCustomRule(context: ValidationContext) -> Visitor {
1313
return Visitor(
1414
enter: { node, _, _, _, _ in
15-
if let node = node as? Field {
15+
switch node.kind {
16+
case .field:
17+
let node = node as! Field
1618
if
1719
let type = getNamedType(type: context.type),
1820
isIntrospectionType(type: type)
@@ -24,8 +26,10 @@ public func NoSchemaIntrospectionCustomRule(context: ValidationContext) -> Visit
2426
)
2527
)
2628
}
29+
return .continue
30+
default:
31+
return .continue
2732
}
28-
return .continue
2933
}
3034
)
3135
}

Sources/GraphQL/Validation/Rules/ExecutableDefinitionsRule.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ func ExecutableDefinitionsRule(context: ValidationContext) -> Visitor {
2828

2929
return Visitor(
3030
enter: { node, _, _, _, _ in
31-
if let node = node as? Document {
31+
switch node.kind {
32+
case .document:
33+
let node = node as! Document
3234
for definition in node.definitions {
3335
if !isExecutable(definition) {
3436
var defName = "schema"
@@ -45,8 +47,10 @@ func ExecutableDefinitionsRule(context: ValidationContext) -> Visitor {
4547
)
4648
}
4749
}
50+
return .continue
51+
default:
52+
return .continue
4853
}
49-
return .continue
5054
}
5155
)
5256
}

Sources/GraphQL/Validation/Rules/FieldsOnCorrectTypeRule.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ func undefinedFieldMessage(
2424
func FieldsOnCorrectTypeRule(context: ValidationContext) -> Visitor {
2525
return Visitor(
2626
enter: { node, _, _, _, _ in
27-
if let node = node as? Field {
27+
switch node.kind {
28+
case .field:
29+
let node = node as! Field
2830
if let type = context.parentType {
2931
let fieldDef = context.fieldDef
3032
if fieldDef == nil {
@@ -59,9 +61,10 @@ func FieldsOnCorrectTypeRule(context: ValidationContext) -> Visitor {
5961
))
6062
}
6163
}
64+
return .continue
65+
default:
66+
return .continue
6267
}
63-
64-
return .continue
6568
}
6669
)
6770
}

Sources/GraphQL/Validation/Rules/FragmentsOnCompositeTypesRule.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
func FragmentsOnCompositeTypesRule(context: ValidationContext) -> Visitor {
1212
return Visitor(
1313
enter: { node, _, _, _, _ in
14-
if let fragment = node as? InlineFragment {
14+
switch node.kind {
15+
case .inlineFragment:
16+
let fragment = node as! InlineFragment
1517
if let typeCondition = fragment.typeCondition {
1618
if let type = typeFromAST(schema: context.schema, inputTypeAST: typeCondition) {
1719
if type is GraphQLCompositeType {
@@ -28,8 +30,8 @@ func FragmentsOnCompositeTypesRule(context: ValidationContext) -> Visitor {
2830
}
2931
}
3032
return .continue
31-
}
32-
if let fragment = node as? FragmentDefinition {
33+
case .fragmentDefinition:
34+
let fragment = node as! FragmentDefinition
3335
let typeCondition = fragment.typeCondition
3436
if let type = typeFromAST(schema: context.schema, inputTypeAST: typeCondition) {
3537
if type is GraphQLCompositeType {
@@ -45,8 +47,9 @@ func FragmentsOnCompositeTypesRule(context: ValidationContext) -> Visitor {
4547
)
4648
}
4749
return .continue
50+
default:
51+
return .continue
4852
}
49-
return .continue
5053
}
5154
)
5255
}

Sources/GraphQL/Validation/Rules/KnownArgumentNamesOnDirectivesRule.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ func KnownArgumentNamesOnDirectivesRule(
1111

1212
let astDefinitions = context.ast.definitions
1313
for def in astDefinitions {
14-
if let def = def as? DirectiveDefinition {
14+
if def.kind == .directiveDefinition {
15+
let def = def as! DirectiveDefinition
1516
let argsNodes = def.arguments
1617
directiveArgs[def.name.value] = argsNodes.map(\.name.value)
1718
}
1819
}
1920

2021
return Visitor(
2122
enter: { node, _, _, _, _ in
22-
if let directiveNode = node as? Directive {
23+
switch node.kind {
24+
case .directive:
25+
let directiveNode = node as! Directive
2326
let directiveName = directiveNode.name.value
2427
let knownArgs = directiveArgs[directiveName]
2528

@@ -38,8 +41,10 @@ func KnownArgumentNamesOnDirectivesRule(
3841
}
3942
}
4043
}
44+
return .continue
45+
default:
46+
return .continue
4147
}
42-
return .continue
4348
}
4449
)
4550
}

Sources/GraphQL/Validation/Rules/KnownDirectivesRule.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func KnownDirectivesRule(context: SDLorNormalValidationContext) -> Visitor {
2525

2626
return Visitor(
2727
enter: { node, _, _, _, ancestors in
28-
guard let node = node as? Directive else { return .continue }
28+
guard node.kind == .directive else { return .continue }
29+
let node = node as! Directive
2930

3031
let name = node.name.value
3132

Sources/GraphQL/Validation/Rules/KnownFragmentNamesRule.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import Foundation
1111
func KnownFragmentNamesRule(context: ValidationContext) -> Visitor {
1212
return Visitor(
1313
enter: { node, _, _, _, _ in
14-
if let fragmentReference = node as? FragmentSpread {
14+
switch node.kind {
15+
case .fragmentSpread:
16+
let fragmentReference = node as! FragmentSpread
1517
let fragmentName = fragmentReference.name.value
1618
let fragmentDefinition = context.getFragment(name: fragmentName)
1719

@@ -21,9 +23,10 @@ func KnownFragmentNamesRule(context: ValidationContext) -> Visitor {
2123
nodes: [fragmentReference.name]
2224
))
2325
}
26+
return .continue
27+
default:
28+
return .continue
2429
}
25-
26-
return .continue
2730
}
2831
)
2932
}

Sources/GraphQL/Validation/Rules/KnownTypeNamesRule.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ func KnownTypeNamesRule(context: SDLorNormalValidationContext) -> Visitor {
2828

2929
return Visitor(
3030
enter: { node, _, parent, _, ancestors in
31-
if let type = node as? NamedType {
31+
switch node.kind {
32+
case .namedType:
33+
let type = node as! NamedType
3234
let typeName = type.name.value
3335
if !typeNames.contains(typeName) {
3436
let definitionNode = ancestors.count > 2 ? ancestors[2] : parent
@@ -52,8 +54,10 @@ func KnownTypeNamesRule(context: SDLorNormalValidationContext) -> Visitor {
5254
)
5355
)
5456
}
57+
return .continue
58+
default:
59+
return .continue
5560
}
56-
return .continue
5761
}
5862
)
5963
}

Sources/GraphQL/Validation/Rules/LoneAnonymousOperationRule.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ func LoneAnonymousOperationRule(context: ValidationContext) -> Visitor {
1111
var operationCount = 0
1212
return Visitor(
1313
enter: { node, _, _, _, _ in
14-
if let document = node as? Document {
14+
switch node.kind {
15+
case .document:
16+
let document = node as! Document
1517
operationCount = document.definitions.filter { $0 is OperationDefinition }.count
1618
return .continue
17-
}
18-
if let operation = node as? OperationDefinition {
19+
case .operationDefinition:
20+
let operation = node as! OperationDefinition
1921
if operation.name == nil, operationCount > 1 {
2022
context.report(
2123
error: GraphQLError(
@@ -25,8 +27,9 @@ func LoneAnonymousOperationRule(context: ValidationContext) -> Visitor {
2527
)
2628
}
2729
return .continue
30+
default:
31+
return .continue
2832
}
29-
return .continue
3033
}
3134
)
3235
}

0 commit comments

Comments
 (0)