Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Benchmarks/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-collections", .upToNextMajor(from: "1.0.0")),
.package(
url: "https://github.com/apple/swift-distributed-tracing",
.upToNextMajor(from: "1.0.0")
),
],
targets: [
.target(
name: "GraphQL",
dependencies: [
.product(name: "OrderedCollections", package: "swift-collections"),
.product(name: "Tracing", package: "swift-distributed-tracing"),
]
),
.testTarget(
Expand Down
48 changes: 30 additions & 18 deletions Sources/GraphQL/GraphQL.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Tracing

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

// Validate
let validationErrors = validate(
schema: schema,
ast: documentAST,
rules: validationRules
)
guard validationErrors.isEmpty else {
return GraphQLResult(errors: validationErrors)
}
let operation = documentAST.definitions.first {
$0 is OperationDefinition
} as! OperationDefinition?

// Execute
return try await execute(
schema: schema,
documentAST: documentAST,
rootValue: rootValue,
context: context,
variableValues: variableValues,
operationName: operationName
)
// See https://opentelemetry.io/docs/specs/semconv/graphql/graphql-spans/
return try await withSpan(operation?.operation.rawValue ?? "GraphQL Operation") { span in
// `graphql.document` was omitted due to the possibility of very large sizes.
span.attributes["graphql.operation.name"] = operation?.name?.value
span.attributes["graphql.operation.type"] = operation?.operation.rawValue

// Validate
let validationErrors = validate(
schema: schema,
ast: documentAST,
rules: validationRules
)
guard validationErrors.isEmpty else {
return GraphQLResult(errors: validationErrors)
}

// Execute
return try await execute(
schema: schema,
documentAST: documentAST,
rootValue: rootValue,
context: context,
variableValues: variableValues,
operationName: operationName
)
}
}

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