@@ -4,8 +4,12 @@ import Testing
44
55@Suite
66struct TypeGeneratorTests {
7- @Test func enumType( ) async throws {
8- let actual = try TypeGenerator ( ) . generateEnum (
7+ let generator = TypeGenerator ( )
8+
9+ // MARK: - Enum Tests
10+
11+ @Test func generateEnum( ) async throws {
12+ let actual = try generator. generateEnum (
913 for: . init(
1014 name: " Foo " ,
1115 description: " foo " ,
@@ -18,6 +22,9 @@ struct TypeGeneratorTests {
1822 value: . string( " bar " ) ,
1923 description: " bar "
2024 ) ,
25+ " baz " : . init(
26+ value: . string( " baz " )
27+ ) ,
2128 ]
2229 )
2330 )
@@ -30,12 +37,78 @@ struct TypeGeneratorTests {
3037 case foo = " foo "
3138 /// bar
3239 case bar = " bar "
40+ case baz = " baz "
3341 }
3442 """
3543 )
3644 }
3745
38- @Test func interfaceType( ) async throws {
46+ @Test func generateInputStruct( ) throws {
47+ let inputType = try GraphQLInputObjectType (
48+ name: " CreateUser " ,
49+ description: " Input for creating a new user " ,
50+ fields: [
51+ " name " : InputObjectField (
52+ type: GraphQLNonNull ( GraphQLString) ,
53+ description: " User's full name "
54+ ) ,
55+ " email " : InputObjectField (
56+ type: GraphQLNonNull ( GraphQLString)
57+ ) ,
58+ " age " : InputObjectField (
59+ type: GraphQLInt,
60+ description: " User's age "
61+ ) ,
62+ ]
63+ )
64+
65+ let result = try generator. generateInputStruct ( for: inputType)
66+
67+ let expected = """
68+
69+ /// Input for creating a new user
70+ struct CreateUserInput: Codable, Sendable {
71+ /// User's full name
72+ let name: String
73+ let email: String
74+ /// User's age
75+ let age: Int?
76+ }
77+ """
78+
79+ #expect( result == expected)
80+ }
81+
82+ @Test func generateInputStructWithRecursiveTypes( ) throws {
83+ let addressInput = try GraphQLInputObjectType (
84+ name: " AddressInput "
85+ )
86+ let personInput = try GraphQLInputObjectType (
87+ name: " PersonInput "
88+ )
89+ personInput. fields = {
90+ [
91+ " name " : InputObjectField ( type: GraphQLNonNull ( GraphQLString) ) ,
92+ " address " : InputObjectField ( type: addressInput) ,
93+ " friends " : InputObjectField ( type: GraphQLList ( personInput) ) ,
94+ ]
95+ }
96+
97+ let result = try generator. generateInputStruct ( for: personInput)
98+
99+ let expected = """
100+
101+ struct PersonInputInput: Codable, Sendable {
102+ let name: String
103+ let address: AddressInputInput?
104+ let friends: [PersonInputInput]?
105+ }
106+ """
107+
108+ #expect( result == expected)
109+ }
110+
111+ @Test func generateInterfaceProtocol( ) async throws {
39112 let interfaceA = try GraphQLInterfaceType (
40113 name: " A " ,
41114 description: " A "
@@ -53,7 +126,15 @@ struct TypeGeneratorTests {
53126 ) ,
54127 " baz " : . init(
55128 type: GraphQLString,
56- description: " baz "
129+ description: " baz " ,
130+ args: [
131+ " arg1 " : GraphQLArgument (
132+ type: GraphQLNonNull ( GraphQLString)
133+ ) ,
134+ " arg2 " : GraphQLArgument (
135+ type: GraphQLInt
136+ ) ,
137+ ]
57138 ) ,
58139 ]
59140 )
@@ -67,14 +148,14 @@ struct TypeGeneratorTests {
67148 func foo(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String
68149
69150 /// baz
70- func baz(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String?
151+ func baz(arg1: String, arg2: Int?, context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String?
71152
72153 }
73154 """
74155 )
75156 }
76157
77- @Test func objectType ( ) async throws {
158+ @Test func generateTypeProtocol ( ) async throws {
78159 let interfaceA = try GraphQLInterfaceType (
79160 name: " A " ,
80161 description: " A "
@@ -92,12 +173,10 @@ struct TypeGeneratorTests {
92173 description: " bar " ,
93174 args: [
94175 " foo " : . init(
95- type: GraphQLNonNull ( GraphQLString) ,
96- description: " foo "
176+ type: GraphQLNonNull ( GraphQLString)
97177 ) ,
98178 " bar " : . init(
99179 type: GraphQLString,
100- description: " bar " ,
101180 defaultValue: . string( " bar " )
102181 ) ,
103182 ]
@@ -127,7 +206,7 @@ struct TypeGeneratorTests {
127206 )
128207 }
129208
130- @Test func queryType ( ) async throws {
209+ @Test func generateRootTypeProtocolForQuery ( ) async throws {
131210 let bar = try GraphQLObjectType (
132211 name: " Bar " ,
133212 description: " bar " ,
@@ -167,7 +246,49 @@ struct TypeGeneratorTests {
167246 )
168247 }
169248
170- @Test func subscriptionType( ) async throws {
249+ @Test func generateRootTypeProtocolForMutation( ) throws {
250+ let mutationType : GraphQLObjectType = try GraphQLObjectType (
251+ name: " Mutation " ,
252+ description: " Mutations " ,
253+ fields: [
254+ " createUser " : GraphQLField (
255+ type: GraphQLString,
256+ description: " Create a new user " ,
257+ args: [
258+ " name " : GraphQLArgument ( type: GraphQLNonNull ( GraphQLString) ) ,
259+ " email " : GraphQLArgument ( type: GraphQLNonNull ( GraphQLString) ) ,
260+ ]
261+ ) ,
262+ " deleteUser " : GraphQLField (
263+ type: GraphQLBoolean,
264+ description: " Delete a user " ,
265+ args: [
266+ " id " : GraphQLArgument ( type: GraphQLNonNull ( GraphQLID) ) ,
267+ ]
268+ ) ,
269+ ]
270+ )
271+
272+ let result = try generator. generateRootTypeProtocol ( for: mutationType)
273+
274+ let expected = """
275+
276+ /// Mutations
277+ protocol MutationProtocol: Sendable {
278+ /// Create a new user
279+ static func createUser(name: String, email: String, context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String?
280+
281+ /// Delete a user
282+ static func deleteUser(id: String, context: GraphQLContext, info: GraphQLResolveInfo) async throws -> Bool?
283+
284+ }
285+ """
286+
287+ #expect( result == expected)
288+ }
289+
290+
291+ @Test func generateRootTypeProtocolForSubscription( ) async throws {
171292 let subscription = try GraphQLObjectType (
172293 name: " Subscription " ,
173294 fields: [
@@ -182,7 +303,7 @@ struct TypeGeneratorTests {
182303 ) ,
183304 ]
184305 )
185- let actual = try TypeGenerator ( ) . generateRootTypeProtocol ( for: subscription)
306+ let actual = try generator . generateRootTypeProtocol ( for: subscription)
186307 #expect(
187308 actual == """
188309
0 commit comments