Skip to content

Commit b05cb27

Browse files
feat!: Remove type name suffixes
Their main intent was to avoid conflicts in the inheriting package, however we've solved the conflict issue by namespacing all generated types.
1 parent 6f4bad0 commit b05cb27

6 files changed

Lines changed: 45 additions & 55 deletions

File tree

Examples/HelloWorldServer/Sources/HelloWorldServer/Resolvers.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ public struct EmailAddress: Scalar {
6565
}
6666

6767
// Now create types that conform to the expected protocols
68-
struct Resolvers: GraphQLGenerated.ResolversProtocol {
68+
struct Resolvers: GraphQLGenerated.Resolvers {
6969
typealias Query = HelloWorldServer.Query
7070
typealias Mutation = HelloWorldServer.Mutation
7171
typealias Subscription = HelloWorldServer.Subscription
7272
}
7373

74-
struct User: GraphQLGenerated.UserProtocol {
74+
struct User: GraphQLGenerated.User {
7575
// User can choose structure
7676
let id: String
7777
let name: String
@@ -101,7 +101,7 @@ struct User: GraphQLGenerated.UserProtocol {
101101
}
102102
}
103103

104-
struct Contact: GraphQLGenerated.ContactProtocol {
104+
struct Contact: GraphQLGenerated.Contact {
105105
// User can choose structure
106106
let email: String
107107

@@ -111,7 +111,7 @@ struct Contact: GraphQLGenerated.ContactProtocol {
111111
}
112112
}
113113

114-
struct Post: GraphQLGenerated.PostProtocol {
114+
struct Post: GraphQLGenerated.Post {
115115
// User can choose structure
116116
let id: String
117117
let title: String
@@ -131,37 +131,37 @@ struct Post: GraphQLGenerated.PostProtocol {
131131
return content
132132
}
133133

134-
func author(context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> any GraphQLGenerated.UserProtocol {
134+
func author(context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> any GraphQLGenerated.User {
135135
return context.users[authorId]!
136136
}
137137
}
138138

139-
struct Query: GraphQLGenerated.QueryProtocol {
139+
struct Query: GraphQLGenerated.Query {
140140
// Required implementations
141-
static func user(id: String, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> (any GraphQLGenerated.UserProtocol)? {
141+
static func user(id: String, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> (any GraphQLGenerated.User)? {
142142
return context.users[id]
143143
}
144144

145-
static func users(context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> [any GraphQLGenerated.UserProtocol] {
146-
return context.users.values.map { $0 as any GraphQLGenerated.UserProtocol }
145+
static func users(context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> [any GraphQLGenerated.User] {
146+
return context.users.values.map { $0 as any GraphQLGenerated.User }
147147
}
148148

149-
static func post(id: String, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> (any GraphQLGenerated.PostProtocol)? {
149+
static func post(id: String, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> (any GraphQLGenerated.Post)? {
150150
return context.posts[id]
151151
}
152152

153-
static func posts(limit _: Int?, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> [any GraphQLGenerated.PostProtocol] {
154-
return context.posts.values.map { $0 as any GraphQLGenerated.PostProtocol }
153+
static func posts(limit _: Int?, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> [any GraphQLGenerated.Post] {
154+
return context.posts.values.map { $0 as any GraphQLGenerated.Post }
155155
}
156156

157-
static func userOrPost(id: String, context: GraphQLContext, info _: GraphQLResolveInfo) async throws -> (any GraphQLGenerated.UserOrPostUnion)? {
157+
static func userOrPost(id: String, context: GraphQLContext, info _: GraphQLResolveInfo) async throws -> (any GraphQLGenerated.UserOrPost)? {
158158
return context.users[id] ?? context.posts[id]
159159
}
160160
}
161161

162-
struct Mutation: GraphQLGenerated.MutationProtocol {
162+
struct Mutation: GraphQLGenerated.Mutation {
163163
// Required implementations
164-
static func upsertUser(userInfo: GraphQLGenerated.UserInfoInput, context: GraphQLContext, info _: GraphQLResolveInfo) -> any GraphQLGenerated.UserProtocol {
164+
static func upsertUser(userInfo: GraphQLGenerated.UserInfo, context: GraphQLContext, info _: GraphQLResolveInfo) -> any GraphQLGenerated.User {
165165
let user = User(
166166
id: userInfo.id,
167167
name: userInfo.name,
@@ -174,10 +174,10 @@ struct Mutation: GraphQLGenerated.MutationProtocol {
174174
}
175175
}
176176

177-
struct Subscription: GraphQLGenerated.SubscriptionProtocol {
177+
struct Subscription: GraphQLGenerated.Subscription {
178178
// Required implementations
179-
static func watchUser(id: String, context: GraphQLContext, info _: GraphQLResolveInfo) async throws -> AnyAsyncSequence<(any GraphQLGenerated.UserProtocol)?> {
180-
return AsyncStream<(any GraphQLGenerated.UserProtocol)?> { continuation in
179+
static func watchUser(id: String, context: GraphQLContext, info _: GraphQLResolveInfo) async throws -> AnyAsyncSequence<(any GraphQLGenerated.User)?> {
180+
return AsyncStream<(any GraphQLGenerated.User)?> { continuation in
181181
context.onTriggerWatch = { [weak context] in
182182
continuation.yield(context?.users[id])
183183
}

Sources/GraphQLGeneratorCore/Generator/SchemaGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package struct SchemaGenerator {
1515
import GraphQLGeneratorRuntime
1616
1717
/// Build a GraphQL schema with the provided resolvers
18-
func buildGraphQLSchema<Resolvers: GraphQLGenerated.ResolversProtocol>(
18+
func buildGraphQLSchema<Resolvers: GraphQLGenerated.Resolvers>(
1919
resolvers: Resolvers.Type,
2020
decoder: MapDecoder = .init()
2121
) throws -> GraphQLSchema {

Sources/GraphQLGeneratorCore/Generator/TypeGenerator.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ package struct TypeGenerator {
1616
1717
"""
1818

19-
// Generate ResolversProtocol
19+
// Generate Resolvers protocol
2020
output += """
2121
2222
enum GraphQLGenerated {
23-
protocol ResolversProtocol: Sendable {
23+
protocol Resolvers: Sendable {
2424
"""
2525
if let queryType = schema.queryType {
2626
output += """
2727
28-
associatedtype Query: \(try swiftTypeDeclaration(for: queryType, includeNamespace: false, nameGenerator: nameGenerator))
28+
associatedtype Query: \(try swiftTypeDeclaration(for: queryType, includeNamespace: true, nameGenerator: nameGenerator))
2929
"""
3030
}
3131
if let mutationType = schema.mutationType {
3232
output += """
3333
34-
associatedtype Mutation: \(try swiftTypeDeclaration(for: mutationType, includeNamespace: false, nameGenerator: nameGenerator))
34+
associatedtype Mutation: \(try swiftTypeDeclaration(for: mutationType, includeNamespace: true, nameGenerator: nameGenerator))
3535
"""
3636
}
3737
if let subscriptionType = schema.subscriptionType {
3838
output += """
3939
40-
associatedtype Subscription: \(try swiftTypeDeclaration(for: subscriptionType, includeNamespace: false, nameGenerator: nameGenerator))
40+
associatedtype Subscription: \(try swiftTypeDeclaration(for: subscriptionType, includeNamespace: true, nameGenerator: nameGenerator))
4141
"""
4242
}
4343
output += """

Sources/GraphQLGeneratorCore/Utilities/swiftTypeName.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ func swiftTypeDeclaration(for type: GraphQLType, includeNamespace: Bool, nameGen
6868
if includeNamespace {
6969
baseName = "GraphQLGenerated.\(baseName)"
7070
}
71-
72-
if type is GraphQLInputObjectType {
73-
return "\(baseName)Input"
74-
} else if type is GraphQLInterfaceType {
75-
return "\(baseName)Interface"
76-
} else if type is GraphQLObjectType {
77-
return "\(baseName)Protocol"
78-
} else if type is GraphQLUnionType {
79-
return "\(baseName)Union"
80-
}
8171
return baseName
8272
}
8373

Tests/GraphQLGeneratorCoreTests/SchemaGeneratorTests.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct SchemaGeneratorTests {
4545
import GraphQLGeneratorRuntime
4646
4747
/// Build a GraphQL schema with the provided resolvers
48-
func buildGraphQLSchema<Resolvers: GraphQLGenerated.ResolversProtocol>(
48+
func buildGraphQLSchema<Resolvers: GraphQLGenerated.Resolvers>(
4949
resolvers: Resolvers.Type,
5050
decoder: MapDecoder = .init()
5151
) throws -> GraphQLSchema {
@@ -63,7 +63,7 @@ struct SchemaGeneratorTests {
6363
foo
6464
""",
6565
resolve: { source, args, context, info in
66-
let parent = try cast(source, to: (any GraphQLGenerated.BarProtocol).self)
66+
let parent = try cast(source, to: (any GraphQLGenerated.Bar).self)
6767
let context = try cast(context, to: GraphQLContext.self)
6868
return try await parent.foo(context: context, info: info)
6969
}
@@ -304,7 +304,7 @@ struct SchemaGeneratorTests {
304304
The ID of the node
305305
\"\"\",
306306
resolve: { source, args, context, info in
307-
let parent = try cast(source, to: (any GraphQLGenerated.NodeInterface).self)
307+
let parent = try cast(source, to: (any GraphQLGenerated.Node).self)
308308
let context = try cast(context, to: GraphQLContext.self)
309309
return try await parent.id(context: context, info: info)
310310
}
@@ -344,7 +344,7 @@ struct SchemaGeneratorTests {
344344
"name": GraphQLField(
345345
type: GraphQLString,
346346
resolve: { source, args, context, info in
347-
let parent = try cast(source, to: (any GraphQLGenerated.ExtendedInterface).self)
347+
let parent = try cast(source, to: (any GraphQLGenerated.Extended).self)
348348
let context = try cast(context, to: GraphQLContext.self)
349349
return try await parent.name(context: context, info: info)
350350
}
@@ -424,15 +424,15 @@ struct SchemaGeneratorTests {
424424
The book title
425425
\"\"\",
426426
resolve: { source, args, context, info in
427-
let parent = try cast(source, to: (any GraphQLGenerated.BookProtocol).self)
427+
let parent = try cast(source, to: (any GraphQLGenerated.Book).self)
428428
let context = try cast(context, to: GraphQLContext.self)
429429
return try await parent.title(context: context, info: info)
430430
}
431431
),
432432
"author": GraphQLField(
433433
type: GraphQLString,
434434
resolve: { source, args, context, info in
435-
let parent = try cast(source, to: (any GraphQLGenerated.BookProtocol).self)
435+
let parent = try cast(source, to: (any GraphQLGenerated.Book).self)
436436
let context = try cast(context, to: GraphQLContext.self)
437437
return try await parent.author(context: context, info: info)
438438
}
@@ -469,15 +469,15 @@ struct SchemaGeneratorTests {
469469
"id": GraphQLField(
470470
type: GraphQLID,
471471
resolve: { source, args, context, info in
472-
let parent = try cast(source, to: (any GraphQLGenerated.UserProtocol).self)
472+
let parent = try cast(source, to: (any GraphQLGenerated.User).self)
473473
let context = try cast(context, to: GraphQLContext.self)
474474
return try await parent.id(context: context, info: info)
475475
}
476476
),
477477
"name": GraphQLField(
478478
type: GraphQLString,
479479
resolve: { source, args, context, info in
480-
let parent = try cast(source, to: (any GraphQLGenerated.UserProtocol).self)
480+
let parent = try cast(source, to: (any GraphQLGenerated.User).self)
481481
let context = try cast(context, to: GraphQLContext.self)
482482
return try await parent.name(context: context, info: info)
483483
}
@@ -687,7 +687,7 @@ struct SchemaGeneratorTests {
687687
A simple field
688688
\"\"\",
689689
resolve: { source, args, context, info in
690-
let parent = try cast(source, to: (any GraphQLGenerated.TestProtocol).self)
690+
let parent = try cast(source, to: (any GraphQLGenerated.Test).self)
691691
let context = try cast(context, to: GraphQLContext.self)
692692
return try await parent.myField(context: context, info: info)
693693
}
@@ -772,7 +772,7 @@ struct SchemaGeneratorTests {
772772
Use newField instead
773773
\"\"\",
774774
resolve: { source, args, context, info in
775-
let parent = try cast(source, to: (any GraphQLGenerated.TestProtocol).self)
775+
let parent = try cast(source, to: (any GraphQLGenerated.Test).self)
776776
let context = try cast(context, to: GraphQLContext.self)
777777
return try await parent.oldField(context: context, info: info)
778778
}
@@ -804,7 +804,7 @@ struct SchemaGeneratorTests {
804804
let expected = """
805805
806806
resolve: { source, args, context, info in
807-
let parent = try cast(source, to: (any GraphQLGenerated.UserProtocol).self)
807+
let parent = try cast(source, to: (any GraphQLGenerated.User).self)
808808
let filter = args["filter"] != .undefined ? try decoder.decode((String?).self, from: args["filter"]) : nil
809809
let context = try cast(context, to: GraphQLContext.self)
810810
return try await parent.posts(filter: filter, context: context, info: info)

Tests/GraphQLGeneratorCoreTests/TypeGeneratorTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct TypeGeneratorTests {
4545

4646
@Test func generateInputStruct() throws {
4747
let inputType = try GraphQLInputObjectType(
48-
name: "CreateUser",
48+
name: "CreateUserInput",
4949
description: "Input for creating a new user",
5050
fields: [
5151
"name": InputObjectField(
@@ -98,10 +98,10 @@ struct TypeGeneratorTests {
9898

9999
let expected = """
100100
101-
struct PersonInputInput: Codable, Sendable {
101+
struct PersonInput: Codable, Sendable {
102102
let name: String
103-
let address: AddressInputInput?
104-
let friends: [PersonInputInput]?
103+
let address: AddressInput?
104+
let friends: [PersonInput]?
105105
}
106106
"""
107107

@@ -143,7 +143,7 @@ struct TypeGeneratorTests {
143143
actual == """
144144
145145
/// B
146-
protocol BInterface: AInterface, Sendable {
146+
protocol B: A, Sendable {
147147
/// foo
148148
func foo(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String
149149
@@ -194,7 +194,7 @@ struct TypeGeneratorTests {
194194
actual == """
195195
196196
/// Foo
197-
protocol FooProtocol: XUnion, AInterface, Sendable {
197+
protocol Foo: X, A, Sendable {
198198
/// foo
199199
func foo(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String
200200
@@ -234,12 +234,12 @@ struct TypeGeneratorTests {
234234
#expect(
235235
actual == """
236236
237-
protocol QueryProtocol: Sendable {
237+
protocol Query: Sendable {
238238
/// foo
239239
static func foo(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String?
240240
241241
/// bar
242-
static func bar(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> (any BarProtocol)?
242+
static func bar(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> (any Bar)?
243243
244244
}
245245
"""
@@ -274,7 +274,7 @@ struct TypeGeneratorTests {
274274
let expected = """
275275
276276
/// Mutations
277-
protocol MutationProtocol: Sendable {
277+
protocol Mutation: Sendable {
278278
/// Create a new user
279279
static func createUser(name: String, email: String, context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String?
280280
@@ -307,7 +307,7 @@ struct TypeGeneratorTests {
307307
#expect(
308308
actual == """
309309
310-
protocol SubscriptionProtocol: Sendable {
310+
protocol Subscription: Sendable {
311311
/// foo
312312
static func watchThis(id: String?, context: GraphQLContext, info: GraphQLResolveInfo) async throws -> AnyAsyncSequence<String?>
313313

0 commit comments

Comments
 (0)