SwiftGraphQL packs a fully-featured GraphQL subscription client that conforms to the GraphQL over WebSocket protocol specification.

You can use GraphQLWebSocket as a standalone client or with the query-builder or in combination with SwiftGraphQLClient.

Using WebSocketExchange

To create a subscription request on the client, you need to use WebSocketExchange. WebSocketExchange depends on the GraphQLWebSocket client that you may further modify to suit your needs.

let http_endpoint = URL(string: "http://localhost:8080/graphql")!
let ws_endpoint = URL(string: "ws://127.0.0.1:4000/graphql")!

let config = GraphQLWebSocketConfiguration()
config.behaviour = .lazy(closeTimeout: 60)

let ws = GraphQLWebSocket(
	request: URLRequest(url: ws_endpoint),
	config: config
)

let client = SwiftGraphQLClient.Client(
	request: URLRequest(url: http_endpoint),
	exchanges: [
		FetchExchange(),
		WebSocketExchange(client: socket)
	]
)

let args = ExecutionArgs(
    query: """
    subscription Counter {
        count(from: 10, to: 1)
    }
    """,
    variables: [:]
)

client.subscribe(args)
	.sink { completion in
		print(completion)
	} receiveValue: { result in
		print(result)
	}

SwiftGraphQLClient also exposes public utility methods that let you use SwiftGraphQL query builder to make a subscription and decode responses automatically.

Using GraphQLWebSocketClient

In case you don’t want to go all-in on SwiftGraphQL, you can use just the WebSockets implementation. It doesn’t depend on any other parts of the codebase and strictly follows the GraphQL over WebSocket protocol.

let ws = GraphQLWebSocket(request: API_ENDPOINT)

let args = ExecutionArgs(
    query: """
    subscription Counter {
        count(from: 10, to: 1)
    }
    """,
    variables: [:]
)

client.subscribe(args)
    .sink { completion in
        print(completion)
    } receiveValue: { result in
        print(result)
    }

Additionally, if you import the SwiftGraphQL library and make a selection, you can also automatically encode queries and decode responses.

import SwiftGraphQL

let subscription = Selection.Subscription<Int> {
    $0.count(from: 10, to: 1)
}

let ws = GraphQLWebSocket(request: API_ENDPOINT)
client.subscribe(subscription)
    .sink { completion in
        print(completion)
    } receiveValue: { result in
        print(result)
    }