-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recfactored to add InputValueRenderable
Finished implementation of operation variable generation
- Loading branch information
1 parent
a4fdcef
commit d3dd12e
Showing
9 changed files
with
465 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
Sources/ApolloCodegenLib/Templates/RenderingHelpers/GraphQLInputValue+Rendered.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// GraphQLInputValue+Rendered.swift | ||
// ApolloCodegenLib | ||
// | ||
// Created by Anthony Miller on 2/15/22. | ||
// Copyright © 2022 Apollo GraphQL. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol InputValueRenderable { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
Sources/ApolloCodegenLib/Templates/RenderingHelpers/InputValueRenderable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import JavaScriptCore | ||
|
||
protocol InputValueRenderable { | ||
var name: String { get } | ||
var type: GraphQLType { get } | ||
var hasDefaultValue: Bool { get } | ||
} | ||
|
||
extension InputValueRenderable { | ||
func renderInputValueType(includeDefault: Bool = false) -> String { | ||
"\(type.renderAsInputValue())\(isSwiftOptional ? "?" : "")\(includeDefault && hasSwiftNilDefault ? " = nil" : "")" | ||
} | ||
|
||
private var isSwiftOptional: Bool { | ||
!isNullable && hasDefaultValue | ||
} | ||
|
||
private var hasSwiftNilDefault: Bool { | ||
isNullable && !hasDefaultValue | ||
} | ||
|
||
var isNullable: Bool { | ||
switch type { | ||
case .nonNull: return false | ||
default: return true | ||
} | ||
} | ||
} | ||
|
||
extension GraphQLInputField: InputValueRenderable { | ||
var hasDefaultValue: Bool { | ||
switch defaultValue { | ||
case .none, .some(nil): | ||
return false | ||
case let .some(value): | ||
guard let value = value as? JSValue else { | ||
fatalError("Cannot determine default value for Input field: \(self)") | ||
} | ||
|
||
return !value.isUndefined | ||
} | ||
} | ||
} | ||
|
||
extension CompilationResult.VariableDefinition: InputValueRenderable { | ||
var hasDefaultValue: Bool { | ||
switch defaultValue { | ||
case .none, .some(nil), .some(.null): return false | ||
default: return true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.