Skip to content

Commit

Permalink
config to disable static field generation
Browse files Browse the repository at this point in the history
  • Loading branch information
yonaskolb committed Apr 22, 2024
1 parent 25e8eca commit 9b3df54
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Sources/SwiftGraphQLCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct SwiftGraphQLCLI: ParsableCommand {
let files: [GeneratedFile]

do {
files = try generator.generate(schema: schema)
files = try generator.generate(schema: schema, generateStaticFields: config.generateStaticFields)
generateCodeSpinner.success("API generated successfully!")
} catch CodegenError.formatting(let err) {
generateCodeSpinner.error(err.localizedDescription)
Expand Down Expand Up @@ -180,6 +180,9 @@ struct Config: Codable, Equatable {
/// Key-Value dictionary of scalar mappings.
let scalars: [String: String]

/// Whether to generate static lookups for object fields
var generateStaticFields = true

// MARK: - Initializers

/// Creates an empty configuration instance.
Expand Down
8 changes: 5 additions & 3 deletions Sources/SwiftGraphQLCodegen/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct GraphQLCodegen {
// MARK: - Methods

/// Generates a SwiftGraphQL Selection File (i.e. the code that tells how to define selections).
public func generate(schema: Schema) throws -> String {
public func generate(schema: Schema, generateStaticFields: Bool) throws -> [GeneratedFile] {
let context = Context(schema: schema, scalars: self.scalars)

let subscription = schema.operations.first { $0.isSubscription }?.type.name
Expand Down Expand Up @@ -67,8 +67,10 @@ public struct GraphQLCodegen {
alias: object.name != subscription
)

let staticFieldSelection = try object.statics(context: context)
contents += "\n\n\(staticFieldSelection)"
if generateStaticFields {
let staticFieldSelection = try object.statics(context: context)
contents += "\n\n\(staticFieldSelection)"
}
try addFile(name: "Objects/\(object.name)", contents: contents)
}

Expand Down

0 comments on commit 9b3df54

Please sign in to comment.