@@ -14,79 +14,102 @@ public struct GraphQLCodegen {
14
14
}
15
15
16
16
// MARK: - Methods
17
-
18
- /// Generates a SwiftGraphQL Selection File (i.e. the code that tells how to define selections).
19
- public func generate( schema: Schema ) throws -> String {
17
+
18
+ /// Generates Swift files for the graph selections
19
+ /// - Parameters:
20
+ /// - schema: The GraphQL schema
21
+ /// - generateStaticFields: Whether to generate static selections for fields on objects
22
+ /// - singleFile: Whether to return all the swift code in a single file
23
+ /// - Returns: A list of generated files
24
+ public func generate( schema: Schema , generateStaticFields: Bool , singleFile: Bool = false ) throws -> [ GeneratedFile ] {
20
25
let context = Context ( schema: schema, scalars: self . scalars)
21
26
22
27
let subscription = schema. operations. first { $0. isSubscription } ? . type. name
23
-
24
- // Code Parts
28
+ let objects = schema. objects
25
29
let operations = schema. operations. map { $0. declaration ( ) }
26
- let objectDefinitions = try schema. objects. map { object in
27
- try object. declaration (
28
- objects: schema. objects,
29
- context: context,
30
- alias: object. name != subscription
31
- )
32
- }
33
-
34
- let staticFieldSelection = try schema. objects. map { object in
35
- try object. statics ( context: context)
36
- }
37
-
38
- let interfaceDefinitions = try schema. interfaces. map {
39
- try $0. declaration ( objects: schema. objects, context: context)
40
- }
41
-
42
- let unionDefinitions = try schema. unions. map {
43
- try $0. declaration ( objects: schema. objects, context: context)
44
- }
45
-
46
- let enumDefinitions = schema. enums. map { $0. declaration }
47
-
48
- let inputObjectDefinitions = try schema. inputObjects. map {
49
- try $0. declaration ( context: context)
50
- }
51
-
52
- // API
53
- let code = """
30
+
31
+ var files : [ GeneratedFile ] = [ ]
32
+
33
+ let header = """
54
34
// This file was auto-generated using maticzav/swift-graphql. DO NOT EDIT MANUALLY!
55
35
import Foundation
56
36
import GraphQL
57
37
import SwiftGraphQL
38
+ """
58
39
59
- // MARK: - Operations
40
+ let graphContents = """
60
41
public enum Operations {}
61
42
\( operations. lines)
62
43
63
- // MARK: - Objects
64
44
public enum Objects {}
65
- \( objectDefinitions. lines)
66
- \( staticFieldSelection. lines)
67
45
68
- // MARK: - Interfaces
69
46
public enum Interfaces {}
70
- \( interfaceDefinitions. lines)
71
47
72
- // MARK: - Unions
73
48
public enum Unions {}
74
- \( unionDefinitions. lines)
75
49
76
- // MARK: - Enums
77
50
public enum Enums {}
78
- \( enumDefinitions. lines)
79
51
80
- // MARK: - Input Objects
81
-
82
52
/// Utility pointer to InputObjects.
83
53
public typealias Inputs = InputObjects
84
54
85
55
public enum InputObjects {}
86
- \( inputObjectDefinitions. lines)
87
56
"""
88
57
89
- let formatted = try code. format ( )
90
- return formatted
58
+ func addFile( name: String , contents: String ) throws {
59
+ let fileContents : String
60
+ if singleFile {
61
+ fileContents = " \n // MARK: \( name) \n \( contents) "
62
+ } else {
63
+ fileContents = " \( header) \n \n \( contents) "
64
+ }
65
+ let file = GeneratedFile ( name: name, contents: try fileContents. format ( ) )
66
+ files. append ( file)
67
+ }
68
+
69
+ try addFile ( name: " Graph " , contents: graphContents)
70
+ for object in objects {
71
+ var contents = try object. declaration (
72
+ objects: objects,
73
+ context: context,
74
+ alias: object. name != subscription
75
+ )
76
+
77
+ if generateStaticFields {
78
+ let staticFieldSelection = try object. statics ( context: context)
79
+ contents += " \n \n \( staticFieldSelection) "
80
+ }
81
+ try addFile ( name: " Objects/ \( object. name) " , contents: contents)
82
+ }
83
+
84
+ for object in schema. inputObjects {
85
+ let contents = try object. declaration ( context: context)
86
+ try addFile ( name: " InputObjects/ \( object. name) " , contents: contents)
87
+ }
88
+
89
+ for enumSchema in schema. enums {
90
+ try addFile ( name: " Enums/ \( enumSchema. name) " , contents: enumSchema. declaration)
91
+ }
92
+
93
+ for interface in schema. interfaces {
94
+ let contents = try interface. declaration ( objects: objects, context: context)
95
+ try addFile ( name: " Interfaces/ \( interface. name) " , contents: contents)
96
+ }
97
+
98
+ for union in schema. unions {
99
+ let contents = try union. declaration ( objects: objects, context: context)
100
+ try addFile ( name: " Unions/ \( union. name) " , contents: contents)
101
+ }
102
+
103
+ if singleFile {
104
+ let fileContent = " \( header) \n \n \( files. map ( \. contents) . joined ( separator: " \n \n " ) ) "
105
+ files = [ GeneratedFile ( name: " Graph " , contents: fileContent) ]
106
+ }
107
+
108
+ return files
91
109
}
92
110
}
111
+
112
+ public struct GeneratedFile {
113
+ public let name : String
114
+ public let contents : String
115
+ }
0 commit comments