Skip to content

Commit 3bbca71

Browse files
committed
Re-added context
1 parent 9f4456e commit 3bbca71

File tree

8 files changed

+61
-47
lines changed

8 files changed

+61
-47
lines changed

Sources/GraphQL/Execution/Execute.swift

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public final class ExecutionContext {
3737
public let schema: GraphQLSchema
3838
public let fragments: [String: FragmentDefinition]
3939
public let rootValue: Any
40+
public let context: Any
4041
public let eventLoopGroup: EventLoopGroup
4142
public let operation: OperationDefinition
4243
public let variableValues: [String: Map]
@@ -62,6 +63,7 @@ public final class ExecutionContext {
6263
schema: GraphQLSchema,
6364
fragments: [String: FragmentDefinition],
6465
rootValue: Any,
66+
context: Any,
6567
eventLoopGroup: EventLoopGroup,
6668
operation: OperationDefinition,
6769
variableValues: [String: Map],
@@ -74,6 +76,7 @@ public final class ExecutionContext {
7476
self.schema = schema
7577
self.fragments = fragments
7678
self.rootValue = rootValue
79+
self.context = context
7780
self.eventLoopGroup = eventLoopGroup
7881
self.operation = operation
7982
self.variableValues = variableValues
@@ -226,24 +229,26 @@ func execute(
226229
schema: GraphQLSchema,
227230
documentAST: Document,
228231
rootValue: Any,
232+
context: Any,
229233
eventLoopGroup: EventLoopGroup,
230234
variableValues: [String: Map] = [:],
231235
operationName: String? = nil
232236
) -> EventLoopFuture<Map> {
233237
let executeStarted = instrumentation.now
234-
let context: ExecutionContext
238+
let buildContext: ExecutionContext
235239

236240
do {
237241
// If a valid context cannot be created due to incorrect arguments,
238242
// this will throw an error.
239-
context = try buildExecutionContext(
243+
buildContext = try buildExecutionContext(
240244
queryStrategy: queryStrategy,
241245
mutationStrategy: mutationStrategy,
242246
subscriptionStrategy: subscriptionStrategy,
243247
instrumentation: instrumentation,
244248
schema: schema,
245249
documentAST: documentAST,
246250
rootValue: rootValue,
251+
context: context,
247252
eventLoopGroup: eventLoopGroup,
248253
rawVariableValues: variableValues,
249254
operationName: operationName
@@ -272,8 +277,8 @@ func execute(
272277
do {
273278
var executeErrors: [GraphQLError] = []
274279

275-
return try executeOperation(exeContext: context,
276-
operation: context.operation,
280+
return try executeOperation(exeContext: buildContext,
281+
operation: buildContext.operation,
277282
rootValue: rootValue)
278283

279284
.thenThrowing { data -> Map in
@@ -282,10 +287,10 @@ func execute(
282287
dataMap[key] = try map(from: value)
283288
}
284289
var result: [String: Map] = ["data": dataMap]
285-
if !context.errors.isEmpty {
286-
result["errors"] = context.errors.map
290+
if !buildContext.errors.isEmpty {
291+
result["errors"] = buildContext.errors.map
287292
}
288-
executeErrors = context.errors
293+
executeErrors = buildContext.errors
289294

290295
return .dictionary(result)
291296
}.mapIfError{ error -> Map in
@@ -305,7 +310,7 @@ func execute(
305310
rootValue: rootValue,
306311
eventLoopGroup: eventLoopGroup,
307312
variableValues: variableValues,
308-
operation: context.operation,
313+
operation: buildContext.operation,
309314
errors: executeErrors,
310315
result: result
311316
)
@@ -333,6 +338,7 @@ func buildExecutionContext(
333338
schema: GraphQLSchema,
334339
documentAST: Document,
335340
rootValue: Any,
341+
context: Any,
336342
eventLoopGroup: EventLoopGroup,
337343
rawVariableValues: [String: Map],
338344
operationName: String?
@@ -387,6 +393,7 @@ func buildExecutionContext(
387393
schema: schema,
388394
fragments: fragments,
389395
rootValue: rootValue,
396+
context: context,
390397
eventLoopGroup: eventLoopGroup,
391398
operation: operation,
392399
variableValues: variableValues,
@@ -673,7 +680,7 @@ public func resolveField(
673680
// The resolve func's optional third argument is a context value that
674681
// is provided to every resolve func within an execution. It is commonly
675682
// used to represent an authenticated user, or request-specific caches.
676-
let eventLoopGroup = exeContext.eventLoopGroup
683+
let context = exeContext.context
677684

678685
// The resolve func's optional fourth argument is a collection of
679686
// information about the current execution state.
@@ -698,7 +705,8 @@ public func resolveField(
698705
resolve: resolve,
699706
source: source,
700707
args: args,
701-
eventLoopGroup: eventLoopGroup,
708+
context: context,
709+
eventLoopGroup: exeContext.eventLoopGroup,
702710
info: info
703711
)
704712

@@ -709,7 +717,7 @@ public func resolveField(
709717
finished: exeContext.instrumentation.now,
710718
source: source,
711719
args: args,
712-
eventLoopGroup: eventLoopGroup,
720+
eventLoopGroup: exeContext.eventLoopGroup,
713721
info: info,
714722
result: result
715723
)
@@ -735,11 +743,12 @@ func resolveOrError(
735743
resolve: GraphQLFieldResolve,
736744
source: Any,
737745
args: Map,
746+
context: Any,
738747
eventLoopGroup: EventLoopGroup,
739748
info: GraphQLResolveInfo
740749
) -> ResultOrError<EventLoopFuture<Any?>, Error> {
741750
do {
742-
return try .result(resolve(source, args, eventLoopGroup, info))
751+
return try .result(resolve(source, args, context, eventLoopGroup, info))
743752
} catch {
744753
return .error(error)
745754
}
@@ -1140,7 +1149,7 @@ func defaultResolveType(
11401149
* which takes the property of the source object of the same name as the field
11411150
* and returns it as the result.
11421151
*/
1143-
func defaultResolve(source: Any, args: Map, eventLoopGroup: EventLoopGroup, info: GraphQLResolveInfo) -> EventLoopFuture<Any?> {
1152+
func defaultResolve(source: Any, args: Map, context: Any, eventLoopGroup: EventLoopGroup, info: GraphQLResolveInfo) -> EventLoopFuture<Any?> {
11441153
guard let source = unwrap(source) else {
11451154
return eventLoopGroup.next().newSucceededFuture(result: nil)
11461155
}

Sources/GraphQL/GraphQL.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public func graphql(
3030
schema: GraphQLSchema,
3131
request: String,
3232
rootValue: Any = Void(),
33+
context: Any = Void(),
3334
eventLoopGroup: EventLoopGroup,
3435
variableValues: [String: Map] = [:],
3536
operationName: String? = nil
@@ -51,6 +52,7 @@ public func graphql(
5152
schema: schema,
5253
documentAST: documentAST,
5354
rootValue: rootValue,
55+
context: context,
5456
eventLoopGroup: eventLoopGroup,
5557
variableValues: variableValues,
5658
operationName: operationName
@@ -82,6 +84,7 @@ public func graphql<Retrieval:PersistedQueryRetrieval>(
8284
queryRetrieval: Retrieval,
8385
queryId: Retrieval.Id,
8486
rootValue: Any = Void(),
87+
context: Any = Void(),
8588
eventLoopGroup: EventLoopGroup,
8689
variableValues: [String: Map] = [:],
8790
operationName: String? = nil
@@ -102,6 +105,7 @@ public func graphql<Retrieval:PersistedQueryRetrieval>(
102105
schema: schema,
103106
documentAST: documentAST,
104107
rootValue: rootValue,
108+
context: context,
105109
eventLoopGroup: eventLoopGroup,
106110
variableValues: variableValues,
107111
operationName: operationName

Sources/GraphQL/Type/Definition.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ public typealias GraphQLIsTypeOf = (
449449
public typealias GraphQLFieldResolve = (
450450
_ source: Any,
451451
_ args: Map,
452+
_ context: Any,
452453
_ eventLoopGroup: EventLoopGroup,
453454
_ info: GraphQLResolveInfo
454455
) throws -> EventLoopFuture<Any?>

Sources/GraphQL/Type/Introspection.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let __Schema = try! GraphQLObjectType(
1010
"types": GraphQLField(
1111
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Type))),
1212
description: "A list of all types supported by this server.",
13-
resolve: { schema, _, eventLoopGroup, _ in
13+
resolve: { schema, _, _, eventLoopGroup, _ in
1414
guard let schema = schema as? GraphQLSchema else {
1515
return eventLoopGroup.next().newSucceededFuture(result: nil)
1616
}
@@ -22,7 +22,7 @@ let __Schema = try! GraphQLObjectType(
2222
"queryType": GraphQLField(
2323
type: GraphQLNonNull(__Type),
2424
description: "The type that query operations will be rooted at.",
25-
resolve: { schema, _, eventLoopGroup, _ in
25+
resolve: { schema, _, _, eventLoopGroup, _ in
2626
guard let schema = schema as? GraphQLSchema else {
2727
return eventLoopGroup.next().newSucceededFuture(result: nil)
2828
}
@@ -35,7 +35,7 @@ let __Schema = try! GraphQLObjectType(
3535
description:
3636
"If this server supports mutation, the type that " +
3737
"mutation operations will be rooted at.",
38-
resolve: { schema, _, eventLoopGroup, _ in
38+
resolve: { schema, _, _, eventLoopGroup, _ in
3939
guard let schema = schema as? GraphQLSchema else {
4040
return eventLoopGroup.next().newSucceededFuture(result: nil)
4141
}
@@ -48,7 +48,7 @@ let __Schema = try! GraphQLObjectType(
4848
description:
4949
"If this server support subscription, the type that " +
5050
"subscription operations will be rooted at.",
51-
resolve: { schema, _, eventLoopGroup, _ in
51+
resolve: { schema, _, _, eventLoopGroup, _ in
5252
guard let schema = schema as? GraphQLSchema else {
5353
return eventLoopGroup.next().newSucceededFuture(result: nil)
5454
}
@@ -59,7 +59,7 @@ let __Schema = try! GraphQLObjectType(
5959
"directives": GraphQLField(
6060
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Directive))),
6161
description: "A list of all directives supported by this server.",
62-
resolve: { schema, _, eventLoopGroup, _ in
62+
resolve: { schema, _, _, eventLoopGroup, _ in
6363
guard let schema = schema as? GraphQLSchema else {
6464
return eventLoopGroup.next().newSucceededFuture(result: nil)
6565
}
@@ -87,7 +87,7 @@ let __Directive = try! GraphQLObjectType(
8787
),
8888
"args": GraphQLField(
8989
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))),
90-
resolve: { directive, _, eventLoopGroup, _ in
90+
resolve: { directive, _, _, eventLoopGroup, _ in
9191
guard let directive = directive as? GraphQLDirective else {
9292
return eventLoopGroup.next().newSucceededFuture(result: nil)
9393
}
@@ -100,7 +100,7 @@ let __Directive = try! GraphQLObjectType(
100100
"onOperation": GraphQLField(
101101
type: GraphQLNonNull(GraphQLBoolean),
102102
deprecationReason: "Use `locations`.",
103-
resolve: { directive, _, eventLoopGroup, _ in
103+
resolve: { directive, _, _, eventLoopGroup, _ in
104104
guard let d = directive as? GraphQLDirective else {
105105
return eventLoopGroup.next().newSucceededFuture(result: nil)
106106
}
@@ -113,7 +113,7 @@ let __Directive = try! GraphQLObjectType(
113113
"onFragment": GraphQLField(
114114
type: GraphQLNonNull(GraphQLBoolean),
115115
deprecationReason: "Use `locations`.",
116-
resolve: { directive, _, eventLoopGroup, _ in
116+
resolve: { directive, _, _, eventLoopGroup, _ in
117117
guard let d = directive as? GraphQLDirective else {
118118
return eventLoopGroup.next().newSucceededFuture(result: nil)
119119
}
@@ -126,7 +126,7 @@ let __Directive = try! GraphQLObjectType(
126126
"onField": GraphQLField(
127127
type: GraphQLNonNull(GraphQLBoolean),
128128
deprecationReason: "Use `locations`.",
129-
resolve: { directive, _, eventLoopGroup, _ in
129+
resolve: { directive, _, _, eventLoopGroup, _ in
130130
guard let d = directive as? GraphQLDirective else {
131131
return eventLoopGroup.next().newSucceededFuture(result: nil)
132132
}
@@ -232,7 +232,7 @@ let __Type: GraphQLObjectType = try! GraphQLObjectType(
232232
fields: [
233233
"kind": GraphQLField(
234234
type: GraphQLNonNull(__TypeKind),
235-
resolve: { type, _, eventLoopGroup, _ in
235+
resolve: { type, _, _, eventLoopGroup, _ in
236236
switch type {
237237
case let type as GraphQLScalarType:
238238
return eventLoopGroup.next().newSucceededFuture(result: TypeKind.scalar)
@@ -265,7 +265,7 @@ let __Type: GraphQLObjectType = try! GraphQLObjectType(
265265
defaultValue: false
266266
)
267267
],
268-
resolve: { type, arguments, eventLoopGroup, _ in
268+
resolve: { type, arguments, _, eventLoopGroup, _ in
269269
if let type = type as? GraphQLObjectType {
270270
let fieldMap = type.fields
271271
var fields = Array(fieldMap.values).sorted(by: { $0.name < $1.name })
@@ -293,7 +293,7 @@ let __Type: GraphQLObjectType = try! GraphQLObjectType(
293293
),
294294
"interfaces": GraphQLField(
295295
type: GraphQLList(GraphQLNonNull(GraphQLTypeReference("__Type"))),
296-
resolve: { type, _, eventLoopGroup, _ in
296+
resolve: { type, _, _, eventLoopGroup, _ in
297297
if let type = type as? GraphQLObjectType {
298298
return eventLoopGroup.next().newSucceededFuture(result: type.interfaces)
299299
}
@@ -303,7 +303,7 @@ let __Type: GraphQLObjectType = try! GraphQLObjectType(
303303
),
304304
"possibleTypes": GraphQLField(
305305
type: GraphQLList(GraphQLNonNull(GraphQLTypeReference("__Type"))),
306-
resolve: { type, args, eventLoopGroup, info in
306+
resolve: { type, args, _, eventLoopGroup, info in
307307
if let type = type as? GraphQLAbstractType {
308308
return eventLoopGroup.next().newSucceededFuture(result: info.schema.getPossibleTypes(abstractType: type))
309309
}
@@ -319,7 +319,7 @@ let __Type: GraphQLObjectType = try! GraphQLObjectType(
319319
defaultValue: false
320320
)
321321
],
322-
resolve: { type, arguments, eventLoopGroup, _ in
322+
resolve: { type, arguments, _, eventLoopGroup, _ in
323323
if let type = type as? GraphQLEnumType {
324324
var values = type.values
325325

@@ -335,7 +335,7 @@ let __Type: GraphQLObjectType = try! GraphQLObjectType(
335335
),
336336
"inputFields": GraphQLField(
337337
type: GraphQLList(GraphQLNonNull(__InputValue)),
338-
resolve: { type, _, eventLoopGroup, _ in
338+
resolve: { type, _, _, eventLoopGroup, _ in
339339
if let type = type as? GraphQLInputObjectType {
340340
let fieldMap = type.fields
341341
return eventLoopGroup.next().newSucceededFuture(result: Array(fieldMap.values).sorted(by: { $0.name < $1.name }))
@@ -358,7 +358,7 @@ let __Field = try! GraphQLObjectType(
358358
"description": GraphQLField(type: GraphQLString),
359359
"args": GraphQLField(
360360
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))),
361-
resolve: { field, _, eventLoopGroup, _ in
361+
resolve: { field, _, _, eventLoopGroup, _ in
362362
guard let field = field as? GraphQLFieldDefinition else {
363363
return eventLoopGroup.next().newSucceededFuture(result: nil)
364364
}
@@ -387,7 +387,7 @@ let __InputValue = try! GraphQLObjectType(
387387
description:
388388
"A GraphQL-formatted string representing the default value for this " +
389389
"input value.",
390-
resolve: { inputValue, _, eventLoopGroup, _ in
390+
resolve: { inputValue, _, _, eventLoopGroup, _ in
391391
guard let inputValue = inputValue as? GraphQLArgumentDefinition else {
392392
return eventLoopGroup.next().newSucceededFuture(result: nil)
393393
}
@@ -491,7 +491,7 @@ let SchemaMetaFieldDef = GraphQLFieldDefinition(
491491
name: "__schema",
492492
type: GraphQLNonNull(__Schema),
493493
description: "Access the current type schema of this server.",
494-
resolve: { _, _, eventLoopGroup, info in
494+
resolve: { _, _, _, eventLoopGroup, info in
495495
return eventLoopGroup.next().newSucceededFuture(result: info.schema)
496496
}
497497
)
@@ -506,7 +506,7 @@ let TypeMetaFieldDef = GraphQLFieldDefinition(
506506
type: GraphQLNonNull(GraphQLString)
507507
)
508508
],
509-
resolve: { _, arguments, eventLoopGroup, info in
509+
resolve: { _, arguments, _, eventLoopGroup, info in
510510
let name = arguments["name"].string!
511511
return eventLoopGroup.next().newSucceededFuture(result: info.schema.getType(name: name))
512512
}
@@ -516,7 +516,7 @@ let TypeNameMetaFieldDef = GraphQLFieldDefinition(
516516
name: "__typename",
517517
type: GraphQLNonNull(GraphQLString),
518518
description: "The name of the current Object type at runtime.",
519-
resolve: { _, _, eventLoopGroup, info in
519+
resolve: { _, _, _, eventLoopGroup, info in
520520
eventLoopGroup.next().newSucceededFuture(result: info.parentType.name)
521521
}
522522
)

Tests/GraphQLTests/FieldExecutionStrategyTests/FieldExecutionStrategyTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class FieldExecutionStrategyTests: XCTestCase {
1515
fields: [
1616
"sleep": GraphQLField(
1717
type: GraphQLString,
18-
resolve: { _, _, eventLoopGroup, _ in
18+
resolve: { _, _, _, eventLoopGroup, _ in
1919
let g = DispatchGroup()
2020
g.enter()
2121
DispatchQueue.global().asyncAfter(wallDeadline: .now() + 0.1) {
@@ -27,7 +27,7 @@ class FieldExecutionStrategyTests: XCTestCase {
2727
),
2828
"bang": GraphQLField(
2929
type: GraphQLString,
30-
resolve: { (_, _, _, info: GraphQLResolveInfo) in
30+
resolve: { (_, _, _, _, info: GraphQLResolveInfo) in
3131
let g = DispatchGroup()
3232
g.enter()
3333
DispatchQueue.global().asyncAfter(wallDeadline: .now() + 0.1) {

0 commit comments

Comments
 (0)