Skip to content

Commit 84a00ab

Browse files
1 parent a722ef9 commit 84a00ab

4 files changed

Lines changed: 75 additions & 20 deletions

File tree

Benchmarks/Package.resolved

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.resolved

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ let package = Package(
88
.library(name: "GraphQL", targets: ["GraphQL"])
99
],
1010
dependencies: [
11-
.package(url: "https://github.com/apple/swift-collections", .upToNextMajor(from: "1.0.0"))
11+
.package(url: "https://github.com/apple/swift-collections", .upToNextMajor(from: "1.0.0")),
12+
.package(
13+
url: "https://github.com/apple/swift-distributed-tracing",
14+
.upToNextMajor(from: "1.0.0")
15+
),
1216
],
1317
targets: [
1418
.target(
1519
name: "GraphQL",
1620
dependencies: [
17-
.product(name: "OrderedCollections", package: "swift-collections")
21+
.product(name: "OrderedCollections", package: "swift-collections"),
22+
.product(name: "Tracing", package: "swift-distributed-tracing"),
1823
]
1924
),
2025
.testTarget(

Sources/GraphQL/GraphQL.swift

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Tracing
2+
13
public struct GraphQLResult: Equatable, Codable, Sendable, CustomStringConvertible {
24
public var data: Map?
35
public var errors: [GraphQLError]
@@ -97,25 +99,37 @@ public func graphql(
9799
let source = Source(body: request, name: "GraphQL request")
98100
let documentAST = try parse(source: source)
99101

100-
// Validate
101-
let validationErrors = validate(
102-
schema: schema,
103-
ast: documentAST,
104-
rules: validationRules
105-
)
106-
guard validationErrors.isEmpty else {
107-
return GraphQLResult(errors: validationErrors)
108-
}
102+
let operation =
103+
documentAST.definitions.first {
104+
$0 is OperationDefinition
105+
} as! OperationDefinition?
109106

110-
// Execute
111-
return try await execute(
112-
schema: schema,
113-
documentAST: documentAST,
114-
rootValue: rootValue,
115-
context: context,
116-
variableValues: variableValues,
117-
operationName: operationName
118-
)
107+
// See https://opentelemetry.io/docs/specs/semconv/graphql/graphql-spans/
108+
return try await withSpan(operation?.operation.rawValue ?? "GraphQL Operation") { span in
109+
// `graphql.document` was omitted due to the possibility of very large sizes.
110+
span.attributes["graphql.operation.name"] = operation?.name?.value
111+
span.attributes["graphql.operation.type"] = operation?.operation.rawValue
112+
113+
// Validate
114+
let validationErrors = validate(
115+
schema: schema,
116+
ast: documentAST,
117+
rules: validationRules
118+
)
119+
guard validationErrors.isEmpty else {
120+
return GraphQLResult(errors: validationErrors)
121+
}
122+
123+
// Execute
124+
return try await execute(
125+
schema: schema,
126+
documentAST: documentAST,
127+
rootValue: rootValue,
128+
context: context,
129+
variableValues: variableValues,
130+
operationName: operationName
131+
)
132+
}
119133
}
120134

121135
/// This is the primary entry point function for fulfilling GraphQL operations

0 commit comments

Comments
 (0)