Latest 0.0.4
Homepage https://github.com/florianmari/LiveGQL
License MIT
Platforms ios 9.0
Dependencies Starscream, JSONCodable
Authors

LiveGQL: Use GraphQL Websocket subscription in Swift

LiveGQL is a simple library to use GraphQL Subscribtion on WebSocket based on Apollo Protocol.

Hello folks! I made this first preview of a future complete library, you can try it now and see in your console how it works! I will provide a fully functionnal version next week! But it’s ready to use in development environment

Features

  • [x] Connect to a GraphQL WebSocket server
  • [x] Send messages
  • [x] Subscribe / unsubscribe
  • [x] Close connection
  • [x] Data handling (delegate)
  • [ ] Error handling
  • [ ] JSON parsing
  • [ ] Implement all Apollo protocol (today just Client part totally implemented)

Requirements

  • iOS 9.0
  • Xcode >= 8.1
  • Swift >= 3.0

We also use Starscream and JSONCodable, thanks to them

Communication

  • Use issue if you have any problem
  • Don’t hesitate to contribute to the project with a pull request

Installation

CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'LiveGQL', '~> 0.0.4'
end

Manually

Just copy files in the Source folder!

Usage

Initializing

Important: to not fall out the variable in the scope, please instantiate above your viewDidLoad() for example.

import LiveGQL

let gql = LiveGQL(socket: "ws://localhost:7003/feedback")

gql.initServer()

Subscribe / Unsubscribe

Subscribe

Just call subscribe method, set an identifier and your subscription query as well.

gql.subscribe(graphql: "subscription {feedbackAdded {id, text}}", identifier: "feed")
Treat server response

You have to implement delegate method, in your main ViewController (for example) just att that

override func viewDidLoad() {
        super.viewDidLoad()
        gql.delegate = self
        // Do any additional setup after loading the view, typically from a nib.
    }

Below your class add the folowing extension and implement the method:

extension ViewController: LiveGQLDelegate {
    func receivedMessage(text: String) {
        print("Received Message: (text)")
    }
}

Unsubscribe

Just call unsubscribe method and your identifier

gql.unsubscribe(subscribtion: "feed")

Close connection

gql.closeConnection()

Bugs

Bugs I know:

  • I don’t verify if the initMessage (initServer()) has been sent (if it hasn’t subscribe doesn’t work)
  • closeConnection() may be bug
  • Error are not handled but your app won’t crash (because it’s on the stream ;))

Latest podspec

{
    "name": "LiveGQL",
    "version": "0.0.4",
    "summary": "Implementation of GraphQL Subscription Websocket in Swift",
    "description": "Simple implementation in Swift of the PROTOCOL to communicate with a GraphQL Apollo WS server.",
    "homepage": "https://github.com/florianmari/LiveGQL",
    "license": "MIT",
    "authors": {
        "Florian Mari": "[email protected]"
    },
    "source": {
        "git": "https://github.com/florianmari/LiveGQL.git",
        "tag": "0.0.4"
    },
    "source_files": "Source/Classes/**/*",
    "platforms": {
        "ios": "9.0"
    },
    "exclude_files": "Classes/Exclude",
    "dependencies": {
        "Starscream": [
            "~> 2.0.3"
        ],
        "JSONCodable": [
            "~> 3.0.1"
        ]
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This