Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating appropriate default values for __typename; #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/libAutoGraphCodeGen/FragmentCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension FragmentDefinitionIR {
let nextIndentation = indentation + " "
let typeDefinition = self.structDeclarationStart(indentation: indentation)
let initializer = try self.initializer(indentation: nextIndentation, outputSchemaName: outputSchemaName)
let scalarPropertyDefinitions = try self.selectionSet.genScalarPropertyVariableDeclarations(indentation: nextIndentation, schemaName: outputSchemaName)
let scalarPropertyDefinitions = try self.selectionSet.genScalarPropertyVariableDeclarations(indentation: nextIndentation, schemaName: outputSchemaName, parentFieldBaseTypeName: self.selectionSet.typeInformation.graphQLTypeName.value)
let fragmentSpreadPropertyDefinitions = try self.selectionSet.genFragmentSpreadPropertyVariableDeclarations(indentation: nextIndentation, schemaName: outputSchemaName)
let (objectSubStructPropertyDefinitions, objectSubStructDefinitions) =
try self.selectionSet.genObjectNestedStructDeclarations(indentation: nextIndentation, schemaName: outputSchemaName)
Expand Down
2 changes: 1 addition & 1 deletion Sources/libAutoGraphCodeGen/OperationCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extension OperationDefinitionIR {
let nextIndentation = indentation + " "
let nextNextIndentation = nextIndentation + " "
let initializerAndInputVariableProperties = try self.genInitializerAndInputVariablePropertyDeclarations(indentation: nextIndentation)
let scalarPropertyDefinitions = try self.selectionSet.genScalarPropertyVariableDeclarations(indentation: nextNextIndentation, schemaName: outputSchemaName, omit__typename: true)
let scalarPropertyDefinitions = try self.selectionSet.genScalarPropertyVariableDeclarations(indentation: nextNextIndentation, schemaName: outputSchemaName, parentFieldBaseTypeName: self.typeName, omit__typename: true)
let fragmentSpreadPropertyDefinitions = try self.selectionSet.genFragmentSpreadPropertyVariableDeclarations(indentation: nextNextIndentation, schemaName: outputSchemaName)
let (objectSubStructPropertyDefinitions, objectSubStructDefinitions) =
try self.selectionSet.genObjectNestedStructDeclarations(indentation: nextIndentation, schemaName: outputSchemaName)
Expand Down
2 changes: 1 addition & 1 deletion Sources/libAutoGraphCodeGen/Pipeline/Pipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public func codeGen(configuration: Configuration) throws {

\(optionalInputValueCode)

\(variableInputParamterCode)
\(variableInputParamterCode)\n
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is a multi-line literal, can simply add a real newline and it should work. also noticing variableInputParamterCode is spelled wrong 😬

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#8

try code.write(toFile: path, atomically: false, encoding: .utf8)
}
Expand Down
23 changes: 17 additions & 6 deletions Sources/libAutoGraphCodeGen/SelectionSetCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ extension SelectionSetIR {
// MARK: - __typename-injection:
let orderedScalarFields = self.scalarFields.ordered(omitting__typename: omit__typename)
let scalarParameters = orderedScalarFields.map {
genFunctionParameter(name: $0.swiftVariableIdentifierName, type: $0.swiftVariableTypeIdentifier(schemaName: schemaName))
let name = $0.swiftVariableIdentifierName
if name == "__typename", let typename = parentFieldBaseTypeName {
return "\(name): String = \"\(typename)\""
}
else {
return genFunctionParameter(name: name, type: $0.swiftVariableTypeIdentifier(schemaName: schemaName))
}
}

let orderedObjectFields = self.objectFields.ordered()
Expand Down Expand Up @@ -180,10 +186,15 @@ extension SelectionSetIR {
return everything.joined(separator: "\n")
}

public func genScalarPropertyVariableDeclarations(indentation: String, schemaName: String, omit__typename: Bool = false) throws -> String {
public func genScalarPropertyVariableDeclarations(indentation: String, schemaName: String, parentFieldBaseTypeName: String, omit__typename: Bool = false) throws -> String {
// MARK: - __typename-injection:
let orderedScalarFields = self.scalarFields.ordered(omitting__typename: omit__typename)
let fields = try orderedScalarFields.map { try $0.genVariableDeclaration(indentation: indentation, schemaName: schemaName) }
let fields = try orderedScalarFields.map {
if $0.swiftVariableIdentifierName == "__typename" {
return "\(indentation)public private(set) var \($0.swiftVariableIdentifierName): String = \"\(parentFieldBaseTypeName)\""
}
return try $0.genVariableDeclaration(indentation: indentation, schemaName: schemaName)
}
return fields.joined(separator: "\n")
}

Expand Down Expand Up @@ -251,7 +262,7 @@ extension SelectionSetIR {
let typeDefinition = "\(indentation)public struct \(baseTypeName): Codable {"
let nextIndentation = indentation + " "
let initializer = try self.genInitializerDeclaration(indentation: nextIndentation, schemaName: schemaName, parentFieldBaseTypeName: baseTypeName)
let innerCode = try self.genPropertyAndNestedStructDeclarations(indentation: nextIndentation, schemaName: schemaName)
let innerCode = try self.genPropertyAndNestedStructDeclarations(indentation: nextIndentation, schemaName: schemaName, parentFieldBaseTypeName: baseTypeName)
return """
\(typeDefinition)
\(initializer)
Expand All @@ -278,8 +289,8 @@ extension SelectionSetIR {
"""
}

public func genPropertyAndNestedStructDeclarations(indentation: String, schemaName: String) throws -> String {
let scalarPropertyVariableDeclarations = try self.genScalarPropertyVariableDeclarations(indentation: indentation, schemaName: schemaName)
public func genPropertyAndNestedStructDeclarations(indentation: String, schemaName: String, parentFieldBaseTypeName: String) throws -> String {
let scalarPropertyVariableDeclarations = try self.genScalarPropertyVariableDeclarations(indentation: indentation, schemaName: schemaName, parentFieldBaseTypeName: parentFieldBaseTypeName)
let fragmentSpreadPropertyDefinitions = try self.genFragmentSpreadPropertyVariableDeclarations(indentation: indentation, schemaName: schemaName)
let (objectSubStructPropertyDefinitions, objectSubStructDefinitions) =
try self.genObjectNestedStructDeclarations(indentation: indentation, schemaName: schemaName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,4 @@ extension Array: VariableInputParameterEncodable {
}
return encoded
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public struct ExampleAniListWithEnumFieldQuery: AutoGraphQLRequest {
}

public struct Page: Codable {
public init(__typename: String = String(), media: [Page.Media?]? = nil) {
public init(__typename: String = "Page", media: [Page.Media?]? = nil) {
self.__typename = __typename
self.media = media
}
Expand All @@ -112,12 +112,12 @@ public struct ExampleAniListWithEnumFieldQuery: AutoGraphQLRequest {
self.media = try values.decode([Page.Media?]?.self, forKey: .media)
}

public private(set) var __typename: String = String()
public private(set) var __typename: String = "Page"

public private(set) var media: [Media?]? = nil

public struct Media: Codable {
public init(__typename: String = String(), description: String? = nil, siteUrl: String? = nil, type: AniListGQLSchema.MediaType? = nil, title: Media.MediaTitle? = nil) {
public init(__typename: String = "Media", description: String? = nil, siteUrl: String? = nil, type: AniListGQLSchema.MediaType? = nil, title: Media.MediaTitle? = nil) {
self.__typename = __typename
self.description = description
self.siteUrl = siteUrl
Expand All @@ -135,15 +135,15 @@ public struct ExampleAniListWithEnumFieldQuery: AutoGraphQLRequest {
self.title = try values.decode(Media.MediaTitle?.self, forKey: .title)
}

public private(set) var __typename: String = String()
public private(set) var __typename: String = "Media"
public private(set) var description: String? = nil
public private(set) var siteUrl: String? = nil
public private(set) var type: AniListGQLSchema.MediaType? = nil

public private(set) var title: MediaTitle? = nil

public struct MediaTitle: Codable {
public init(__typename: String = String(), english: String? = nil, native: String? = nil) {
public init(__typename: String = "MediaTitle", english: String? = nil, native: String? = nil) {
self.__typename = __typename
self.english = english
self.native = native
Expand All @@ -157,7 +157,7 @@ public struct ExampleAniListWithEnumFieldQuery: AutoGraphQLRequest {
self.native = try values.decode(String?.self, forKey: .native)
}

public private(set) var __typename: String = String()
public private(set) var __typename: String = "MediaTitle"
public private(set) var english: String? = nil
public private(set) var native: String? = nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public struct ExamplePokemonQuery: AutoGraphQLRequest {
}

public struct pokemon_v2_ability: Codable {
public init(__typename: String = String(), generation_id: Int? = nil, id: Int = Int(), is_main_series: Bool = Bool(), name: String = String(), pokemon_v2_abilitychanges: [pokemon_v2_ability.pokemon_v2_abilitychange] = []) {
public init(__typename: String = "pokemon_v2_ability", generation_id: Int? = nil, id: Int = Int(), is_main_series: Bool = Bool(), name: String = String(), pokemon_v2_abilitychanges: [pokemon_v2_ability.pokemon_v2_abilitychange] = []) {
self.__typename = __typename
self.generation_id = generation_id
self.id = id
Expand All @@ -91,7 +91,7 @@ public struct ExamplePokemonQuery: AutoGraphQLRequest {
self.pokemon_v2_abilitychanges = try values.decode([pokemon_v2_ability.pokemon_v2_abilitychange].self, forKey: .pokemon_v2_abilitychanges)
}

public private(set) var __typename: String = String()
public private(set) var __typename: String = "pokemon_v2_ability"
public private(set) var generation_id: Int? = nil
public private(set) var id: Int = Int()
public private(set) var is_main_series: Bool = Bool()
Expand All @@ -100,7 +100,7 @@ public struct ExamplePokemonQuery: AutoGraphQLRequest {
public private(set) var pokemon_v2_abilitychanges: [pokemon_v2_abilitychange] = []

public struct pokemon_v2_abilitychange: Codable {
public init(__typename: String = String(), ability_id: Int? = nil, pokemon_v2_ability: pokemon_v2_abilitychange.pokemon_v2_ability? = nil) {
public init(__typename: String = "pokemon_v2_abilitychange", ability_id: Int? = nil, pokemon_v2_ability: pokemon_v2_abilitychange.pokemon_v2_ability? = nil) {
self.__typename = __typename
self.ability_id = ability_id
self.pokemon_v2_ability = pokemon_v2_ability
Expand All @@ -114,13 +114,13 @@ public struct ExamplePokemonQuery: AutoGraphQLRequest {
self.pokemon_v2_ability = try values.decode(pokemon_v2_abilitychange.pokemon_v2_ability?.self, forKey: .pokemon_v2_ability)
}

public private(set) var __typename: String = String()
public private(set) var __typename: String = "pokemon_v2_abilitychange"
public private(set) var ability_id: Int? = nil

public private(set) var pokemon_v2_ability: pokemon_v2_ability? = nil

public struct pokemon_v2_ability: Codable {
public init(__typename: String = String(), is_main_series: Bool = Bool(), name: String = String(), pokemon_v2_abilityflavortexts_aggregate: pokemon_v2_ability.pokemon_v2_abilityflavortext_aggregate = pokemon_v2_ability.pokemon_v2_abilityflavortext_aggregate()) {
public init(__typename: String = "pokemon_v2_ability", is_main_series: Bool = Bool(), name: String = String(), pokemon_v2_abilityflavortexts_aggregate: pokemon_v2_ability.pokemon_v2_abilityflavortext_aggregate = pokemon_v2_ability.pokemon_v2_abilityflavortext_aggregate()) {
self.__typename = __typename
self.is_main_series = is_main_series
self.name = name
Expand All @@ -136,14 +136,14 @@ public struct ExamplePokemonQuery: AutoGraphQLRequest {
self.pokemon_v2_abilityflavortexts_aggregate = try values.decode(pokemon_v2_ability.pokemon_v2_abilityflavortext_aggregate.self, forKey: .pokemon_v2_abilityflavortexts_aggregate)
}

public private(set) var __typename: String = String()
public private(set) var __typename: String = "pokemon_v2_ability"
public private(set) var is_main_series: Bool = Bool()
public private(set) var name: String = String()

public private(set) var pokemon_v2_abilityflavortexts_aggregate: pokemon_v2_abilityflavortext_aggregate = pokemon_v2_abilityflavortext_aggregate()

public struct pokemon_v2_abilityflavortext_aggregate: Codable {
public init(__typename: String = String(), aggregate: pokemon_v2_abilityflavortext_aggregate.pokemon_v2_abilityflavortext_aggregate_fields? = nil) {
public init(__typename: String = "pokemon_v2_abilityflavortext_aggregate", aggregate: pokemon_v2_abilityflavortext_aggregate.pokemon_v2_abilityflavortext_aggregate_fields? = nil) {
self.__typename = __typename
self.aggregate = aggregate
}
Expand All @@ -155,12 +155,12 @@ public struct ExamplePokemonQuery: AutoGraphQLRequest {
self.aggregate = try values.decode(pokemon_v2_abilityflavortext_aggregate.pokemon_v2_abilityflavortext_aggregate_fields?.self, forKey: .aggregate)
}

public private(set) var __typename: String = String()
public private(set) var __typename: String = "pokemon_v2_abilityflavortext_aggregate"

public private(set) var aggregate: pokemon_v2_abilityflavortext_aggregate_fields? = nil

public struct pokemon_v2_abilityflavortext_aggregate_fields: Codable {
public init(__typename: String = String(), count: Int = Int()) {
public init(__typename: String = "pokemon_v2_abilityflavortext_aggregate_fields", count: Int = Int()) {
self.__typename = __typename
self.count = count
}
Expand All @@ -172,7 +172,7 @@ public struct ExamplePokemonQuery: AutoGraphQLRequest {
self.count = try values.decode(Int.self, forKey: .count)
}

public private(set) var __typename: String = String()
public private(set) var __typename: String = "pokemon_v2_abilityflavortext_aggregate_fields"
public private(set) var count: Int = Int()
}
}
Expand Down
Loading