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

fix: Throw error when no schema nor operation files are found #2618

Merged
merged 4 commits into from
Oct 28, 2022
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
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ commands:
command: pod install --verbose
name: CocoaPods - Install
- run:
working_directory: Tests/CodegenCLITests/pod-install-test/Pods/Apollo/
command: ./apollo-ios-cli init --schema-name NewTestSchema
working_directory: Tests/CodegenCLITests/pod-install-test/
command: ./Pods/Apollo/apollo-ios-cli init --schema-name NewTestSchema
name: CocoaPods - CLI Test (init)
- run:
working_directory: Tests/CodegenCLITests/pod-install-test/Pods/Apollo/
command: ./apollo-ios-cli generate
working_directory: Tests/CodegenCLITests/pod-install-test/
command: ./Pods/Apollo/apollo-ios-cli generate
name: CocoaPods - CLI Test (generate)
swiftpm_plugin_test:
steps:
Expand Down
28 changes: 21 additions & 7 deletions Sources/ApolloCodegenLib/ApolloCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class ApolloCodegen {
case testMocksInvalidSwiftPackageConfiguration
case inputSearchPathInvalid(path: String)
case schemaNameConflict(name: String)
case cannotLoadSchema
case cannotLoadOperations

public var errorDescription: String? {
switch self {
Expand All @@ -27,6 +29,10 @@ public class ApolloCodegen {
return "Input search path '\(path)' is invalid. Input search paths must include a file extension component. (eg. '.graphql')"
case let .schemaNameConflict(name):
return "Schema name \(name) conflicts with name of a type in your GraphQL schema. Please choose a different schema name. Suggestions: \(name)Schema, \(name)GraphQL, \(name)API"
case .cannotLoadSchema:
return "A GraphQL schema could not be found. Please verify the schema search paths."
case .cannotLoadOperations:
return "No GraphQL operations could be found. Please verify the operation search paths."
}
}
}
Expand Down Expand Up @@ -56,12 +62,14 @@ public class ApolloCodegen {
rootURL: rootURL
)

try validate(config: configContext)
Copy link
Member Author

@calvincestari calvincestari Oct 27, 2022

Choose a reason for hiding this comment

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

I've separated these two validate calls because the first one includes path syntax validation and should be done before compilation of the schema + operations. The second validation (line 72) ensures no schema name conflict.

I don't like that they're now separate, some ideas to improve:

  • rename the second one from validate to something else
  • move the schema name logic somewhere else


let compilationResult = try compileGraphQLResult(
configContext,
experimentalFeatures: configuration.experimentalFeatures
)

try validate(config: configContext, compilationResult: compilationResult)
try validate(schemaName: configContext.schemaName, compilationResult: compilationResult)

let ir = IR(
schemaName: configContext.schemaName,
Expand Down Expand Up @@ -112,7 +120,7 @@ public class ApolloCodegen {
}

/// Performs validation against deterministic errors that will cause code generation to fail.
static func validate(config: ConfigurationContext, compilationResult: CompilationResult) throws {
static func validate(config: ConfigurationContext) throws {
if case .swiftPackage = config.output.testMocks,
config.output.schemaTypes.moduleType != .swiftPackageManager {
throw Error.testMocksInvalidSwiftPackageConfiguration
Expand All @@ -124,8 +132,6 @@ public class ApolloCodegen {
for searchPath in config.input.operationSearchPaths {
try validate(inputSearchPath: searchPath)
}

try validate(schemaName: config.schemaName, compilationResult: compilationResult)
}

static private func validate(inputSearchPath: String) throws {
Expand All @@ -134,7 +140,7 @@ public class ApolloCodegen {
}
}

static private func validate(schemaName: String, compilationResult: CompilationResult) throws {
static func validate(schemaName: String, compilationResult: CompilationResult) throws {
guard
!compilationResult.referencedTypes.contains(where: { namedType in
namedType.swiftName == schemaName.firstUppercased
Expand All @@ -153,10 +159,8 @@ public class ApolloCodegen {
experimentalFeatures: ApolloCodegenConfiguration.ExperimentalFeatures = .init()
) throws -> CompilationResult {
let frontend = try GraphQLJSFrontend()

let graphQLSchema = try createSchema(config, frontend)
let operationsDocument = try createOperationsDocument(config, frontend, experimentalFeatures)

let validationOptions = ValidationOptions(config: config)

let graphqlErrors = try frontend.validateDocument(
Expand Down Expand Up @@ -184,6 +188,11 @@ public class ApolloCodegen {
_ frontend: GraphQLJSFrontend
) throws -> GraphQLSchema {
let matches = try Glob(config.input.schemaSearchPaths, relativeTo: config.rootURL).match()

guard !matches.isEmpty else {
throw Error.cannotLoadSchema
}

let sources = try matches.map { try frontend.makeSource(from: URL(fileURLWithPath: $0)) }
return try frontend.loadSchema(from: sources)
}
Expand All @@ -194,6 +203,11 @@ public class ApolloCodegen {
_ experimentalFeatures: ApolloCodegenConfiguration.ExperimentalFeatures
) throws -> GraphQLDocument {
let matches = try Glob(config.input.operationSearchPaths, relativeTo: config.rootURL).match()

guard !matches.isEmpty else {
throw Error.cannotLoadOperations
}

let documents = try matches.map({ path in
return try frontend.parseDocument(
from: URL(fileURLWithPath: path),
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApolloCodegenLib/Glob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public struct Glob {
globfree(&globT)
}

CodegenLogger.log("Matching \(pattern)", logLevel: .debug)
CodegenLogger.log("Evaluating \(pattern)", logLevel: .debug)
Copy link
Member Author

Choose a reason for hiding this comment

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

Matching was too close to Matched. Evaluating is more indicative of what the logic is actually doing.


let response = glob(pattern, flags, nil, &globT)

Expand Down
Loading