Skip to content

Commit

Permalink
fix: introspection error
Browse files Browse the repository at this point in the history
  • Loading branch information
maticzav committed May 17, 2021
1 parent 0044725 commit 0bf985b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,29 @@ cd swift-graphql
make install
```

To run the generator type `swift-graphql`. If you are using any custom scalars, you should create a configuration file called `swiftgraphql.yml` and put in data-type mappings as a key-value dictionary like this. Keys should be GraphQL types, and values should be SwiftGraphQL Codecs.
### Generating the code

```
USAGE: swift-graphql <endpoint> [--config <config>] [--output <output>]
ARGUMENTS:
<endpoint> GraphQL server endpoint.
OPTIONS:
--config <config> Relative path from CWD to your YML config file.
-o, --output <output> Relative path from CWD to the output file.
-h, --help Show help information.
```

To run the generator, type `swift-graphql`. If you are using any custom scalars, you should create a configuration file called `swiftgraphql.yml` and put in data-type mappings as a key-value dictionary like this. Keys should be GraphQL types, and values should be SwiftGraphQL Codecs.

```yml
scalars:
Date: DateTime
Upload: Upload
```
You can also run `swift-graphql help` to learn more about options and how it works.
You can also run `swift-graphql --help` to learn more about options and how it works.

---

Expand Down
22 changes: 16 additions & 6 deletions Sources/GraphQLAST/Introspection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,17 @@ func fetch(from endpoint: URL, withHeaders headers: [String: String] = [:]) thro
}

request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("*/*", forHTTPHeaderField: "Accept")
request.httpMethod = "POST"

let query: [String: Any] = ["query": introspectionQuery]
let payload: [String: Any] = [
"query": introspectionQuery,
"variables": [String: Any](),
"operationName": "IntrospectionQuery"
]

request.httpBody = try! JSONSerialization.data(
withJSONObject: query,
withJSONObject: payload,
options: JSONSerialization.WritingOptions()
)

Expand All @@ -176,8 +180,14 @@ func fetch(from endpoint: URL, withHeaders headers: [String: String] = [:]) thro
return
}

guard let httpResponse = response as? HTTPURLResponse, (200 ... 299).contains(httpResponse.statusCode) else {
result = .failure(.statusCode)
guard let httpResponse = response as? HTTPURLResponse else {
result = .failure(.unknown)
semaphore.signal()
return
}

guard (200 ... 299).contains(httpResponse.statusCode) else {
result = .failure(.statusCode(httpResponse.statusCode))
semaphore.signal()
return
}
Expand Down Expand Up @@ -205,7 +215,7 @@ func fetch(from endpoint: URL, withHeaders headers: [String: String] = [:]) thro

enum IntrospectionError: Error {
case error(Error)
case statusCode
case statusCode(Int)
case unknown
}

Expand Down
6 changes: 6 additions & 0 deletions Sources/SwiftGraphQLCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ struct SwiftGraphQLCLI: ParsableCommand {

@Option(help: "Include this Authorization header in the request to the endpoint.")
var authorization: String?

// MARK: - Configuration

var configuration = CommandConfiguration(
commandName: "swift-graphql"
)

// MARK: - Main

Expand Down
15 changes: 15 additions & 0 deletions Tests/GraphQLASTTests/ASTIntrospectionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@testable import GraphQLAST
import XCTest

final class ASTIntrospectionTests: XCTestCase {
/* Schema */

func testIntrospectServer() throws {
let url = URL(string: "https://swapi-ql.herokuapp.com/graphql")!
let schema = try Schema(from: url)

/* Tests */

XCTAssertNotNil(schema)
}
}

0 comments on commit 0bf985b

Please sign in to comment.