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

Implement custom scalar prompt #6

Merged
merged 1 commit into from
Jun 8, 2024
Merged
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
30 changes: 26 additions & 4 deletions Sources/GraphQLCodeGen/codegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SwiftSyntax
import SwiftSyntaxBuilder
import GraphQLParser

enum CodegenErrors: Error {
public enum CodegenErrors: Error {
case missingQueryTypeName
case missingQueryType

Expand All @@ -14,7 +14,7 @@ enum CodegenErrors: Error {
case TODO(String)
}

public class Context {
class Context {
let serverUrl: String
let schema: __Schema
private let documents: [DocumentNode]
Expand All @@ -40,16 +40,21 @@ public class Context {
}

var visitedTypes: Set<String> = Set()

// data used for stdout
var requiredCustomScalars: [String: (desc: String?, specifiedByURL: String?)] = [:]
}

private func generateVisitedTypes(ctx: Context) throws -> [DeclSyntaxProtocol] {
func generateVisitedTypes(ctx: Context) throws -> [DeclSyntaxProtocol] {
var decls: [DeclSyntaxProtocol] = []
while let tpName = ctx.visitedTypes.popFirst() {
if (isGraphQLBuiltInScalarType(str: tpName)) { continue }
guard let tp = ctx.schema.types.first(where: { $0.name == tpName }) else {
throw CodegenErrors.invalidType(tpName)
}
switch tp.kind {
case .SCALAR:
if (isGraphQLBuiltInScalarType(str: tpName)) { continue }
ctx.requiredCustomScalars[tpName] = (desc: tp.description, specifiedByURL: tp.specifiedByURL)
case .INPUT_OBJECT:
for field in tp.inputFields ?? [] {
let innerTp = try getWrappedType(ctx: ctx, type: field.type)
Expand Down Expand Up @@ -218,5 +223,22 @@ public func generate(serverUrl: String, schema: __Schema, documents: [DocumentNo
)
}.formatted().description
content += "\n"

let requiredCustomScalars = ctx.requiredCustomScalars.enumerated()
.sorted(by: { $0.element.key < $1.element.key })
.map { $0.element }
if (requiredCustomScalars.count > 0) {
print("Please implement custom scalars:")
for (name, (desc, specifiedByURL)) in requiredCustomScalars {
print("- \(name)")
if let desc {
print(" - description: \(desc)")
}
if let specifiedByURL {
print(" - specifiedByURL: \(specifiedByURL)")
}
}
}

return content
}
6 changes: 3 additions & 3 deletions Sources/GraphQLCodeGen/generator-primitives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func convertSchemaTypeToSwiftType(ctx: Context, type: __Type, nonNull: Bool = fa
let tp: TypeSyntaxProtocol
switch type.kind {
case .SCALAR:
ctx.visitedTypes.insert(type.name!)
if let scalarType = GraphQLBuiltInScalarType(rawValue: type.name!) {
tp = IdentifierTypeSyntax(name: TokenSyntax.identifier(scalarType.swiftType))
} else {
// TODO: Support custom scalars
// https://spec.graphql.org/October2021/#sec-Scalars.Custom-Scalars
throw CodegenErrors.TODO("support convertSchemaTypeToSwiftType for custom scalars")
tp = IdentifierTypeSyntax(name: TokenSyntax.identifier(type.name!))
}
case .OBJECT:
ctx.visitedTypes.insert(type.name!)
Expand Down Expand Up @@ -65,7 +65,7 @@ func getWrappedType(ctx: Context, type: __Type) throws -> __Type {
case .SCALAR:
return type
case .OBJECT:
guard let name = type.name else { throw CodegenErrors.invalidType("Expect name for OBJECT type") }
guard let name: String = type.name else { throw CodegenErrors.invalidType("Expect name for OBJECT type") }
return ctx.schema.types.first(where: { $0.name == name })!
case .INTERFACE:
return type
Expand Down