Skip to content

Commit 11e7e09

Browse files
committed
Passe validation rules as parameter to execute function instead of using protocol approach and use default ruleset all the time
1 parent 484b853 commit 11e7e09

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

Sources/Graphiti/API/API.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@ public protocol API {
66
associatedtype ContextType
77
var resolver: Resolver { get }
88
var schema: Schema<Resolver, ContextType> { get }
9-
var validationRules: [(ValidationContext) -> Visitor] { get }
109
}
1110

1211
public extension API {
13-
var validationRules: [(ValidationContext) -> Visitor] {
14-
[]
15-
}
16-
1712
func execute(
1813
request: String,
1914
context: ContextType,
2015
on eventLoopGroup: EventLoopGroup,
2116
variables: [String: Map] = [:],
22-
operationName: String? = nil
17+
operationName: String? = nil,
18+
validationRules: [(ValidationContext) -> Visitor] = []
2319
) -> EventLoopFuture<GraphQLResult> {
2420
return schema.execute(
2521
request: request,
@@ -37,7 +33,8 @@ public extension API {
3733
context: ContextType,
3834
on eventLoopGroup: EventLoopGroup,
3935
variables: [String: Map] = [:],
40-
operationName: String? = nil
36+
operationName: String? = nil,
37+
validationRules: [(ValidationContext) -> Visitor] = []
4138
) -> EventLoopFuture<SubscriptionResult> {
4239
return schema.subscribe(
4340
request: request,
@@ -60,7 +57,8 @@ public extension API {
6057
context: ContextType,
6158
on eventLoopGroup: EventLoopGroup,
6259
variables: [String: Map] = [:],
63-
operationName: String? = nil
60+
operationName: String? = nil,
61+
validationRules: [(ValidationContext) -> Visitor] = []
6462
) async throws -> GraphQLResult {
6563
return try await schema.execute(
6664
request: request,
@@ -79,7 +77,8 @@ public extension API {
7977
context: ContextType,
8078
on eventLoopGroup: EventLoopGroup,
8179
variables: [String: Map] = [:],
82-
operationName: String? = nil
80+
operationName: String? = nil,
81+
validationRules: [(ValidationContext) -> Visitor] = []
8382
) async throws -> SubscriptionResult {
8483
return try await schema.subscribe(
8584
request: request,

Sources/Graphiti/Schema/Schema.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public extension Schema {
7474
) -> EventLoopFuture<GraphQLResult> {
7575
do {
7676
return try graphql(
77-
validationRules: validationRules,
77+
validationRules: GraphQL.specifiedRules + validationRules,
7878
schema: schema,
7979
request: request,
8080
rootValue: resolver,
@@ -99,7 +99,7 @@ public extension Schema {
9999
) -> EventLoopFuture<SubscriptionResult> {
100100
do {
101101
return try graphqlSubscribe(
102-
validationRules: validationRules,
102+
validationRules: GraphQL.specifiedRules + validationRules,
103103
schema: schema,
104104
request: request,
105105
rootValue: resolver,

Tests/GraphitiTests/ValidationRulesTests.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ class ValidationRulesTests: XCTestCase {
1818
}
1919
let api = TestAPI<TestResolver, NoContext>(
2020
resolver: TestResolver(),
21-
schema: testSchema,
22-
validationRules: GraphQL.specifiedRules + [NoIntrospectionRule]
21+
schema: testSchema
2322
)
2423

2524
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
@@ -36,7 +35,8 @@ class ValidationRulesTests: XCTestCase {
3635
}
3736
""",
3837
context: NoContext(),
39-
on: group
38+
on: group,
39+
validationRules: [NoIntrospectionRule]
4040
).wait(),
4141
GraphQLResult(errors: [
4242
.init(message: "GraphQL introspection is not allowed, but the query contained __schema or __type", locations: [.init(line: 2, column: 3)])
@@ -48,11 +48,9 @@ class ValidationRulesTests: XCTestCase {
4848
private class TestAPI<Resolver, ContextType>: API {
4949
public let resolver: Resolver
5050
public let schema: Schema<Resolver, ContextType>
51-
var validationRules: [(ValidationContext) -> Visitor]
5251

53-
init(resolver: Resolver, schema: Schema<Resolver, ContextType>, validationRules: [(ValidationContext) -> Visitor]) {
52+
init(resolver: Resolver, schema: Schema<Resolver, ContextType>) {
5453
self.resolver = resolver
5554
self.schema = schema
56-
self.validationRules = validationRules
5755
}
5856
}

0 commit comments

Comments
 (0)