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
24 changes: 24 additions & 0 deletions Benchmarks/Benchmarks/GraphQLBenchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Benchmark
import GraphQL

let benchmarks: @Sendable () -> Void = {
Benchmark("graphql") { _ in
let result = try await graphql(
schema: starWarsSchema,
request: """
query NestedQuery {
hero {
name
friends {
name
appearsIn
friends {
name
}
}
}
}
"""
)
}
}
171 changes: 171 additions & 0 deletions Benchmarks/Benchmarks/StarWarsData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import GraphQL

/**
* This defines a basic set of data for our Star Wars Schema.
*
* This data is hard coded for the sake of the demo, but you could imagine
* fetching this data from a backend service rather than from hardcoded
* values in a more complex demo.
*/

enum Episode: String, Encodable, Sendable {
case newHope = "NEWHOPE"
case empire = "EMPIRE"
case jedi = "JEDI"

init?(_ string: String?) {
guard let string = string else {
return nil
}

self.init(rawValue: string)
}
}

protocol Character: Encodable, Sendable {
var id: String { get }
var name: String { get }
var friends: [String] { get }
var appearsIn: [Episode] { get }
}

struct Human: Character {
let id: String
let name: String
let friends: [String]
let appearsIn: [Episode]
let homePlanet: String?

init(
id: String,
name: String,
friends: [String],
appearsIn: [Episode],
homePlanet: String? = nil
) {
self.id = id
self.name = name
self.friends = friends
self.appearsIn = appearsIn
self.homePlanet = homePlanet
}
}

struct Droid: Character {
let id: String
let name: String
let friends: [String]
let appearsIn: [Episode]
let primaryFunction: String
}

let luke = Human(
id: "1000",
name: "Luke Skywalker",
friends: ["1002", "1003", "2000", "2001"],
appearsIn: [.newHope, .empire, .jedi],
homePlanet: "Tatooine"
)

let vader = Human(
id: "1001",
name: "Darth Vader",
friends: ["1004"],
appearsIn: [.newHope, .empire, .jedi],
homePlanet: "Tatooine"
)

let han = Human(
id: "1002",
name: "Han Solo",
friends: ["1000", "1003", "2001"],
appearsIn: [.newHope, .empire, .jedi]
)

let leia = Human(
id: "1003",
name: "Leia Organa",
friends: ["1000", "1002", "2000", "2001"],
appearsIn: [.newHope, .empire, .jedi],
homePlanet: "Alderaan"
)

let tarkin = Human(
id: "1004",
name: "Wilhuff Tarkin",
friends: ["1001"],
appearsIn: [.newHope]
)

let humanData: [String: Human] = [
"1000": luke,
"1001": vader,
"1002": han,
"1003": leia,
"1004": tarkin,
]

let c3po = Droid(
id: "2000",
name: "C-3PO",
friends: ["1000", "1002", "1003", "2001"],
appearsIn: [.newHope, .empire, .jedi],
primaryFunction: "Protocol"
)

let r2d2 = Droid(
id: "2001",
name: "R2-D2",
friends: ["1000", "1002", "1003"],
appearsIn: [.newHope, .empire, .jedi],
primaryFunction: "Astromech"
)

let droidData: [String: Droid] = [
"2000": c3po,
"2001": r2d2,
]

/**
* Helper function to get a character by ID.
*/
@Sendable func getCharacter(id: String) -> Character? {
return humanData[id] ?? droidData[id]
}

/**
* Allows us to query for a character"s friends.
*/
@Sendable func getFriends(character: Character) -> [Character] {
return character.friends.reduce(into: []) { friends, friendID in
if let friend = getCharacter(id: friendID) {
friends.append(friend)
}
}
}

/**
* Allows us to fetch the undisputed hero of the Star Wars trilogy, R2-D2.
*/
@Sendable func getHero(episode: Episode?) -> Character {
if episode == .empire {
// Luke is the hero of Episode V.
return luke
}
// R2-D2 is the hero otherwise.
return r2d2
}

/**
* Allows us to query for the human with the given id.
*/
@Sendable func getHuman(id: String) -> Human? {
return humanData[id]
}

/**
* Allows us to query for the droid with the given id.
*/
@Sendable func getDroid(id: String) -> Droid? {
return droidData[id]
}
Loading
Loading