Skip to content

Commit

Permalink
Merge pull request #70 from d-exclaimation/reorganize-and-seperate
Browse files Browse the repository at this point in the history
Reorganize Code and General Utilities Improvement
  • Loading branch information
d-exclaimation authored Jul 12, 2022
2 parents 926a85b + 174e769 commit ee1a0fe
Show file tree
Hide file tree
Showing 18 changed files with 1,204 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Documentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ You can add Pioneer into any existing Vapor application with any GraphQL schema
Add this line to add Pioneer as one of your dependencies.

```swift
.package(url: "https://github.com/d-exclaimation/pioneer", from: "0.8.5")
.package(url: "https://github.com/d-exclaimation/pioneer", from: "0.8.6")
```

Go to the `main.swift` or any Swift file where you apply your Vapor routing like your `routes.swift` file.
Expand Down
259 changes: 259 additions & 0 deletions Documentation/features/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
---
icon: gear
order: 50
---

# Configuration

!!!success Custom Configuration options
From `0.8.6`, Pioneer brought in a structure that will allow easier configuration, which would only require you to pass in the config object into [Pioneer](/references/pioneer) initializer.
!!!

This configuration structure would allow user of the library to create multiple configuration for Pioneer on different environment or situation.

## Config

The Config object takes in all the parameters required to initialized a [Pioneer](/references/pioneer) server instance.

```swift
let server = Pioneer(
.init(...)
)
```

_You are still allowed to directly passed in the required parameters for [Pioneer](/references/pioneer) into its initializer without the use of configs._

## Built-in configs

Pioneer also brought in custom built-in configuration for different options for a GraphQL server.

### `.default`

A "default" configuration that is _recommended_ for most users

=== Example

```swift
let server = Pioneer(
.default(
using: schema,
resolver: .init(),
context: { req, res in
Context(req, res)
},
websocketContext: { req, payload, gql in
Context(req, payload, gql)
}
)
)
```

===

==- Configurations

| Name | Selected |
| ------------------- | ----------------------------------------------------------- |
| `httpStrategy` | [!badge variant="primary" text=".queryOnlyGet"] |
| `websocketProtocol` | [!badge variant="success" text=".graphqlWs"] |
| `playground` | [!badge variant="primary" text=".redirect(.apolloSandbox)"] |
| `keepAlive` | [!badge variant="success" text="12_500_000"] |

===

### `.secured`

A secured "default" configuration that is also _recommended_ for many users

=== Example

```swift
let server = Pioneer(
.secured(
using: schema,
resolver: .init(),
context: { req, res in
Context(req, res)
},
websocketContext: { req, payload, gql in
Context(req, payload, gql)
}
)
)
```

===

==- Configurations

| Name | Selected |
| ------------------- | ----------------------------------------------------------- |
| `httpStrategy` | [!badge variant="primary" text=".csrfPrevention"] |
| `websocketProtocol` | [!badge variant="success" text=".graphqlWs"] |
| `playground` | [!badge variant="primary" text=".redirect(.apolloSandbox)"] |
| `keepAlive` | [!badge variant="success" text="12_500_000"] |

===

### `.detect`

A configuration that detect from Environment variables

=== Example

```swift
let server = try Pioneer(
.detect(
using: schema,
resolver: .init(),
context: { req, res in
Context(req, res)
},
websocketContext: { req, payload, gql in
Context(req, payload, gql)
}
)
)
```

===

==- Configurations

| Name | Environment variables |
| ------------------- | ---------------------------- |
| `httpStrategy` | `PIONEER_HTTP_STRATEGY` |
| `websocketProtocol` | `PIONEER_WEBSOCKET_PROTOCOL` |
| `playground` | `PIONEER_PLAYGROUND` |
| `introspection` | `PIONEER_INTROSPECTION` |
| `keepAlive` | `PIONEER_KEEP_ALIVE` |

<sub> Check [DocC](https://swiftpackageindex.com/d-exclaimation/pioneer/main/documentation/pioneer) for more clarification </sub>

===

### `.httpOnly`

A configuration for a HTTP only GraphQL server

=== Example

```swift
let server = Pioneer(
.httpOnly(
using: schema,
resolver: .init(),
context: { req, res in
Context(req, res)
},
httpStrategy: .csrfPrevention,
playground: .graphiql
)
)
```

===

==- Configurations

| Name | Selected |
| ------------------- | -------------------------------------------- |
| `websocketProtocol` | [!badge variant="danger" text=".disable"] |
| `keepAlive` | [!badge variant="success" text="12_500_000"] |

===

### `.simpleHttpOnly`

A simpler configuration for a HTTP only GraphQL server

=== Example

```swift
let server = Pioneer(
.simpleHttpOnly(
using: schema,
resolver: .init(),
context: { req, res in
Context(req, res)
}
)
)
```

===

==- Configurations

| Name | Selected |
| ------------------- | ----------------------------------------------- |
| `httpStrategy` | [!badge variant="primary" text=".queryOnlyGet"] |
| `websocketProtocol` | [!badge variant="danger" text=".disable"] |
| `keepAlive` | [!badge variant="success" text="12_500_000"] |

===

### `.wsOnly`

A configuration for a WebSocket\* only GraphQL server

_\*Introspection through HTTP is still allowed_

=== Example

```swift
let server = Pioneer(
.wsOnly(
using: schema,
resolver: .init(),
context: { req, payload, gql in
Context(req, payload, gql)
},
websocketProtocol: .graphqlWs,
playground: .graphiql
)
)
```

===

==- Configurations

| Name | Selected |
| -------------- | -------------------------------------------- |
| `httpStrategy` | [!badge variant="warning" text=".onlyPost"] |
| `keepAlive` | [!badge variant="success" text="12_500_000"] |

===

### `.simpleWsOnly`

A simpler configuration for a WebSocket\* only GraphQL server

_\*Introspection through HTTP is still allowed_

=== Example

```swift
let server = Pioneer(
.simpleWsOnly(
using: schema,
resolver: .init(),
context: { req, payload, gql in
Context(req, payload, gql)
}
)
)
```

===

==- Configurations

| Name | Selected |
| ------------------- | -------------------------------------------- |
| `httpStrategy` | [!badge variant="warning" text=".onlyPost"] |
| `websocketProtocol` | [!badge variant="success" text=".graphqlWs"] |
| `keepAlive` | [!badge variant="success" text="12_500_000"] |

===
2 changes: 1 addition & 1 deletion Documentation/guides/getting-started/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/GraphQLSwift/Graphiti.git", from: "1.0.0"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.61.1"),
.package(url: "https://github.com/d-exclaimation/pioneer", from: "0.8.5")
.package(url: "https://github.com/d-exclaimation/pioneer", from: "0.8.6")
],
targets: [
.target(
Expand Down
28 changes: 28 additions & 0 deletions Documentation/references/structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ let id = ID("any-string")

===

### `init` (UUID)

Returns a new [ID](#id) from an UUID.

=== Example

```swift
let id = ID(UUID())
```

===

==- Options

| Name | Type | Description |
| ---- | -------------------------------------- | -------------------------------------- |
| \_ | [!badge variant="primary" text="UUID"] | UUID value to which the ID is built on |

===

### `description`

Returns the string value to satify `CustomStringConvertible` protocol.
Expand All @@ -57,6 +77,14 @@ Returns the length of the string value from this [ID](#id).

A getter for the string value.

### `uuid`

A getter for the UUID value of this ID if possible

### `toUUID`

Get a UUID from this ID scalar if possible, otherwise throw an Error

### `uuid` (static)

A static initializer function to create a new [ID](#id) from a newly generated UUID.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pioneer is an open-source Swift GraphQL server for [Vapor](https://github.com/va
## Setup

```swift
.package(url: "https://github.com/d-exclaimation/pioneer", from: "0.8.5")
.package(url: "https://github.com/d-exclaimation/pioneer", from: "0.8.6")
```

## Swift for GraphQL
Expand Down
4 changes: 2 additions & 2 deletions Sources/Pioneer/Extensions/Closure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

/// Define an expression from a closure
/// - Returns: The returned value of this closure
func def<ReturnType>(_ fn: () -> ReturnType) -> ReturnType {
fn()
func def<ReturnType>(_ fn: () throws -> ReturnType) rethrows -> ReturnType {
try fn()
}
33 changes: 32 additions & 1 deletion Sources/Pioneer/GraphQL/BuiltinTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ public typealias NoArgs = NoArguments
/// The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable.
public struct ID : Codable, ExpressibleByStringLiteral, CustomStringConvertible, Hashable, Sendable {
/// Inner string properties
public var id: String
private var id: String

public init(_ id: String) {
self.id = id
}

public init(uuid: UUID) {
self.id = uuid.uuidString
}

public init(stringLiteral value: String) {
id = value
}
Expand Down Expand Up @@ -65,6 +69,33 @@ public struct ID : Codable, ExpressibleByStringLiteral, CustomStringConvertible,
public var string: String {
id
}

/// UUID value of this ID if possible
public var uuid: UUID? {
UUID(id)
}

/// Get a UUID from this ID scalar
/// - Returns: The UUID object if possible otherwise throw an error
public func toUUID() throws -> UUID {
guard let uuid = self.uuid else {
throw ConversionError(id: id, reason: "Cannot convert this ID that is not in UUID string format to UUID")
}
return uuid
}

/// Conversion Error for the ID scalar
public struct ConversionError: Error {
/// The ID in question as string
public var id: String

/// The detailed reasoning why conversion failed
public var reason: String

public var localizedDescription: String {
"ID.ConversionError ('\(id)'): \"\(reason)\""
}
}
}


Expand Down
Loading

0 comments on commit ee1a0fe

Please sign in to comment.