Skip to content

Commit 0b783e2

Browse files
1 parent 397c0f4 commit 0b783e2

File tree

4 files changed

+71
-18
lines changed

4 files changed

+71
-18
lines changed

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ let package = Package(
99
],
1010
dependencies: [
1111
.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: [
1721
.product(name: "OrderedCollections", package: "swift-collections"),
22+
.product(name: "Tracing", package: "swift-distributed-tracing"),
1823
]
1924
),
2025
.testTarget(

Sources/GraphQL/GraphQL.swift

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Tracing
12

23
public struct GraphQLResult: Equatable, Codable, Sendable, CustomStringConvertible {
34
public var data: Map?
@@ -98,25 +99,36 @@ public func graphql(
9899
let source = Source(body: request, name: "GraphQL request")
99100
let documentAST = try parse(source: source)
100101

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

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

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

0 commit comments

Comments
 (0)