An open API service indexing awesome lists of open source software.

https://github.com/graphqlswift/graphql-hummingbird

Easily expose GraphQL APIs in Hummingbird
https://github.com/graphqlswift/graphql-hummingbird

graphql hummingbird server swift

Last synced: 4 months ago
JSON representation

Easily expose GraphQL APIs in Hummingbird

Awesome Lists containing this project

README

          

# GraphQLHummingbird

[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FGraphQLSwift%2Fgraphql-hummingbird%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/GraphQLSwift/graphql-hummingbird)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FGraphQLSwift%2Fgraphql-hummingbird%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/GraphQLSwift/graphql-hummingbird)

> ***WARNING***: This package is in v0.x beta. It's API is still evolving and is subject to breaking changes in minor version bumps.

A Swift library for integrating [GraphQL](https://github.com/GraphQLSwift/GraphQL) with [Hummingbird](https://github.com/hummingbird-project/hummingbird), enabling you to easily expose GraphQL APIs in your Hummingbird applications.

## Features

- Simple integration of GraphQL schemas with Hummingbird routing
- Compatibility with the [GraphQL over HTTP spec](https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md)
- Subscription support using WebSockets, with support for [`graphql-transport-ws`](https://github.com/GraphQLSwift/GraphQLTransportWS) and [`graphql-ws`](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md) subprotocols
- Built-in [GraphiQL](https://github.com/graphql/graphiql) IDE

## Installation

Add GraphQLHummingbird as a dependency in your `Package.swift`:

```swift
dependencies: [
.package(url: "https://github.com/NeedleInAJayStack/graphql-hummingbird.git", from: "1.0.0"),
]
```

Then add it to your target:

```swift
.target(
name: "YourTarget",
dependencies: [
.product(name: "GraphQLHummingbird", package: "graphql-hummingbird"),
]
)
```

## Usage

See [the HelloWorld project](https://github.com/GraphQLSwift/graphql-hummingbird/tree/main/Examples/HelloWorld) for a full working example.

### Basic Example

```swift
import GraphQL
import GraphQLHummingbird
import Hummingbird

// Define your GraphQL schema
// To construct schemas, consider using `Graphiti` or `graphql-generator`
let schema = try GraphQLSchema(
query: GraphQLObjectType(
name: "Query",
fields: [
"hello": GraphQLField(
type: GraphQLString,
resolve: { _, _, _, _ in
"World"
}
)
]
)
)

// Define your Context
struct GraphQLContext: Sendable {}

// Create router and register GraphQL
let router = Router()
router.graphql(schema: schema) { _, _ in
return GraphQLContext()
}

// Create and run the application
let app = Application(
router: router,
configuration: .init(address: .hostname("127.0.0.1", port: 8080))
)
try await app.runService()
```

That's it! You can now view the GraphiQL IDE at http://localhost:8080/graphql, or query directly using `GET` or `POST`:

```bash
curl -X POST http://localhost:8080/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ hello }"}'
```

Response:
```json
{
"data": {
"hello": "World"
}
}
```

See the `graphql` function documentation for advanced configuration options.

### WebSockets

Subscription support via WebSockets can be enabled by calling the `graphqlWebSocket` function on a `Router` whose context conforms to `WebSocketRequestContext`, from the `HummingbirdWebSocket` package:

```swift
import GraphQL
import GraphQLHummingbird
import Hummingbird
import HummingbirdWebSocket

struct MyWebSocketContext: WebSocketRequestContext, RequestContext {
...
}

let router = Router(context: MyContext.self)
router.graphql(schema: schema) { _, _ in
GraphQLContext()
}
let webSocketRouter = Router(context: MyWebSocketContext.self)
webSocketRouter.graphqlWebSocket(schema: schema) { _, _ in
GraphQLContext()
}
let app = Application(
router: router,
server: .http1WebSocketUpgrade(webSocketRouter: webSocketRouter)
)
```

The example above follows Hummingbird best practices when it uses a separate router for HTTP and WebSocket requests. For more details, see the [Hummingbird WebSocket documentation](https://docs.hummingbird.codes/2.0/documentation/hummingbird/websocketserverupgrade#Overview).