Skip to content

Commit

Permalink
Codegen async/await Mega PR (#57)
Browse files Browse the repository at this point in the history
Co-authored-by: Zach FettersMoore <4425109+BobaFetters@users.noreply.github.com>
Co-authored-by: Zach FettersMoore <zach.fetters@apollographql.com>
Co-authored-by: SecOps[bot] <136828330+svc-secops@users.noreply.github.com>
Co-authored-by: renovate[bot] <svc-secops+renovate@apollographql.com>
Co-authored-by: runner <runner@Mac-1696447481311.local>
Co-authored-by: Jim Isaacs <299156+jimisaacs@users.noreply.github.com>
  • Loading branch information
7 people committed Oct 26, 2023
1 parent c2145a5 commit f08c0b5
Show file tree
Hide file tree
Showing 112 changed files with 5,098 additions and 3,846 deletions.
4 changes: 2 additions & 2 deletions .package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Quick/Nimble.git",
"state" : {
"revision" : "1f3bde57bde12f5e7b07909848c071e9b73d6edc",
"version" : "10.0.0"
"revision" : "edaedc1ec86f14ac6e2ca495b94f0ff7150d98d0",
"version" : "12.3.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let project = Project(
name: "ApolloDev",
organizationName: "apollographql",
packages: [
.package(url: "https://github.com/Quick/Nimble.git", from: "10.0.0"),
.package(url: "https://github.com/Quick/Nimble.git", from: "12.3.0"),
.package(path: "apollo-ios"),
.package(path: "apollo-ios-codegen"),
.package(path: "apollo-ios-pagination"),
Expand Down
23 changes: 23 additions & 0 deletions Tests/ApolloCodegenInternalTestHelpers/AsyncMap.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation

extension Sequence {
public func asyncMap<T>(
_ transform: (Element) async throws -> T
) async rethrows -> [T] {
var values = [T]()

for element in self {
try await values.append(transform(element))
}

return values
}

public func asyncForEach(
_ body: (Element) async throws -> Void
) async rethrows {
for element in self {
try await body(element)
}
}
}
68 changes: 0 additions & 68 deletions Tests/ApolloCodegenInternalTestHelpers/CodegenTestHelper.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ extension GraphQLJSFrontend {
schema: String,
document: String,
config: ApolloCodegen.ConfigurationContext
) throws -> CompilationResult {
let schemaSource = try makeSource(schema, filePath: "")
let documentSource = try makeSource(document, filePath: "")
) async throws -> CompilationResult {
async let schemaSource = try makeSource(schema, filePath: "")
async let documentSource = try makeSource(document, filePath: "")

let schema = try loadSchema(from: [schemaSource])
let document = try parseDocument(
let schema = try await loadSchema(from: [schemaSource])
let document = try await parseDocument(
documentSource,
experimentalClientControlledNullability: config.experimentalFeatures.clientControlledNullability
)

return try compile(
return try await compile(
schema: schema,
document: document,
validationOptions: ValidationOptions(config: config)
Expand All @@ -29,11 +29,11 @@ extension GraphQLJSFrontend {
schema: String,
document: String,
enableCCN: Bool = false
) throws -> CompilationResult {
) async throws -> CompilationResult {
let config = ApolloCodegen.ConfigurationContext(
config: .mock(experimentalFeatures: .init(clientControlledNullability: enableCCN)))

return try compile(
return try await compile(
schema: schema,
document: document,
config: config
Expand All @@ -44,20 +44,36 @@ extension GraphQLJSFrontend {
schema: String,
documents: [String],
config: ApolloCodegen.ConfigurationContext
) throws -> CompilationResult {
let schemaSource = try makeSource(schema, filePath: "")
let schema = try loadSchema(from: [schemaSource])

let documents: [GraphQLDocument] = try documents.enumerated().map {
let source = try makeSource($0.element, filePath: "Doc_\($0.offset)")
return try parseDocument(
source,
) async throws -> CompilationResult {
async let schemaSource = try makeSource(schema, filePath: "")

let sources: [GraphQLSource] = try await documents.enumerated().asyncMap {
try await makeSource($0.element, filePath: "Doc_\($0.offset)")
}

return try await compile(
schema: schemaSource,
definitions: sources,
config: config
)
}

public func compile(
schema schemaSource: GraphQLSource,
definitions: [GraphQLSource],
config: ApolloCodegen.ConfigurationContext
) async throws -> CompilationResult {
let schema = try await loadSchema(from: [schemaSource])

let documents: [GraphQLDocument] = try await definitions.asyncMap {
return try await parseDocument(
$0,
experimentalClientControlledNullability: config.experimentalFeatures.clientControlledNullability
)
}

let mergedDocument = try mergeDocuments(documents)
return try compile(
let mergedDocument = try await mergeDocuments(documents)
return try await compile(
schema: schema,
document: mergedDocument,
validationOptions: ValidationOptions(config: config)
Expand All @@ -68,11 +84,11 @@ extension GraphQLJSFrontend {
schema: String,
documents: [String],
enableCCN: Bool = false
) throws -> CompilationResult {
) async throws -> CompilationResult {
let config = ApolloCodegen.ConfigurationContext(
config: .mock(experimentalFeatures: .init(clientControlledNullability: enableCCN)))

return try compile(
return try await compile(
schema: schema,
documents: documents,
config: config
Expand All @@ -83,16 +99,16 @@ extension GraphQLJSFrontend {
schemaJSON: String,
document: String,
config: ApolloCodegen.ConfigurationContext
) throws -> CompilationResult {
let documentSource = try makeSource(document, filePath: "")
let schemaSource = try makeSource(schemaJSON, filePath: "schema.json")
let schema = try loadSchema(from: [schemaSource])
let document = try parseDocument(
) async throws -> CompilationResult {
async let documentSource = try makeSource(document, filePath: "")
async let schemaSource = try makeSource(schemaJSON, filePath: "schema.json")

let schema = try await loadSchema(from: [schemaSource])
let document = try await parseDocument(
documentSource,
experimentalClientControlledNullability: config.experimentalFeatures.clientControlledNullability)

return try compile(
return try await compile(
schema: schema,
document: document,
validationOptions: ValidationOptions(config: config)
Expand All @@ -103,11 +119,11 @@ extension GraphQLJSFrontend {
schemaJSON: String,
document: String,
enableCCN: Bool = false
) throws -> CompilationResult {
) async throws -> CompilationResult {
let config = ApolloCodegen.ConfigurationContext(
config: .mock(experimentalFeatures: .init(clientControlledNullability: enableCCN)))

return try compile(
return try await compile(
schemaJSON: schemaJSON,
document: document,
config: config
Expand Down
30 changes: 15 additions & 15 deletions Tests/ApolloCodegenInternalTestHelpers/IR+Mocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ extension IRBuilder {
schema: String,
document: String,
enableCCN: Bool = false
) throws -> IRBuilder {
let frontend = try GraphQLJSFrontend()
let compilationResult = try frontend.compile(
) async throws -> IRBuilder {
let frontend = try await GraphQLJSFrontend()
let compilationResult = try await frontend.compile(
schema: schema,
document: document,
enableCCN: enableCCN
Expand All @@ -24,9 +24,9 @@ extension IRBuilder {
schema: String,
documents: [String],
enableCCN: Bool = false
) throws -> IRBuilder {
let frontend = try GraphQLJSFrontend()
let compilationResult = try frontend.compile(
) async throws -> IRBuilder {
let frontend = try await GraphQLJSFrontend()
let compilationResult = try await frontend.compile(
schema: schema,
documents: documents,
enableCCN: enableCCN
Expand All @@ -38,18 +38,17 @@ extension IRBuilder {
schemaJSON: String,
document: String,
enableCCN: Bool = false
) throws -> IRBuilder {
let frontend = try GraphQLJSFrontend()
let compilationResult = try frontend.compile(
) async throws -> IRBuilder {
let frontend = try await GraphQLJSFrontend()
let compilationResult = try await frontend.compile(
schemaJSON: schemaJSON,
document: document,
enableCCN: enableCCN
)
return .mock(compilationResult: compilationResult)
}

public static func mock(
schemaNamespace: String = "TestSchema",
public static func mock(
compilationResult: CompilationResult
) -> IRBuilder {
return IRBuilder(compilationResult: compilationResult)
Expand All @@ -62,7 +61,7 @@ extension IR.NamedFragment {
public static func mock(
_ name: String,
type: GraphQLCompositeType = .mock("MockType"),
source: String? = nil
source: String = ""
) -> IR.NamedFragment {
let definition = CompilationResult.FragmentDefinition.mock(name, type: type, source: source)
let rootField = CompilationResult.Field.mock(name, type: .entity(type))
Expand All @@ -79,7 +78,7 @@ extension IR.NamedFragment {
.descriptor(
forType: type,
inclusionConditions: nil,
givenAllTypesInSchema: .init([type])
givenAllTypesInSchema: .init([type], schemaRootTypes: .mock())
))))

return IR.NamedFragment(
Expand Down Expand Up @@ -111,7 +110,7 @@ extension IR.Operation {
scopePath: [.descriptor(
forType: .mock(),
inclusionConditions: nil,
givenAllTypesInSchema: .init([]))
givenAllTypesInSchema: .init([], schemaRootTypes: .mock()))
])
),
referencedFragments: referencedFragments
Expand All @@ -127,7 +126,8 @@ extension IR.Operation {
let definition = CompilationResult.OperationDefinition.mock(
name: name,
type: type,
source: source
source: source,
referencedFragments: referencedFragments.map(\.definition)
)

return IR.Operation.mock(definition: definition, referencedFragments: referencedFragments)
Expand Down

0 comments on commit f08c0b5

Please sign in to comment.