Skip to content

Commit 3d78151

Browse files
refactor: Formatting
1 parent ad54a71 commit 3d78151

16 files changed

Lines changed: 225 additions & 209 deletions

File tree

Examples/HelloWorldServer/Package.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// swift-tools-version: 6.2
1+
// swift-tools-version: 6.0
22

33
import PackageDescription
44

55
let package = Package(
66
name: "HelloWorldServer",
77
platforms: [
8-
.macOS(.v13)
8+
.macOS(.v13),
99
],
1010
dependencies: [
1111
.package(name: "graphql-generator", path: "../.."),
@@ -19,7 +19,7 @@ let package = Package(
1919
.product(name: "GraphQLGeneratorRuntime", package: "graphql-generator"),
2020
],
2121
plugins: [
22-
.plugin(name: "GraphQLGeneratorPlugin", package: "graphql-generator")
22+
.plugin(name: "GraphQLGeneratorPlugin", package: "graphql-generator"),
2323
]
2424
),
2525
.testTarget(
@@ -28,6 +28,6 @@ let package = Package(
2828
"HelloWorldServer",
2929
.product(name: "GraphQL", package: "GraphQL"),
3030
]
31-
)
31+
),
3232
]
3333
)

Examples/HelloWorldServer/Sources/HelloWorldServer/Resolvers.swift

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Context: @unchecked Sendable {
2121
onTriggerWatch()
2222
}
2323
}
24+
2425
// Scalars must be represented by a Swift type of the same name, conforming to the Scalar protocol
2526
public struct EmailAddress: Scalar {
2627
let email: String
@@ -31,16 +32,18 @@ public struct EmailAddress: Scalar {
3132

3233
// Codability conformance. Required for usage in InputObject
3334
public init(from decoder: any Decoder) throws {
34-
self.email = try decoder.singleValueContainer().decode(String.self)
35+
email = try decoder.singleValueContainer().decode(String.self)
3536
}
37+
3638
public func encode(to encoder: any Encoder) throws {
37-
try self.email.encode(to: encoder)
39+
try email.encode(to: encoder)
3840
}
3941

4042
// Scalar conformance. Not necessary, but default methods are very inefficient.
4143
public static func serialize(this: Self) throws -> Map {
4244
return .string(this.email)
4345
}
46+
4447
public static func parseValue(map: Map) throws -> Map {
4548
switch map {
4649
case .string:
@@ -49,6 +52,7 @@ public struct EmailAddress: Scalar {
4952
throw GraphQLError(message: "EmailAddress cannot represent non-string value: \(map)")
5053
}
5154
}
55+
5256
public static func parseLiteral(value: any Value) throws -> Map {
5357
guard let ast = value as? StringValue else {
5458
throw GraphQLError(
@@ -66,6 +70,7 @@ struct Resolvers: ResolversProtocol {
6670
typealias Mutation = HelloWorldServer.Mutation
6771
typealias Subscription = HelloWorldServer.Subscription
6872
}
73+
6974
struct User: UserProtocol {
7075
// User can choose structure
7176
let id: String
@@ -75,31 +80,37 @@ struct User: UserProtocol {
7580
let role: Role?
7681

7782
// Required implementations
78-
func id(context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> String {
83+
func id(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
7984
return id
8085
}
81-
func name(context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> String {
86+
87+
func name(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
8288
return name
8389
}
84-
func email(context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> EmailAddress {
90+
91+
func email(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> EmailAddress {
8592
return EmailAddress(email: email)
8693
}
87-
func age(context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> Int? {
94+
95+
func age(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> Int? {
8896
return age
8997
}
90-
func role(context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> Role? {
98+
99+
func role(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> Role? {
91100
return role
92101
}
93102
}
103+
94104
struct Contact: ContactProtocol {
95105
// User can choose structure
96106
let email: String
97107

98108
// Required implementations
99-
func email(context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> EmailAddress {
109+
func email(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> EmailAddress {
100110
return EmailAddress(email: email)
101111
}
102112
}
113+
103114
struct Post: PostProtocol {
104115
// User can choose structure
105116
let id: String
@@ -108,42 +119,49 @@ struct Post: PostProtocol {
108119
let authorId: String
109120

110121
// Required implementations
111-
func id(context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> String {
122+
func id(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
112123
return id
113124
}
114-
func title(context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> String {
125+
126+
func title(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
115127
return title
116128
}
117-
func content(context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> String {
129+
130+
func content(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
118131
return content
119132
}
120-
func author(context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> any UserProtocol {
133+
134+
func author(context: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> any UserProtocol {
121135
return context.users[authorId]!
122136
}
123137
}
124138

125139
struct Query: QueryProtocol {
126140
// Required implementations
127-
static func user(id: String, context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> (any UserProtocol)? {
141+
static func user(id: String, context: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> (any UserProtocol)? {
128142
return context.users[id]
129143
}
130-
static func users(context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> [any UserProtocol] {
144+
145+
static func users(context: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> [any UserProtocol] {
131146
return context.users.values.map { $0 as any UserProtocol }
132147
}
133-
static func post(id: String, context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> (any PostProtocol)? {
148+
149+
static func post(id: String, context: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> (any PostProtocol)? {
134150
return context.posts[id]
135151
}
136-
static func posts(limit: Int?, context: Context, info: GraphQL.GraphQLResolveInfo) async throws -> [any PostProtocol] {
152+
153+
static func posts(limit _: Int?, context: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> [any PostProtocol] {
137154
return context.posts.values.map { $0 as any PostProtocol }
138155
}
139-
static func userOrPost(id: String, context: Context, info: GraphQLResolveInfo) async throws -> (any UserOrPostUnion)? {
156+
157+
static func userOrPost(id: String, context: Context, info _: GraphQLResolveInfo) async throws -> (any UserOrPostUnion)? {
140158
return context.users[id] ?? context.posts[id]
141159
}
142160
}
143161

144162
struct Mutation: MutationProtocol {
145163
// Required implementations
146-
static func upsertUser(userInfo: UserInfoInput, context: Context, info: GraphQLResolveInfo) -> any UserProtocol {
164+
static func upsertUser(userInfo: UserInfoInput, context: Context, info _: GraphQLResolveInfo) -> any UserProtocol {
147165
let user = User(
148166
id: userInfo.id,
149167
name: userInfo.name,
@@ -158,7 +176,7 @@ struct Mutation: MutationProtocol {
158176

159177
struct Subscription: SubscriptionProtocol {
160178
// Required implementations
161-
static func watchUser(id: String, context: Context, info: GraphQLResolveInfo) async throws -> AnyAsyncSequence<(any UserProtocol)?> {
179+
static func watchUser(id: String, context: Context, info _: GraphQLResolveInfo) async throws -> AnyAsyncSequence<(any UserProtocol)?> {
162180
return AsyncStream<(any UserProtocol)?> { continuation in
163181
context.onTriggerWatch = { [weak context] in
164182
continuation.yield(context?.users[id])

Examples/HelloWorldServer/Tests/HelloWorldServerTests/HelloWorldServerTests.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ struct HelloWorldServerTests {
88
@Test func query() async throws {
99
let schema = try buildGraphQLSchema(resolvers: Resolvers.self)
1010
let context = Context(
11-
users: ["1" : .init(id: "1", name: "John", email: "john@example.com", age: 18, role: .user)],
12-
posts: ["1" : .init(id: "1", title: "Foo", content: "bar", authorId: "1")]
11+
users: ["1": .init(id: "1", name: "John", email: "john@example.com", age: 18, role: .user)],
12+
posts: ["1": .init(id: "1", title: "Foo", content: "bar", authorId: "1")]
1313
)
1414
let actual = try await graphql(
1515
schema: schema,
@@ -43,10 +43,10 @@ struct HelloWorldServerTests {
4343
"name": "John",
4444
"email": "john@example.com",
4545
"age": 18,
46-
"role": "USER"
47-
]
48-
]
49-
]
46+
"role": "USER",
47+
],
48+
],
49+
],
5050
]
5151
)
5252
#expect(actual == expected)
@@ -80,8 +80,8 @@ struct HelloWorldServerTests {
8080
"name": "Jane",
8181
"email": "jane@example.com",
8282
"age": nil,
83-
"role": "USER"
84-
]
83+
"role": "USER",
84+
],
8585
]
8686
)
8787
#expect(actual == expected)
@@ -90,7 +90,7 @@ struct HelloWorldServerTests {
9090
@Test func subscription() async throws {
9191
let schema = try buildGraphQLSchema(resolvers: Resolvers.self)
9292
let context = Context(
93-
users: ["1" : .init(id: "1", name: "John", email: "john@example.com", age: 18, role: .user)],
93+
users: ["1": .init(id: "1", name: "John", email: "john@example.com", age: 18, role: .user)],
9494
posts: [:]
9595
)
9696
let stream = try await graphqlSubscribe(
@@ -120,8 +120,8 @@ struct HelloWorldServerTests {
120120
"name": "John",
121121
"email": "john@example.com",
122122
"age": 18,
123-
"role": "USER"
124-
]
123+
"role": "USER",
124+
],
125125
]
126126
)
127127
)
@@ -135,8 +135,8 @@ struct HelloWorldServerTests {
135135
"name": "John",
136136
"email": "john@example.com",
137137
"age": 18,
138-
"role": "USER"
139-
]
138+
"role": "USER",
139+
],
140140
]
141141
)
142142
)

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let package = Package(
88
.macOS(.v13),
99
.iOS(.v16),
1010
.tvOS(.v16),
11-
.watchOS(.v9)
11+
.watchOS(.v9),
1212
],
1313
products: [
1414
.plugin(
Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import PackagePlugin
21
import Foundation
2+
import PackagePlugin
33

44
@main
55
struct GraphQLGeneratorPlugin: BuildToolPlugin {
@@ -28,11 +28,11 @@ struct GraphQLGeneratorPlugin: BuildToolPlugin {
2828

2929
let outputFiles = [
3030
outputDirectory.appendingPathComponent("Types.swift"),
31-
outputDirectory.appendingPathComponent("Schema.swift")
31+
outputDirectory.appendingPathComponent("Schema.swift"),
3232
]
3333

3434
let arguments = schemaInputs.flatMap { ["\($0.path)"] } + [
35-
"--output-directory", outputDirectory.path
35+
"--output-directory", outputDirectory.path,
3636
]
3737

3838
return [
@@ -42,52 +42,52 @@ struct GraphQLGeneratorPlugin: BuildToolPlugin {
4242
arguments: arguments,
4343
inputFiles: schemaInputs,
4444
outputFiles: outputFiles
45-
)
45+
),
4646
]
4747
}
4848
}
4949

5050
#if canImport(XcodeProjectPlugin)
51-
import XcodeProjectPlugin
52-
53-
extension GraphQLGeneratorPlugin: XcodeBuildToolPlugin {
54-
/// Entry point for creating build commands for targets in Xcode projects.
55-
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
56-
// Find GraphQL schema files
57-
let schemaFiles = target.inputFiles.filter { file in
58-
file.url.pathExtension == "graphql" || file.url.pathExtension == "gql"
51+
import XcodeProjectPlugin
52+
53+
extension GraphQLGeneratorPlugin: XcodeBuildToolPlugin {
54+
/// Entry point for creating build commands for targets in Xcode projects.
55+
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
56+
// Find GraphQL schema files
57+
let schemaFiles = target.inputFiles.filter { file in
58+
file.url.pathExtension == "graphql" || file.url.pathExtension == "gql"
59+
}
60+
61+
// If no schema files found, return early
62+
guard !schemaFiles.isEmpty else { return [] }
63+
64+
// Find the generator tool
65+
let generatorTool = try context.tool(named: "GraphQLGenerator")
66+
67+
// Create output directory for generated files
68+
let outputDirectory = context.pluginWorkDirectoryURL
69+
70+
let schemaInputs = schemaFiles.map(\.url)
71+
72+
let outputFiles = [
73+
outputDirectory.appendingPathComponent("Types.swift"),
74+
outputDirectory.appendingPathComponent("Schema.swift"),
75+
]
76+
77+
let arguments = schemaInputs.flatMap { ["\($0.path)"] } + [
78+
"--output-directory", outputDirectory.path,
79+
]
80+
81+
return [
82+
.buildCommand(
83+
displayName: "Generating GraphQL Swift code from \(schemaFiles.count) schema file(s)",
84+
executable: generatorTool.url,
85+
arguments: arguments,
86+
inputFiles: schemaInputs,
87+
outputFiles: outputFiles
88+
),
89+
]
5990
}
60-
61-
// If no schema files found, return early
62-
guard !schemaFiles.isEmpty else { return [] }
63-
64-
// Find the generator tool
65-
let generatorTool = try context.tool(named: "GraphQLGenerator")
66-
67-
// Create output directory for generated files
68-
let outputDirectory = context.pluginWorkDirectoryURL
69-
70-
let schemaInputs = schemaFiles.map(\.url)
71-
72-
let outputFiles = [
73-
outputDirectory.appendingPathComponent("Types.swift"),
74-
outputDirectory.appendingPathComponent("Schema.swift")
75-
]
76-
77-
let arguments = schemaInputs.flatMap { ["\($0.path)"] } + [
78-
"--output-directory", outputDirectory.path
79-
]
80-
81-
return [
82-
.buildCommand(
83-
displayName: "Generating GraphQL Swift code from \(schemaFiles.count) schema file(s)",
84-
executable: generatorTool.url,
85-
arguments: arguments,
86-
inputFiles: schemaInputs,
87-
outputFiles: outputFiles
88-
)
89-
]
9091
}
91-
}
9292

9393
#endif

Sources/GraphQLGenerator/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Foundation
21
import ArgumentParser
2+
import Foundation
33
import GraphQLGeneratorCore
44

55
@main

Sources/GraphQLGeneratorCore/Generator/CodeGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import GraphQL
33

44
/// Main code generator that orchestrates generation of all Swift files
55
package struct CodeGenerator {
6-
package init() { }
6+
package init() {}
77

88
/// Generate all Swift files from the schema
99
/// Returns a dictionary of filename -> file content

0 commit comments

Comments
 (0)